From 8e6c1549dfa93533258cd3727f317bc6915520c1 Mon Sep 17 00:00:00 2001 From: Nathan Windisch Date: Wed, 24 Jul 2024 22:41:11 +0100 Subject: [PATCH] Created RedditPage#parse_comments, which parses comment data from a Reddit .json link. --- src/reddit.py | 52 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/src/reddit.py b/src/reddit.py index e409897..16066ce 100644 --- a/src/reddit.py +++ b/src/reddit.py @@ -65,6 +65,7 @@ class RedditPage: except: None self.parse_posts() + self.parse_comments() return RedditPageData(posts=self.posts, comments=self.comments, after=self.after) @@ -120,21 +121,44 @@ class RedditPage: "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"], + "upvotes": item["ups"], + "downvotes": item["downs"], + "total_comments": item["num_comments"], + "total_awards": item["total_awards_received"], + + # Comment flags + "is_edited": item["edited"], + "is_archived": item["archived"], + "is_locked": item["locked"], + "is_quarantined": item["quarantine"], + "is_stickied": item["stickied"], + "is_send_replies": item["send_replies"], + + # Comment date + "published_at": item["created_utc"], + }) - "score": item["score"], - "upvotes": item["ups"], - "downvotes": item["downs"], - "total_comments": item["num_comments"], - "total_awards": item["total_awards_received"], - "is_edited": item["edited"], - "is_archived": item["archived"], - "is_locked": item["locked"], - "is_quarantined": item["quarantine"], - "is_stickied": item["stickied"], - "is_send_replies": item["send_replies"], - "published_at": item["created_utc"], - }) - return comments class Tools: