Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
elebumm
GitHub Repository: elebumm/RedditVideoMakerBot
Path: blob/master/utils/subreddit.py
327 views
1
import json
2
from os.path import exists
3
4
from utils import settings
5
from utils.ai_methods import sort_by_similarity
6
from utils.console import print_substep
7
8
9
def get_subreddit_undone(submissions: list, subreddit, times_checked=0, similarity_scores=None):
10
"""_summary_
11
12
Args:
13
submissions (list): List of posts that are going to potentially be generated into a video
14
subreddit (praw.Reddit.SubredditHelper): Chosen subreddit
15
16
Returns:
17
Any: The submission that has not been done
18
"""
19
# Second try of getting a valid Submission
20
if times_checked and settings.config["ai"]["ai_similarity_enabled"]:
21
print("Sorting based on similarity for a different date filter and thread limit..")
22
submissions = sort_by_similarity(
23
submissions, keywords=settings.config["ai"]["ai_similarity_enabled"]
24
)
25
26
# recursively checks if the top submission in the list was already done.
27
if not exists("./video_creation/data/videos.json"):
28
with open("./video_creation/data/videos.json", "w+") as f:
29
json.dump([], f)
30
with open("./video_creation/data/videos.json", "r", encoding="utf-8") as done_vids_raw:
31
done_videos = json.load(done_vids_raw)
32
for i, submission in enumerate(submissions):
33
if already_done(done_videos, submission):
34
continue
35
if submission.over_18:
36
try:
37
if not settings.config["settings"]["allow_nsfw"]:
38
print_substep("NSFW Post Detected. Skipping...")
39
continue
40
except AttributeError:
41
print_substep("NSFW settings not defined. Skipping NSFW post...")
42
if submission.stickied:
43
print_substep("This post was pinned by moderators. Skipping...")
44
continue
45
if (
46
submission.num_comments <= int(settings.config["reddit"]["thread"]["min_comments"])
47
and not settings.config["settings"]["storymode"]
48
):
49
print_substep(
50
f'This post has under the specified minimum of comments ({settings.config["reddit"]["thread"]["min_comments"]}). Skipping...'
51
)
52
continue
53
if settings.config["settings"]["storymode"]:
54
if not submission.selftext:
55
print_substep("You are trying to use story mode on post with no post text")
56
continue
57
else:
58
# Check for the length of the post text
59
if len(submission.selftext) > (
60
settings.config["settings"]["storymode_max_length"] or 2000
61
):
62
print_substep(
63
f"Post is too long ({len(submission.selftext)}), try with a different post. ({settings.config['settings']['storymode_max_length']} character limit)"
64
)
65
continue
66
elif len(submission.selftext) < 30:
67
continue
68
if settings.config["settings"]["storymode"] and not submission.is_self:
69
continue
70
if similarity_scores is not None:
71
return submission, similarity_scores[i].item()
72
return submission
73
print("all submissions have been done going by top submission order")
74
VALID_TIME_FILTERS = [
75
"day",
76
"hour",
77
"month",
78
"week",
79
"year",
80
"all",
81
] # set doesn't have __getitem__
82
index = times_checked + 1
83
if index == len(VALID_TIME_FILTERS):
84
print("All submissions have been done.")
85
86
return get_subreddit_undone(
87
subreddit.top(
88
time_filter=VALID_TIME_FILTERS[index],
89
limit=(50 if int(index) == 0 else index + 1 * 50),
90
),
91
subreddit,
92
times_checked=index,
93
) # all the videos in hot have already been done
94
95
96
def already_done(done_videos: list, submission) -> bool:
97
"""Checks to see if the given submission is in the list of videos
98
99
Args:
100
done_videos (list): Finished videos
101
submission (Any): The submission
102
103
Returns:
104
Boolean: Whether the video was found in the list
105
"""
106
107
for video in done_videos:
108
if video["id"] == str(submission):
109
return True
110
return False
111
112