Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/utils/videos.py
327 views
1
import json
2
import time
3
4
from praw.models import Submission
5
6
from utils import settings
7
from utils.console import print_step
8
9
10
def check_done(
11
redditobj: Submission,
12
) -> Submission:
13
# don't set this to be run anyplace that isn't subreddit.py bc of inspect stack
14
"""Checks if the chosen post has already been generated
15
16
Args:
17
redditobj (Submission): Reddit object gotten from reddit/subreddit.py
18
19
Returns:
20
Submission|None: Reddit object in args
21
"""
22
with open("./video_creation/data/videos.json", "r", encoding="utf-8") as done_vids_raw:
23
done_videos = json.load(done_vids_raw)
24
for video in done_videos:
25
if video["id"] == str(redditobj):
26
if settings.config["reddit"]["thread"]["post_id"]:
27
print_step(
28
"You already have done this video but since it was declared specifically in the config file the program will continue"
29
)
30
return redditobj
31
print_step("Getting new post as the current one has already been done")
32
return None
33
return redditobj
34
35
36
def save_data(subreddit: str, filename: str, reddit_title: str, reddit_id: str, credit: str):
37
"""Saves the videos that have already been generated to a JSON file in video_creation/data/videos.json
38
39
Args:
40
filename (str): The finished video title name
41
@param subreddit:
42
@param filename:
43
@param reddit_id:
44
@param reddit_title:
45
"""
46
with open("./video_creation/data/videos.json", "r+", encoding="utf-8") as raw_vids:
47
done_vids = json.load(raw_vids)
48
if reddit_id in [video["id"] for video in done_vids]:
49
return # video already done but was specified to continue anyway in the config file
50
payload = {
51
"subreddit": subreddit,
52
"id": reddit_id,
53
"time": str(int(time.time())),
54
"background_credit": credit,
55
"reddit_title": reddit_title,
56
"filename": filename,
57
}
58
done_vids.append(payload)
59
raw_vids.seek(0)
60
json.dump(done_vids, raw_vids, ensure_ascii=False, indent=4)
61
62