|
import os |
|
from datetime import datetime |
|
from typing import Any, Dict, List |
|
|
|
import praw |
|
|
|
from utilities.my_logger import setup_logger |
|
|
|
|
|
logger = setup_logger(__name__) |
|
|
|
|
|
def get_reddit_instance() -> praw.Reddit: |
|
"""Initialize and return a Reddit instance using PRAW.""" |
|
return praw.Reddit( |
|
client_id=os.getenv('REDDIT_CLIENT_ID'), |
|
client_secret=os.getenv('REDDIT_CLIENT_SECRET'), |
|
user_agent=os.getenv('REDDIT_USER_AGENT'), |
|
ratelimit_seconds=20, |
|
) |
|
|
|
|
|
def extract_submission_data(submission: praw.models.Submission) -> Dict[str, Any]: |
|
"""Extract and return relevant data from a given Reddit submission.""" |
|
return { |
|
"content": submission.selftext, |
|
"poster": str(submission.author), |
|
"date_utc": datetime.utcfromtimestamp(submission.created_utc).strftime('%Y-%m-%d %H:%M:%S'), |
|
"flair": submission.link_flair_text, |
|
"title": submission.title, |
|
"score": submission.ups, |
|
"permalink": submission.permalink, |
|
} |
|
|
|
|
|
def praw_downloader() -> List[Dict[str, str]]: |
|
"""Main function to extract and save all submissions from the subreddit.""" |
|
reddit = get_reddit_instance() |
|
subreddit = reddit.subreddit('bestofredditorupdates') |
|
|
|
logger.info('Starting to fetch submissions from bestofredditorupdates.') |
|
|
|
submissions = [] |
|
for submission in subreddit.new(limit=200): |
|
|
|
data = extract_submission_data(submission) |
|
submissions.append(data) |
|
|
|
logger.info(f'Finished downloading {len(submissions)} submissions.') |
|
return submissions |
|
|
|
|
|
if __name__ == "__main__": |
|
praw_downloader() |
|
|