Created RedditPage#parse_posts, which parses post data from a Reddit .json link.

This commit is contained in:
Nathan Windisch 2024-07-24 22:40:10 +01:00
parent 8ccd04fca2
commit 5c9e36de31

View File

@ -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"],