def get_video(args): """Get a single videos.""" session = get_session() folder = args.get("folder") clip = Clip.get_or_create(session, args["viewkey"]) if clip.completed: if clip.title is not None and clip.extension is not None: target_path = get_clip_path(folder, clip.title, clip.extension) link_duplicate(clip, target_path) logger.info("Clip already exists") return success, info = download_video(args["viewkey"], name=folder) clip.title = info["title"] clip.tags = info["tags"] clip.cartegories = info["categories"] clip.completed = True clip.location = info["out_path"] clip.extension = info["ext"] session.commit()
def update(args): """Get all information about a user and download their videos.""" session = get_session() threshold = datetime.now() - timedelta(hours=8) # Go through all users users = (session.query(User).filter(User.last_scan <= threshold).order_by( User.key).all()) for user in users: # Re query the user type, since this can change over time print(user.key) info = get_user_info(user.key) user.user_type = info["type"] logger.info(f"\nStart downloading user: {user.name}") if download_user_videos(session, user): user.last_scan = datetime.now() session.commit() # Go through all playlists playlists = (session.query(Playlist).filter( Playlist.last_scan <= threshold).order_by(Playlist.name).all()) for playlist in playlists: logger.info(f"\nStart downloading playlist: {playlist.name}") if download_playlist_videos(session, playlist): user.last_scan = datetime.now() session.commit() # Go through all channels channels = (session.query(Channel).filter( Channel.last_scan <= threshold).order_by(Channel.name).all()) for channel in channels: logger.info(f"\nStart downloading channel: {channel.name}") if download_channel_videos(session, channel): user.last_scan = datetime.now() session.commit() clips = (session.query(Clip).filter(Clip.completed.is_(False)).filter( Clip.location.isnot(None)).all()) for clip in clips: download_video(clip.viewkey, name=os.path.dirname(clip.location)) clip.completed = True session.commit()
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