-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
73 lines (62 loc) · 2.32 KB
/
Parser.py
File metadata and controls
73 lines (62 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
from Authorizer import Authorizer
import matplotlib.pyplot as plt
import numpy as np
"""
Parses data from JSON returned from Authorizer
Creates frequency list for each subreddit
Plots the top 10 subreddit according to user input
"""
class Parser:
"""
Creates dictionary of frequency for each subreddit based off JSON data
Keyword arguments:
limit -- number of posts (default 100)
subreddit -- type of subreddit (default '/r/popular')
t -- time range (default 'day')
"""
def __init__(self, limit=100, subreddit='all', t='day'):
authorizer = Authorizer(limit, subreddit=subreddit, t=t)
self.subreddit = subreddit
self.time = t
request_JSON = authorizer.getJSON()
sub_count = {}
for text in request_JSON:
children = text['data']['children']
for child in children:
child_subreddit = child['data']['subreddit']
if child_subreddit in sub_count:
sub_count[child_subreddit] += 1
else:
sub_count[child_subreddit] = 1
sorted_dictionary = sorted((value, key) for (key, value) in sub_count.items())[::-1]
self.top_subreddits = []
self.counts = []
for key in sorted_dictionary:
self.top_subreddits.append(key[1])
self.counts.append(key[0])
"""Plots the frequency of top 10 subreddits using Matplotlib"""
def plot(self):
subreddits = self.top_subreddits[:10]
y_pos = np.arange(len(subreddits))
number_of_occurrence = self.counts[:10]
for n in range(len(subreddits)):
if len(subreddits[n]) > 10:
subreddits[n] = subreddits[n][:10] + '...'
plt.bar(y_pos, number_of_occurrence, align='center', alpha=0.5)
plt.xticks(y_pos, subreddits, fontsize=8)
ax = plt.gca()
pad = 5
for tick in ax.xaxis.get_major_ticks()[0:]:
tick.set_pad(pad)
if pad < 20:
pad += 10
elif pad > 20:
pad = 5
plt.ylabel('Count')
plt.title('Top 10 Subreddits of '+self.time+' in /r/'+self.subreddit)
plt.tight_layout()
plt.savefig('app/static/graph.png')
plt.gcf().clear()
if __name__ == '__main__':
parser = Parser()
parser.plot()