Advertisement
Guest User

Untitled

a guest
Nov 10th, 2017
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.90 KB | None | 0 0
  1. from collections import Counter
  2. import cPickle as pickle
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. import praw
  6.  
  7.  
  8. def collect_data():
  9.     print("Connecting to Reddit...")
  10.     reddit = praw.Reddit(client_id='aaaaa',
  11.                          client_secret='bbbbb',
  12.                          password='ccccc',
  13.                          user_agent='ddddd',
  14.                          username='eeeee')
  15.     print("Connected.")
  16.     print("-----")
  17.  
  18.     submissions_counter = Counter()
  19.     comments_counter = Counter()
  20.  
  21.     counter = 0
  22.     for submission in reddit.subreddit('korea').submissions(start=1483228800,
  23.                                                             end=1510364058):
  24.         print(counter)
  25.         counter += 1
  26.         print(submission.title)
  27.         submissions_counter[submission.author] += 1
  28.  
  29.         submission.comments.replace_more(limit=30)
  30.         for comment in submission.comments.list():
  31.             comments_counter[comment.author] += 1
  32.  
  33.     pickle.dump(submissions_counter, open("submissions.p", "wb"))
  34.     pickle.dump(comments_counter, open("comments.p", "wb"))
  35.  
  36.  
  37. def load_data():
  38.     submissions_counter = pickle.load(open("submissions.p", "rb"))
  39.     comments_counter = pickle.load(open("comments.p", "rb"))
  40.     return submissions_counter, comments_counter
  41.  
  42.  
  43. def make_graph(names, comments):
  44.     pos = np.arange(500) * 20
  45.     plt.figure(figsize=(15, 200))
  46.     plt.barh(pos, comments, height=10, align='center')
  47.     plt.yticks(pos, names)
  48.     plt.xlabel("Number of comments")
  49.     plt.tight_layout()
  50.     plt.ylim([-20, 10000])
  51.     plt.gca().invert_yaxis()
  52.     for i, v in enumerate(comments):
  53.         plt.text(v + 5, i * 20 + 2, str(v),
  54.                  color='blue', fontweight='bold')
  55.     plt.savefig('submission.png')
  56.  
  57. s, c = load_data()
  58. c_most = c.most_common(500)
  59. a = {x: c[x] for x in c if c[x] >= 30}
  60. i = 0
  61. for each in a:
  62.     i += 1
  63. print(i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement