From 5c9e36de3122125ee2fa4e42802827a81cc8830b Mon Sep 17 00:00:00 2001 From: Nathan Windisch Date: Wed, 24 Jul 2024 22:40:10 +0100 Subject: [PATCH] Created RedditPage#parse_posts, which parses post data from a Reddit .json link. --- src/reddit.py | 52 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/src/reddit.py b/src/reddit.py index 5086510..e409897 100644 --- a/src/reddit.py +++ b/src/reddit.py @@ -64,6 +64,7 @@ class RedditPage: try: self.after = raw_data["data"]["after"] except: None + self.parse_posts() return RedditPageData(posts=self.posts, comments=self.comments, after=self.after) @@ -71,11 +72,54 @@ class RedditPage: if "data" in data and "children" in data["data"]: for item in data["data"]["children"]: self.children.append(item) + def parse_posts(self): + for item in self.children: + if item["kind"] != "t3": continue # skip non-post items + item = item["data"] + self.posts.append({ + # General information + "id": item["name"], + "title": item["title"], + "description": item["selftext"], + "link": item["url"], + + # Author & subreddit information + "author_username": item["author"], + "author_id": item["author_fullname"], + "subreddit_name": item["subreddit"], + "subreddit_id": item["subreddit_id"], + "subreddit_subscribers": item["subreddit_subscribers"], + + # Post information + "score": item["score"], + "upvotes": item["ups"], + "downvotes": item["downs"], + "upvote_ratio": item["upvote_ratio"], + "total_comments": item["num_comments"], + "total_crossposts": item["num_crossposts"], + "total_awards": item["total_awards_received"], + "domain": item["domain"], + "flair_text": item["link_flair_text"], + "media_embed": item["media_embed"], + + # Post flags + "is_pinned": item["pinned"], + "is_self": item["is_self"], + "is_video": item["is_video"], + "is_media_only": item["media_only"], + "is_over_18": item["over_18"], + "is_edited": item["edited"], + "is_hidden": item["hidden"], + "is_archived": item["archived"], + "is_locked": item["locked"], + "is_quarantined": item["quarantine"], + "is_spoiler": item["spoiler"], + "is_stickied": item["stickied"], + "is_send_replies": item["send_replies"], + + "created_at": item["created_utc"], + }) - "author_username": item["author"], - "author_id": item["author_fullname"], - "subreddit_name": item["subreddit"], - "subreddit_id": item["subreddit_id"], "score": item["score"], "upvotes": item["ups"],