Fix --add flag
This commit is contained in:
@@ -30,7 +30,7 @@ def get_playlist_length(youtube, playlist_id):
|
||||
|
||||
return playlist_info['items'][0]['contentDetails']['itemCount']
|
||||
|
||||
def list_songs(youtube, channel_id):
|
||||
def list_songs(youtube, channel_id, stdout=False):
|
||||
max_results = 50
|
||||
request = youtube.playlistItems().list(
|
||||
part="snippet,contentDetails",
|
||||
@@ -41,9 +41,15 @@ def list_songs(youtube, channel_id):
|
||||
print()
|
||||
|
||||
# Process the first page of results
|
||||
deleted = 0
|
||||
songs_list = []
|
||||
|
||||
for idx, item in enumerate(response['items'], start=1):
|
||||
title = item['snippet']['title']
|
||||
print(f"{idx:02d} - {title}")
|
||||
if title == "Deleted video":
|
||||
deleted += 1
|
||||
continue
|
||||
songs_list.append(f"{idx-deleted:02d} - {title}")
|
||||
|
||||
# Continue making requests for additional pages using pageToken
|
||||
new_starting = 51
|
||||
@@ -55,12 +61,28 @@ def list_songs(youtube, channel_id):
|
||||
pageToken=response['nextPageToken']
|
||||
)
|
||||
response = request.execute()
|
||||
|
||||
# Process the results for the additional pages
|
||||
for idx, item in enumerate(response['items'], start=new_starting):
|
||||
title = item['snippet']['title']
|
||||
print(f"{idx:02d} - {title}")
|
||||
if title == "Deleted video":
|
||||
deleted += 1
|
||||
continue
|
||||
songs_list.append(f"{idx-deleted:02d} - {title}")
|
||||
new_starting += 50
|
||||
|
||||
if stdout:
|
||||
# Print to standard output
|
||||
print("\n".join(songs_list))
|
||||
|
||||
get_key_by_value = lambda dict, val: next((k for k, v in dict.items() if v == val), None)
|
||||
|
||||
channel_name = get_key_by_value(channels, channel_id)
|
||||
|
||||
# Save to file if output_file is provided
|
||||
with open(f"{channel_name} songs", 'w', encoding='utf-8') as file:
|
||||
file.write("\n".join(songs_list))
|
||||
|
||||
def add_song(youtube, channel_id, song):
|
||||
pos = get_playlist_length(youtube, channel_id)
|
||||
|
||||
@@ -83,23 +105,17 @@ def add_song(youtube, channel_id, song):
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description="Manage YouTube playlist songs.")
|
||||
parser.add_argument("channel_name", help="Name of the YouTube channel")
|
||||
parser.add_argument(
|
||||
"--list",
|
||||
action="store_true",
|
||||
help="List all songs in the playlist"
|
||||
)
|
||||
parser.add_argument(
|
||||
"--add",
|
||||
nargs=2,
|
||||
metavar=("channel", "song"),
|
||||
help="Add to the playlist a song"
|
||||
)
|
||||
parser.add_argument("-l", "--list", action="store_true", help="List songs in the playlist")
|
||||
parser.add_argument("-a", "--add", metavar="song", help="Add a song to the playlist")
|
||||
parser.add_argument("channel_name", help="Name of the playlist")
|
||||
|
||||
args = parser.parse_args()
|
||||
channel_name = args.channel_name
|
||||
channel_id = channels.get(channel_name)
|
||||
|
||||
if not args.list and not args.add:
|
||||
parser.error("Please provide either -l or -a option.")
|
||||
|
||||
if not channel_id:
|
||||
print(f"Error: Channel '{channel_name}' not found.")
|
||||
return
|
||||
@@ -121,16 +137,14 @@ def main():
|
||||
credentials=credentials)
|
||||
|
||||
if args.list:
|
||||
list_songs(youtube, channel_id)
|
||||
list_songs(youtube, channel_id, stdout=False)
|
||||
elif args.add:
|
||||
target_channel, song = args.add
|
||||
song = args.add
|
||||
# # this assumes a song is passed as in either of the following ways
|
||||
# # https://youtu.be/eEnyi9L6KP4 --> split by "/"
|
||||
# # https://youtube.com/watch?v=Ez-gizOF0Wo --> split by "="
|
||||
song = re.split(r"/|=", song)[-1]
|
||||
add_song(youtube, channels.get(target_channel), song)
|
||||
else:
|
||||
print("Invalid command. Use --list or --add.")
|
||||
add_song(youtube, channel_id, song)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user