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