I have the following code: import praw Define a custom object to store comment details…
- User
I have the following code:
import praw
Define a custom object to store comment details
class RedditComment: def init(self, body: str, upvotes: int, downvotes: int, replies: 'RedditComment'): self.body = body self.upvotes = upvotes self.downvotes = downvotes self.replies = replies
Define a custom object to store Reddit post details
class RedditPost: def init(self, post: praw.Reddit.submission): self.post = post self.title = str(post.title) self.body = str(post.selftext) self.upvotes = int(post.score) self.comments = []
code# Function to retrieve comments and their replies to store in comments variable def get_comments(self, comment_list=None): if comment_list is None: comment_list = self.post.comments self.comments = [] for comment in comment_list: if isinstance(comment, praw.models.MoreComments): continue replies = self.get_comments(comment.replies) if comment.replies else [] self.comments.append(RedditComment( body=comment.body, upvotes=comment.ups, downvotes=comment.downs, replies=replies ))Authenticate with Reddit using PRAW
reddit = praw.Reddit( client_id='w', client_secret='xZwlg', user_agent='w77' )
Replace 'reddit_post_id' with the actual ID of the post you want to process
reddit_post_id = '194cyij'
submission = reddit.submission(id=reddit_post_id)
You can't actually get the number of downvotes directly through the Reddit API.
You would need to calculate it. Here we only fetch the upvotes (score) and the title and body of the post.
post = RedditPost(reddit.submission(id=reddit_post_id))
Now you have a custom RedditPost object with all the information you need.
print(post.title) print(post.upvotes) print() post.get_comments() for comment in post.comments: print(comment.body)
What I currently have is an attempt to make classes of data accessible to me in python. However, I realized it would be much easier to simply take a postid and export all its content to a json file containing title, body, and all replies in proper sequence. How would you go about doing such a thing in python?
Assistant
- User
Sorry, you misunderstood. I want you to scrap my code and show me an entirely new piece in python that takes a post id, gets said data, and saves it to a postid.json
Assistant
Sure, here's an example of how you could do that in Python using the
requestslibrary to make an API request to Reddit's API:codeimport requests import json def get_reddit_post_data(post_id): url = f"https://api.reddit.com/posts/{post_id}.json" response = requests.get(url) data = response.json() return data def save_data_to_file(data, file_name): with open(file_name, "w") as f: json.dump(data, f) post_id = "194cyij" data = get_reddit_post_data(post_id) save_data_to_file(data, f"{post_id}.json")This code will retrieve the data for the specified post ID from Reddit's API, and then save it to a JSON file with the same name as the post ID.
You'll need to replace
post_idwith the actual ID of the post you want to retrieve data for.Also, you'll need to make sure that you have the
requestslibrary installed, you can install it by running in your terminal.