From 8b5670a8ebe17e8fa4f2bac00cb7ae2d40d5b290 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 1 Jun 2022 19:12:19 -0400 Subject: [PATCH] feat: ads a check to see if the video has already been done as suggested in #12 closes #12 --- README.md | 5 ++++- reddit/subreddit.py | 5 ++++- utils/videos.py | 22 ++++++++++++++++++++++ video_creation/TTSwrapper.py | 5 ++--- video_creation/data/videos.json | 23 +++++++++++++++++++++++ video_creation/final_video.py | 18 +++++++++++++++++- 6 files changed, 72 insertions(+), 6 deletions(-) create mode 100644 utils/videos.py create mode 100644 video_creation/data/videos.json diff --git a/README.md b/README.md index 6058b65..d0e59df 100644 --- a/README.md +++ b/README.md @@ -44,8 +44,11 @@ In its current state, this bot does exactly what it needs to do. However, lots o I have tried to simplify the code so anyone can read it and start contibuting at any skill level. Don't be shy :) contribute! +- [ ] Creating better documentation and adding a command line interface. - [x] Allowing users to choose a reddit thread instead of being randomized. - [x] Allowing users to choose a background that is picked instead of the Minecraft one. - [x] Allowing users to choose between any subreddit. - [x] Allowing users to change voice. -- [ ] Creating better documentation and adding a command line interface. +- [x] Checks if a video has already been created +- [x] Light and Dark modes +- [x] Nsfw post filter diff --git a/reddit/subreddit.py b/reddit/subreddit.py index ada56e5..049bb06 100644 --- a/reddit/subreddit.py +++ b/reddit/subreddit.py @@ -6,6 +6,8 @@ import random from dotenv import load_dotenv from os import getenv, environ +from utils.videos import check_done + def ascifi(text): regrex_pattern = re.compile(pattern="[" @@ -45,12 +47,13 @@ def get_subreddit_threads(): else: threads = subreddit.hot(limit=25) submission = list(threads)[random.randrange(0, 25)] + submission = check_done(submission) - print(submission) print_substep( f'subreddit thread is: {submission.title}\n(if you dont like this, you can change it by exiting and rerunning the program)') environ["VIDEO_TITLE"] = str(ascifi(submission.title)) + environ["VIDEO_ID"] = str(ascifi(submission.id)) try: content["thread_url"] = submission.url diff --git a/utils/videos.py b/utils/videos.py new file mode 100644 index 0000000..66b9a75 --- /dev/null +++ b/utils/videos.py @@ -0,0 +1,22 @@ +import inspect +import json +from os import getenv + +from utils.console import print_step + + +def check_done(redditobj): # don't set this to be run anyplace that isn't subreddit.py bc of inspect stack + """params: + reddit_object: The Reddit Object you received in askreddit.py""" + if getenv('POST_ID'): + print_step( + 'You already have done this video but since it was declared specifically in the .env file the program will continue') + return redditobj + with open('./video_creation/data/videos.json', 'r') as done_vids_raw: + done_videos = json.load(done_vids_raw) + for video in done_videos: + if video['id'] == str(redditobj): + print_step('Getting new post as the current one has already been done') + from reddit.subreddit import get_subreddit_threads + return get_subreddit_threads() # recursive func + return redditobj diff --git a/video_creation/TTSwrapper.py b/video_creation/TTSwrapper.py index 130f2de..e239e81 100644 --- a/video_creation/TTSwrapper.py +++ b/video_creation/TTSwrapper.py @@ -65,9 +65,8 @@ class TTTTSWrapper: # TikTok Text-to-Speech Wrapper b64d = base64.b64decode(vstr) - out = open(filename, "wb") - out.write(b64d) - out.close() + with open(filename, "wb") as out: + out.write(b64d) @staticmethod def randomvoice(): diff --git a/video_creation/data/videos.json b/video_creation/data/videos.json new file mode 100644 index 0000000..19419c0 --- /dev/null +++ b/video_creation/data/videos.json @@ -0,0 +1,23 @@ +[ + { + "id": "ult7el", + "time": "1654117088", + "background_credit": "Orbital Gameplay", + "reddit_title": "What famous place is not worth visiting?", + "filename": "What famous place is not worth....mp4" + }, + { + "id": "v2hup1", + "time": "1654118137", + "background_credit": "bbswitzer", + "reddit_title": "What job interview question do you think is pointless?", + "filename": "What job interview question do....mp4" + }, + { + "id": "v2nqiu", + "time": "1654118543", + "background_credit": "bbswitzer", + "reddit_title": "What is a fact that people are not ready to hear?", + "filename": "What is a fact that people are....mp4" + } +] \ No newline at end of file diff --git a/video_creation/final_video.py b/video_creation/final_video.py index e5dfe68..296761d 100644 --- a/video_creation/final_video.py +++ b/video_creation/final_video.py @@ -1,4 +1,6 @@ +import json import os +import time from moviepy.editor import (VideoFileClip, AudioFileClip, ImageClip, concatenate_videoclips, concatenate_audioclips, CompositeAudioClip, CompositeVideoClip) @@ -44,8 +46,22 @@ def make_final_video(number_of_clips): else: return title[0:30] + "..." - final.write_videofile(f"results/{get_video_title()}.mp4", fps=30, audio_codec="aac", audio_bitrate="192k") + filename = f'{get_video_title()}.mp4' + def save_data(): + with open('./video_creation/data/videos.json', 'r+') as raw_vids: + done_vids = json.load(raw_vids) + if str(os.getenv("VIDEO_ID")) in [video['id'] for video in done_vids]: + return # video already done but was specified to continue anyway in the .env file + payload = {"id": str(os.getenv("VIDEO_ID")), 'time': str(int(time.time())), + "background_credit": str(os.getenv('background_credit')), + "reddit_title": str(os.getenv('VIDEO_TITLE')), "filename": filename} + done_vids.append(payload) + raw_vids.seek(0) + json.dump(done_vids, raw_vids, ensure_ascii=False, indent=4) + + save_data() + final.write_videofile(f"results/{filename}", fps=30, audio_codec="aac", audio_bitrate="192k") print_step("Removing temporary files 🗑") cleanups = cleanup() print_substep(f"Removed {cleanups} temporary files 🗑")