Guest User

get-subreddit.py

a guest
May 10th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.85 KB | None | 0 0
  1. import praw
  2.  
  3. import argparse
  4.  
  5. import json
  6.  
  7. class GetSubreddit():
  8.     user_agent = "gnu/linux:get-subreddit:v1.0 (by /u/A_Truth_Value)"
  9.     def main(self):
  10.         parser = argparse.ArgumentParser()
  11.         parser.add_argument("subreddit", help="The subreddit to grab.")
  12.         parser.add_argument("-o", "--output", help="Directory to write JSON "
  13.                             "to.")
  14.         arguments = parser.parse_args()
  15.         if arguments.subreddit:
  16.             rconnection = praw.Reddit(self.user_agent)
  17.             submissions = rconnection.get_subreddit(arguments.subreddit).get_hot(
  18.                 limit=None)
  19.             posts = []
  20.             for value in submissions:
  21.                 posts.append(value)
  22.             description = ("The JSON data format I've chosen is slim but"
  23.                            "represents the essential attributes of posts"
  24.                            "and submissions. For submissions it is as follows:"
  25.                            "\n\n"
  26.                            "id: The unique id of the submission."
  27.                            "title: The title of the submission.\n"
  28.                            "body: For text posts, the body of the submission."
  29.                            "\n"
  30.                            "time_created: The time of creation.\n"
  31.                            "submitted_by: The username of the submitter.\n"
  32.                            "score: The score of the submission.\n"
  33.                            "url: The content-link of the submission.\n"
  34.                            "permalink: The original reddit link of the "
  35.                            "submission\n"
  36.                            "root_replies: A dictionary containing the top "
  37.                            "level replies for this submission. The keys of the"
  38.                            " dictionary are the unique id of each post.\n"
  39.                            "For comments a *tree* is constructed starting with"
  40.                            " a root post and then the child posts of that and"
  41.                            " their child posts recursively until a complete"
  42.                            " comment tree is constructed. Attributes are:\n\n"
  43.                            "id: The unique id of the comment."
  44.                            "body: The body of the comment."
  45.                            "author: The user thet wrote the comment."
  46.                            "time_posted: The time that the comment was posted."
  47.                            "score: The comments score."
  48.                            "replies: A dictionary of replies. Null if none.")
  49.             submission_dicts = {'description':description}
  50.             for submission in posts:
  51.                 submission_dict = {}
  52.                 submission_dict["title"] = submission.title
  53.                 submission_dict["body"] = submission.title
  54.                 submission_dict["time_created"] = submission.created_utc
  55.                 submission_dict["submitted_by"] = submission.author.name
  56.                 submission_dict["score"] = submission.score
  57.                 submission_dict["url"] = submission.url
  58.                 submission_dict["permalink"] = submission.permalink
  59.                 submission_dict["root_replies"] = self.create_tree(submission.
  60.                                                                    comments)
  61.                 submission_dicts[submission.id] = submission_dict
  62.             outfile = open(arguments.output, 'w')
  63.             json.dump(submission_dicts, outfile)
  64.         else:
  65.             raise ValueError("You didn't specify a subreddit to grab.")
  66.  
  67.     def create_tree(self, replies):
  68.         tree_nodes = {}
  69.         for reply in replies:
  70.             if isinstance(reply, praw.objects.MoreComments):
  71.                 comments = reply.comments()
  72.                 reply = comments[0]
  73.                 replies.append(comments[1:])
  74.             elif isinstance(reply, list):
  75.                 if reply:
  76.                     raise ValueError("THE LIST IS REAL!")
  77.                 else:
  78.                     print("Turns out it was junk.")
  79.                     return None
  80.             tree_node = {}
  81.             try:
  82.                 tree_node["body"] = reply.body
  83.             except:
  84.                 raise ValueError("Assumption made about data was wrong. O_o")
  85.             if reply.author:
  86.                 tree_node["author"] = reply.author.name
  87.             else:
  88.                 tree_node["author"] = reply.author
  89.             tree_node["created_utc"] = reply.created_utc
  90.             tree_node["score"] = reply.score
  91.             if reply.replies:
  92.                 tree_node["replies"] = self.create_tree(reply.replies)
  93.             else:
  94.                 tree_node["replies"] = None
  95.             tree_nodes[reply.id] = tree_node
  96.         if not replies:
  97.             return None
  98.         print(tree_nodes)
  99.         return tree_nodes
  100.  
  101. getsub = GetSubreddit()
  102. getsub.main()
Advertisement
Add Comment
Please, Sign In to add comment