def download_user_videos(session, user): """Download all videos of a user.""" video_viewkeys = get_user_video_viewkeys(user) # Try to get all uploaded videos video_upload_viewkeys = get_video_upload_viewkeys(user) # If that doesn't work, try to get all public uploaded videos if len(video_upload_viewkeys) == 0: video_upload_viewkeys = get_video_upload_viewkeys(user, True) viewkeys = set(video_viewkeys + video_upload_viewkeys) if len(viewkeys) == 0: logger.error(f"Found 0 videos for user {user.key}. Aborting") sys.exit(1) full_success = True logger.info(f"Found {len(viewkeys)} videos.") for viewkey in viewkeys: clip = Clip.get_or_create(session, viewkey, user) # The clip has already been downloaded, skip it. if clip.completed: if clip.title is not None and clip.extension is not None: target_path = get_clip_path(user.name, clip.title, clip.extension) link_duplicate(clip, target_path) if clip.user is None: clip.user = user session.commit() continue success, info = download_video(viewkey, user.name) if success: clip.title = info["title"] clip.tags = info["tags"] clip.cartegories = info["categories"] clip.completed = True clip.user = user clip.location = info["out_path"] clip.extension = info["ext"] logger.info(f"New video: {clip.title}") else: full_success = False session.commit() time.sleep(20) return full_success
def download_playlist_videos(session, playlist): """Download all videos of a playlist.""" viewkeys = set(get_playlist_video_viewkeys(playlist)) if len(viewkeys) == 0: logger.error(f"Found 0 videos in playlist {Playlist.id}. Aborting") sys.exit(1) full_success = True logger.info(f"Found {len(viewkeys)} videos.") for viewkey in viewkeys: clip = Clip.get_or_create(session, viewkey) # The clip has already been downloaded, skip it. if clip.completed: if clip.title is not None and clip.extension is not None: target_path = get_clip_path(playlist.name, clip.title, clip.extension) link_duplicate(clip, target_path) continue success, info = download_video(viewkey, f"playlists/{playlist.name}") if success: clip.title = info["title"] clip.tags = info["tags"] clip.cartegories = info["categories"] clip.completed = True clip.location = info["out_path"] clip.extension = info["ext"] logger.info(f"New video: {clip.title}") else: full_success = False session.commit() time.sleep(20) return full_success