Add videos from CSV files
This commit is contained in:
parent
a9f006bc84
commit
07d28c6d4f
30
main.py
30
main.py
@ -115,7 +115,7 @@ def list_playlist(yt_api, playlist_id: str):
|
||||
|
||||
|
||||
def add_video_to_playlist(yt_api, video_id: str, playlist_id: str,
|
||||
dry_run: bool = False) -> bool:
|
||||
dry_run: bool = False) -> bool:
|
||||
playlist_name = get_playlist_name(playlist_id)
|
||||
video_info = get_video_info(yt_api, video_id)
|
||||
video_title = _truncate_title(video_info['snippet']['title'])
|
||||
@ -227,7 +227,13 @@ def main():
|
||||
|
||||
parser_add = subparsers.add_parser('add', help='Add videos to a playlist')
|
||||
parser_add.add_argument('playlist', help='Playlist name/ID')
|
||||
parser_add.add_argument('video_ids', nargs='+', help='Video IDs to add')
|
||||
parser_add.add_argument('video_ids', nargs='*', help='Video IDs to add')
|
||||
|
||||
parser_add_csv = subparsers.add_parser('add-csv', help='Add videos to a playlist from a CSV file')
|
||||
parser_add_csv.add_argument('playlist', help='Playlist name/ID')
|
||||
parser_add_csv.add_argument('csv', help='CSV file with video IDs')
|
||||
parser_add_csv.add_argument('-l', '--limit', type=int, default=-1,
|
||||
help='Limit number of videos to process')
|
||||
|
||||
parser_copy = subparsers.add_parser('copy', help='Copy videos from one playlist to another')
|
||||
parser_copy.add_argument('src_playlist', help='Source playlist name/ID')
|
||||
@ -284,6 +290,24 @@ def main():
|
||||
continue
|
||||
add_video_to_playlist(youtube, video_id, playlist, args.dry_run)
|
||||
|
||||
elif args.command == 'add-csv':
|
||||
video_ids = []
|
||||
with open(args.csv, newline='') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
next(reader, None) # skip the headers
|
||||
video_ids.extend(row[0] for row in reader if row)
|
||||
playlist = get_playlist_id(args.playlist)
|
||||
pl_videos = {video['snippet']['resourceId']['videoId']
|
||||
for video in list_playlist(youtube, playlist)}
|
||||
if args.limit > 0:
|
||||
video_ids = video_ids[:args.limit]
|
||||
for video_id in video_ids:
|
||||
if video_id in pl_videos:
|
||||
print(f'Video {video_id} already in playlist {args.playlist}')
|
||||
continue
|
||||
add_video_to_playlist(youtube, video_id, playlist, args.dry_run)
|
||||
|
||||
|
||||
elif args.command == "dups":
|
||||
processed = 0
|
||||
playlist_id = get_playlist_id(args.playlist)
|
||||
@ -299,6 +323,8 @@ def main():
|
||||
else:
|
||||
plitems_processed.add(video_id)
|
||||
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
|
||||
Loading…
Reference in New Issue
Block a user