@ -9,13 +9,11 @@ from utils.subreddit import get_subreddit_undone
from utils . videos import check_done
def get_subreddit_threads ( ) :
def get_subreddit_threads ( POST_ID : str ) :
"""
Returns a list of threads from the AskReddit subreddit .
"""
submission = None
print_substep ( " Logging into Reddit. " )
content = { }
@ -29,11 +27,14 @@ def get_subreddit_threads():
passkey = f " { pw } : { code } "
else :
passkey = getenv ( " REDDIT_PASSWORD " )
username = getenv ( " REDDIT_USERNAME " )
if username . casefold ( ) . startswith ( ' u/ ' ) :
username = username [ 2 : ]
reddit = praw . Reddit (
client_id = getenv ( " REDDIT_CLIENT_ID " ) ,
client_secret = getenv ( " REDDIT_CLIENT_SECRET " ) ,
user_agent = " Accessing Reddit threads " ,
username = getenv( " REDDIT_USERNAME " ) ,
username = username ,
passkey = passkey ,
check_for_async = False ,
)
@ -41,7 +42,7 @@ def get_subreddit_threads():
# Ask user for subreddit input
print_step ( " Getting subreddit threads... " )
if not getenv (
" SUBREDDIT "
" SUBREDDIT "
) : # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
try :
subreddit = reddit . subreddit (
@ -58,20 +59,23 @@ def get_subreddit_threads():
f " Using subreddit: r/ { getenv ( ' SUBREDDIT ' ) } from environment variable config "
)
subreddit_choice = getenv ( " SUBREDDIT " )
if subreddit_choice . casefold ( ) . startswith ( ' r/ ' ) : # removes the r/ from the input
if subreddit_choice . casefold ( ) . startswith ( ' r/ ' ) : # removes the r/ from the input
subreddit_choice = subreddit_choice [ 2 : ]
subreddit = reddit . subreddit (
subreddit_choice
) # Allows you to specify in .env. Done for automation purposes.
if getenv ( " POST_ID " ) :
if POST_ID : # would only be called if there are multiple queued posts
submission = reddit . submission ( id = POST_ID )
elif getenv ( " POST_ID " ) and len ( getenv ( " POST_ID " ) . split ( ' + ' ) ) == 1 :
submission = reddit . submission ( id = getenv ( " POST_ID " ) )
else :
threads = subreddit . hot ( limit = 25 )
submission = get_subreddit_undone ( threads , subreddit )
submission = check_done ( submission ) # double checking
if submission is None :
return get_subreddit_threads ( ) # submission already done. rerun
return get_subreddit_threads ( POST_ID ) # submission already done. rerun
upvotes = submission . score
ratio = submission . upvote_ratio * 100
num_comments = submission . num_comments
@ -94,12 +98,12 @@ def get_subreddit_threads():
continue # # see https://github.com/JasonLovesDoggo/RedditVideoMakerBot/issues/78
if not top_level_comment . stickied :
if len ( top_level_comment . body ) < = int ( getenv ( " MAX_COMMENT_LENGTH " , " 500 " ) ) :
if not top_level_comment . author is None :
if top_level_comment . author is not None : # if errors occur with this change to if not.
content [ " comments " ] . append (
{
" comment_body " : top_level_comment . body ,
" comment_url " : top_level_comment . permalink ,
" comment_id " : top_level_comment . id ,
" comment_id " : top_level_comment . id
}
)
print_substep ( " Received subreddit threads Successfully. " , style = " bold green " )