Exemple #1
0
def save_threads(youtube: YouTube,
                 da: DataAccess,
                 from_vid: str,
                 dry_run: bool = True):
    for video in da.gen_all_videos_in_order(from_vid):
        vid = video["id"]
        vtitle = video["snippet"]["title"]

        print()
        print(f"Processing {vtitle}...")
        if da.have_comments_for_video(vid):
            print(f'We\'ve already got comments for "{vtitle}".')
            print("Skipping...")
            continue

        if not dry_run:
            threads = youtube.get_comment_threads_for_video(vid)

            with open(os.path.join(ROOT_DIR, "db", "commentThreads",
                                   f"{vid}.json"),
                      mode="w") as f:
                f.write(json.dumps(threads))
        else:
            print("\t(Dry run)")

        print(f'Threads for "{vtitle}" saved.')
        print()
        print("------------------------------------------------------------")

        # Give a little delay between batches.
        # - DOS paranoia.
        sleep(1)