Created RedditPage#parse_comments, which parses comment data from a Reddit .json link.

This commit is contained in:
Nathan Windisch 2024-07-24 22:41:11 +01:00
parent 5c9e36de31
commit 8e6c1549df

View File

@ -65,6 +65,7 @@ class RedditPage:
except: None except: None
self.parse_posts() self.parse_posts()
self.parse_comments()
return RedditPageData(posts=self.posts, comments=self.comments, after=self.after) return RedditPageData(posts=self.posts, comments=self.comments, after=self.after)
@ -120,21 +121,44 @@ class RedditPage:
"created_at": item["created_utc"], "created_at": item["created_utc"],
}) })
def parse_comments(self):
for item in self.children:
if item["kind"] != "t1": continue
item = item["data"]
self.comments.append({
# General information
"id": item["name"],
"body": item["body"],
"link": item["permalink"],
"post_id": item["link_id"],
"post_title": item["link_title"],
"post_link": item["link_permalink"],
# Author & subreddit information
"author_username": item["author"],
"author_id": item["author_fullname"],
"subreddit_name": item["subreddit"],
"subreddit_id": item["subreddit_id"],
# Comment information
"score": item["score"], "score": item["score"],
"upvotes": item["ups"], "upvotes": item["ups"],
"downvotes": item["downs"], "downvotes": item["downs"],
"total_comments": item["num_comments"], "total_comments": item["num_comments"],
"total_awards": item["total_awards_received"], "total_awards": item["total_awards_received"],
# Comment flags
"is_edited": item["edited"], "is_edited": item["edited"],
"is_archived": item["archived"], "is_archived": item["archived"],
"is_locked": item["locked"], "is_locked": item["locked"],
"is_quarantined": item["quarantine"], "is_quarantined": item["quarantine"],
"is_stickied": item["stickied"], "is_stickied": item["stickied"],
"is_send_replies": item["send_replies"], "is_send_replies": item["send_replies"],
# Comment date
"published_at": item["created_utc"], "published_at": item["created_utc"],
}) })
return comments
class Tools: class Tools: