From bac9bbd7b3c6d48a41a6692e9d9e5cf08f16e20c Mon Sep 17 00:00:00 2001 From: knoflook Date: Mon, 11 Apr 2022 13:46:52 +0200 Subject: [PATCH] vid: remove all vids if API down --- lumbunglib/video.py | 84 +++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 38 deletions(-) diff --git a/lumbunglib/video.py b/lumbunglib/video.py index 3623381..f120c1f 100644 --- a/lumbunglib/video.py +++ b/lumbunglib/video.py @@ -102,52 +102,60 @@ def main(): v = peertube.VideoApi(client) count = 100 page = 0 - response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) - - videos = response.to_dict() - total = videos['total'] - videos = videos['data'] - total -= count - if total > 0: - to_download = total // count - last_page = total % count - for i in range(to_download): - page += 1 - response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) - videos += response.to_dict()['data'] - if last_page > 0: - page += 1 - response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) - videos += response.to_dict()['data'][-1*last_page:] + try: + response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) + videos = response.to_dict() + total = videos['total'] + videos = videos['data'] + total -= count + if total > 0: + to_download = total // count + last_page = total % count + for i in range(to_download): + page += 1 + response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) + videos += response.to_dict()['data'] + if last_page > 0: + page += 1 + response = v.videos_get(count=count, filter="local", tags_one_of="publish", start=page) + videos += response.to_dict()['data'][-1*last_page:] - output_dir = os.environ.get("OUTPUT_DIR") + output_dir = os.environ.get("OUTPUT_DIR") - if not os.path.exists(output_dir): - os.mkdir(output_dir) + if not os.path.exists(output_dir): + os.mkdir(output_dir) - existing_posts = os.listdir(output_dir) + existing_posts = os.listdir(output_dir) - for video_metadata in videos: - post_name = slugify(video_metadata["name"]) + "-" + video_metadata["uuid"] - post_dir = os.path.join(output_dir, post_name) + for video_metadata in videos: + post_name = slugify(video_metadata["name"]) + "-" + video_metadata["uuid"] + post_dir = os.path.join(output_dir, post_name) - if ( - post_name not in existing_posts - ): # if there is a video we dont already have, make it - print( - "New: ", video_metadata["name"], "({})".format(video_metadata["uuid"]) - ) - create_post(post_dir, video_metadata, host) + if ( + post_name not in existing_posts + ): # if there is a video we dont already have, make it + print( + "New: ", video_metadata["name"], "({})".format(video_metadata["uuid"]) + ) + create_post(post_dir, video_metadata, host) - elif ( - post_name in existing_posts - ): # if we already have the video do nothing, possibly update - update_post(post_dir, video_metadata, host) - existing_posts.remove( - post_name - ) # create list of posts which have not been returned by peertube + elif ( + post_name in existing_posts + ): # if we already have the video do nothing, possibly update + update_post(post_dir, video_metadata, host) + existing_posts.remove( + post_name + ) # create list of posts which have not been returned by peertube + + except: + print("didn't get a response from peertube, instance might have been taken down or made private. removing all posts.") + output_dir = os.environ.get("OUTPUT_DIR") + if not os.path.exists(output_dir): + os.mkdir(output_dir) + existing_posts = os.listdir(output_dir) for post in existing_posts: print("deleted", post) # rm posts not returned shutil.rmtree(os.path.join(output_dir, post)) +