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']
|
return playlist_info['items'][0]['contentDetails']['itemCount']
|
||||||
|
|
||||||
def list_songs(youtube, channel_id):
|
def list_songs(youtube, channel_id, stdout=False):
|
||||||
max_results = 50
|
max_results = 50
|
||||||
request = youtube.playlistItems().list(
|
request = youtube.playlistItems().list(
|
||||||
part="snippet,contentDetails",
|
part="snippet,contentDetails",
|
||||||
@@ -41,9 +41,15 @@ def list_songs(youtube, channel_id):
|
|||||||
print()
|
print()
|
||||||
|
|
||||||
# Process the first page of results
|
# Process the first page of results
|
||||||
|
deleted = 0
|
||||||
|
songs_list = []
|
||||||
|
|
||||||
for idx, item in enumerate(response['items'], start=1):
|
for idx, item in enumerate(response['items'], start=1):
|
||||||
title = item['snippet']['title']
|
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
|
# Continue making requests for additional pages using pageToken
|
||||||
new_starting = 51
|
new_starting = 51
|
||||||
@@ -55,12 +61,28 @@ def list_songs(youtube, channel_id):
|
|||||||
pageToken=response['nextPageToken']
|
pageToken=response['nextPageToken']
|
||||||
)
|
)
|
||||||
response = request.execute()
|
response = request.execute()
|
||||||
|
|
||||||
# Process the results for the additional pages
|
# Process the results for the additional pages
|
||||||
for idx, item in enumerate(response['items'], start=new_starting):
|
for idx, item in enumerate(response['items'], start=new_starting):
|
||||||
title = item['snippet']['title']
|
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
|
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):
|
def add_song(youtube, channel_id, song):
|
||||||
pos = get_playlist_length(youtube, channel_id)
|
pos = get_playlist_length(youtube, channel_id)
|
||||||
|
|
||||||
@@ -83,23 +105,17 @@ def add_song(youtube, channel_id, song):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description="Manage YouTube playlist songs.")
|
parser = argparse.ArgumentParser(description="Manage YouTube playlist songs.")
|
||||||
parser.add_argument("channel_name", help="Name of the YouTube channel")
|
parser.add_argument("-l", "--list", action="store_true", help="List songs in the playlist")
|
||||||
parser.add_argument(
|
parser.add_argument("-a", "--add", metavar="song", help="Add a song to the playlist")
|
||||||
"--list",
|
parser.add_argument("channel_name", help="Name of the playlist")
|
||||||
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"
|
|
||||||
)
|
|
||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
channel_name = args.channel_name
|
channel_name = args.channel_name
|
||||||
channel_id = channels.get(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:
|
if not channel_id:
|
||||||
print(f"Error: Channel '{channel_name}' not found.")
|
print(f"Error: Channel '{channel_name}' not found.")
|
||||||
return
|
return
|
||||||
@@ -121,16 +137,14 @@ def main():
|
|||||||
credentials=credentials)
|
credentials=credentials)
|
||||||
|
|
||||||
if args.list:
|
if args.list:
|
||||||
list_songs(youtube, channel_id)
|
list_songs(youtube, channel_id, stdout=False)
|
||||||
elif args.add:
|
elif args.add:
|
||||||
target_channel, song = args.add
|
song = args.add
|
||||||
# # this assumes a song is passed as in either of the following ways
|
# # this assumes a song is passed as in either of the following ways
|
||||||
# # https://youtu.be/eEnyi9L6KP4 --> split by "/"
|
# # https://youtu.be/eEnyi9L6KP4 --> split by "/"
|
||||||
# # https://youtube.com/watch?v=Ez-gizOF0Wo --> split by "="
|
# # https://youtube.com/watch?v=Ez-gizOF0Wo --> split by "="
|
||||||
song = re.split(r"/|=", song)[-1]
|
song = re.split(r"/|=", song)[-1]
|
||||||
add_song(youtube, channels.get(target_channel), song)
|
add_song(youtube, channel_id, song)
|
||||||
else:
|
|
||||||
print("Invalid command. Use --list or --add.")
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user