You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
RedditVideoMakerBot/utils/subreddit.py

82 lines
2.8 KiB

import json
from os.path import exists
from utils import settings
from utils.console import print_substep
def get_subreddit_undone(submissions: list, subreddit, times_checked=0):
"""_summary_
Args:
2 years ago
times_checked: (int): For internal use, number of times function was called
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
"""
# 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 submission in 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"]):
2 years ago
print_substep(
"This post has under the specified minimum of comments"
2 years ago
f'({settings.config["reddit"]["thread"]["min_comments"]}). Skipping...'
2 years ago
)
continue
return submission
print("all submissions have been done going by top submission order")
2 years ago
VALID_TIME_FILTERS = [
"hour",
2 years ago
"day",
2 years ago
"month",
"week",
"year",
"all",
] # set doesn't have __getitem__
2 years ago
index = times_checked + 1 if times_checked != 0 else times_checked
if index == len(VALID_TIME_FILTERS):
print("all time filters have been checked you absolute madlad ")
return get_subreddit_undone(
2 years ago
subreddit.top(
time_filter=VALID_TIME_FILTERS[index], limit=(50 if int(index) == 0 else index + 1 * 50)
2 years ago
),
subreddit,
times_checked=index,
2 years ago
) # 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