SHARE
TWEET

FMABot Stats gatherer

mvollingjr Dec 13th, 2016 (edited) 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import praw
  2. import yaml
  3. import re
  4. import copy
  5. import string
  6.  
  7. from praw.models import MoreComments
  8.  
  9. class commentor:
  10.     username = ''
  11.     points = 0
  12.     words = 0
  13.     comments = 0
  14.     top_comments = 0
  15.     reply_comments = 0
  16.     replies = 0
  17.     threads = 0
  18.     holy_shits = 0
  19.     def __init__(self, user):
  20.         self.username = user
  21.     def add_comment(self):
  22.         self.comments += 1
  23.     def add_top_comment(self):
  24.         self.top_comments += 1
  25.     def add_reply(self):
  26.         self.reply_comments += 1
  27.     def add_karma(self, karma):
  28.         self.points += karma
  29.     def add_replies(self, replies):
  30.         self.replies += replies
  31.     def add_words(self, words):
  32.         self.words += words
  33.     def add_thread(self):
  34.         self.threads += 1
  35.     def add_holy_shits(self, num):
  36.         self.holy_shits += num
  37.  
  38. class ranked:
  39.     desc = ''
  40.     max_score = 0
  41.     second_score = 0
  42.     max_data = None
  43.     second_data = None
  44.     def __init__(self, desc):
  45.         self.desc = desc
  46.     def add(self, data, score):
  47.         if score > self.max_score:
  48.             self.second_score = self.max_score
  49.             self.second_data = self.max_data
  50.             self.max_score = score
  51.             self.max_data = data
  52.         elif score > self.second_score:
  53.             self.second_score = score
  54.             self.second_data = data
  55.     def __str__(self):
  56.         ret  = self.desc + ':\n'
  57.         ret += '  Winner: ' + self.max_data + '\n'
  58.         ret += '  Score: ' + str(self.max_score) + '\n'
  59.         ret += '  Runner up: ' + self.second_data + '\n'
  60.         ret += '  Score: ' + str(self.second_score)
  61.         return ret
  62.    
  63.            
  64. class word:
  65.     count = 0
  66.     def __init__(self, word):
  67.         self.word = word
  68.     def increment(self):
  69.         self.count += 1
  70.  
  71. # Load login information from config.yml
  72. infile = open('config.yml', 'r')
  73. config = yaml.load(infile)
  74. print("Config info loaded")
  75.  
  76. # Login to reddit
  77. reddit = praw.Reddit(client_id=config['id'],
  78.                      client_secret=config['secret'],
  79.                      username=config['username'],
  80.                      password=config['password'],
  81.                      user_agent=config['agent'])
  82.  
  83. # verify login information
  84. print('Logged in as', reddit.user.me())
  85.  
  86. # Get index post
  87. index_submission = reddit.submission(id='559rhx')
  88. print('Index Title:', index_submission.title)
  89.  
  90. # Filter index for episode discussion links
  91. refilter = '\[\*\*Episode [0-9]+\*\*\]\(https:\/\/redd.it\/(?P<id>[a-zA-Z0-9]+)\)'
  92. submission_links = re.findall(refilter, index_submission.selftext)
  93.  
  94. print('Found', len(submission_links), 'links')
  95.  
  96. #dictionary of commentors
  97. users = dict()
  98. words = dict()
  99.  
  100. #sort submission_links
  101. submission_links.sort()
  102.  
  103. #statistics
  104. deleted_comments = 0
  105. total_holy_shits = 0
  106. most_comments = ranked('Most Comments')
  107. most_commentors = ranked('Most Commentors in a thread')
  108. most_karma = ranked('Most Karma gained by a user')
  109. most_comments_from_user = ranked('User with the most comments')
  110. most_replied_to = ranked('Person with most replies')
  111. most_replies = ranked('Person who replied the most')
  112. most_top_level_comments = ranked('Person with most top comments')
  113. most_used_word = ranked('Most used word')
  114. most_holy_shits_comment = ranked('Comment with most holy shits')
  115. most_holy_shits_post = ranked('Post with most holy shits')
  116. most_words_comment = ranked('Comment with the most words')
  117. most_words_post = ranked('Post with the most words')
  118. most_words_user = ranked('User with the most words')
  119. holy_shit_list = list()
  120. full_metal_count = 0
  121.  
  122. #translate key
  123. trans_key = str.maketrans({key: None for key in string.punctuation})
  124.  
  125. # only run on first discussion thread for testing
  126. for submission_id in submission_links:
  127.     # print submission info
  128.     print(submission_id + ':')
  129.     sub = reddit.submission(id=submission_id)
  130.     print(' Title :', sub.title)
  131.     print(' Date  :', sub.created_utc)
  132.     print(' Author:', sub.author.name)
  133.     print(' Number of comments:', sub.num_comments)
  134.     # Expand 'More comments' sections
  135.     sub.comments.replace_more(limit=0)
  136.    
  137.     # Submission stats
  138.     commentors = list()
  139.     post_holy_shits = 0
  140.     post_words = 0
  141.  
  142.     # Gather comment stats
  143.     for comment in sub.comments.list():
  144.         if comment.author is None:
  145.             print('  Found deleted comment')
  146.             deleted_comments += 1
  147.         else:
  148.             # Gather username stats
  149.             name = comment.author.name
  150.             print('  Found comment by', name)
  151.             if (name not in users):
  152.                 users[name] = commentor(name)
  153.             if (name not in commentors):
  154.                 commentors.append(name)
  155.                 users[name].add_thread()
  156.                
  157.             # Gather word stats
  158.             body_copy = comment.body.lower()
  159.             body_copy.translate(trans_key) #remove punctuation to count words
  160.             comment_words = comment.body.split(' ')
  161.             post_words += len(comment_words)
  162.             for comment_word in comment_words:
  163.                 if comment_word not in words:
  164.                     words[comment_word] = word(comment_word)
  165.                 words[comment_word].increment()
  166.             full_metals = re.findall('full metal', body_copy)
  167.             full_metal_count += len(full_metals)
  168.            
  169.             holy_shits = len(re.findall('holy shit', body_copy))
  170.             total_holy_shits += holy_shits
  171.             post_holy_shits += holy_shits
  172.            
  173.            
  174.             # Gather user stats
  175.             users[name].add_comment()
  176.             users[name].add_karma(comment.score)
  177.             users[name].add_replies(len(comment.replies))
  178.             users[name].add_words(len(comment_words))
  179.             users[name].add_holy_shits(holy_shits)
  180.             if comment.parent_id[1] is '3':
  181.                 users[name].add_top_comment()
  182.             else:
  183.                 users[name].add_reply()
  184.            
  185.             # Gather comment stats
  186.             url = 'https://www.reddit.com/r/anime/comments/' + sub.id + '//' + comment.id
  187.             most_holy_shits_comment.add(url, holy_shits)
  188.             most_words_comment.add(url, len(comment_words))
  189.    
  190.     # Gather post stats
  191.     most_comments.add(sub.title, sub.num_comments)
  192.     most_commentors.add(sub.title, len(commentors))
  193.     most_words_post.add(sub.title, post_words)
  194.     most_holy_shits_post.add(sub.title, post_holy_shits)
  195.     holy_shit_list.append((sub.title, post_holy_shits))
  196.  
  197.  
  198. for key in users:
  199.     user = users[key]
  200.     print(user.username + ':')
  201.     print(' Comments:', user.comments)
  202.     print(' Top Comments:', user.top_comments)
  203.     print(' Reply Comments:', user.reply_comments)
  204.     print(' Karma:', user.points)
  205.     print(' Words:', user.words)
  206.     print(' Times Replied To:', user.replies)
  207.     print(' Threads:', user.threads)
  208.     # Run user statistics
  209.     most_karma.add(user.username, user.points)
  210.     most_comments_from_user.add(user.username, user.comments)
  211.     most_replies.add(user.username, user.reply_comments)
  212.     most_replied_to.add(user.username, user.replies)
  213.     most_top_level_comments.add(user.username, user.top_comments)
  214.     most_words_user.add(user.username, user.words)
  215.    
  216. # Run Word Stats
  217. for key in words:
  218.     most_used_word.add(key, words[key].count)
  219.  
  220. # Print statistics
  221. print('Found Statistics:')
  222. print('Deleted Comments:', deleted_comments)
  223. print(most_comments)
  224. print(most_commentors)
  225. print(most_karma)
  226. print(most_comments_from_user)
  227. print(most_words_user)
  228. print(most_replied_to)
  229. print(most_top_level_comments)
  230. print(most_replies)
  231. print(most_used_word)
  232. print(most_words_comment)
  233. print(most_words_post)
  234. print(most_holy_shits_comment)
  235. print(most_holy_shits_post)
  236. print('Holy shits by post:')
  237. for item in holy_shit_list:
  238.     print('  ' + item[0] + ':', item[1])
  239. print('Holy shit mentions:', total_holy_shits)
  240. print('Hype mentions:', words['hype'].count)
  241. print(' ')
  242. print('Person who wasted the most time gathering useless stats: /u/mvolling')
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top