-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
226 lines (198 loc) · 7.42 KB
/
app.py
File metadata and controls
226 lines (198 loc) · 7.42 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import string
from wordcloud import WordCloud
import matplotlib.pyplot as plt, mpld3
from flask import Flask, send_file, json, render_template
import praw
from wordOps import countWords, punctRm, excludeWordsList, totalKarmaOfWords, contributorsToSubreddit
from config import RedditConfig
# Send "public/any.name" when route "<site>.com/{any.name}" is hit
app = Flask(__name__, static_url_path="", static_folder="static", template_folder='templates')
import errors
# Initialize PRAW (reddit wrapper) from config.py
# DO NOT push config.py to github, we do not want to
# make the API keys public.
# The format of config.py is:
#
# class RedditConfig:
# id = '<client_id>'
# secret = '<client_secret>'
# userAgent = '<user_agent>'
reddit = praw.Reddit(client_id=RedditConfig.id,
client_secret=RedditConfig.secret,
user_agent=RedditConfig.userAgent)
# Remap '/' to index. Other files can be served statically.
@app.route('/')
def index():
return render_template('index.html', chart="""
Welcome to Seeing Redd.
Please use the search bar above to see more.
""")
def newPosts(searchbase):
return searchbase.new(limit=100)
def hotPosts(searchbase):
return searchbase.hot(limit=100)
def topPostsAllTime(searchbase):
return searchbase.top(time_filter='all', limit=100)
def topPostsPast24Hours(searchbase):
return searchbase.top(time_filter='day', limit=100)
def controversialPostsAllTime(searchbase):
return searchbase.controversial(time_filter='all', limit=100)
def controversialPast24Hours(searchbase):
return searchbase.controversial(time_filter='day', limit=100)
switch = {"new": lambda x: newPosts(x),
"hot": lambda x: hotPosts(x),
"topalltime": lambda x: topPostsAllTime(x),
"top24hrs": lambda x: topPostsPast24Hours(x),
"controversialall": lambda x: controversialPostsAllTime(x),
"controversial24hrs": lambda x: controversialPast24Hours(x),
}
@app.route('/r/<sr>/<category>')
def wordCountSubreddit(sr, category):
funct = switch.get(category)
subreddit = reddit.subreddit(sr)
submissions = funct(subreddit)
posts = list(map(lambda x: x.selftext + " " + x.title, submissions))
sortedWords = countWords(posts, punctRm, excludeWordsList)
sortedWords = sortedWords[:50]
labels = list()
values = list()
for word in sortedWords:
labels.append(word[0])
values.append(word[1])
contributors = contributorsToSubreddit(funct, subreddit)
contList = ""
for c in contributors:
contList+=str(c.name) + ", \n"
#totalKarma = totalKarmaOfWords(sortedWords, funct, subreddit)
#words = list()
#karma = list()
#for k in totalKarma:
# words.append(k[0])
# karma.append(k[1])
fig = plt.figure(figsize=(10,20))
plt.rcParams.update({'font.size': 20})
if sortedWords:
# Generate Chart
plt.subplot(2, 1, 2)
plt.bar(range(len(labels)), values, tick_label=labels)
ax1 = fig.add_subplot(212)
fig.subplots_adjust(top=0.85)
ax1.set_xlabel('Word')
y_rotate=ax1.set_ylabel('Instances')
y_rotate.set_rotation(0)
ax1.set_title('/r/' + str(subreddit))
fig.tight_layout()
# Generate Word Cloud
plt.subplot(2, 1, 1)
text = str(sortedWords)
text = text.replace("'", "")
wordcloud = WordCloud(width=1000, height=1000, margin=0).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.margins(x=0, y=0)
plt.xticks([])
plt.yticks([])
# Generate Total Karma
#plt.subplot(4, 1, 3)
#plt.bar(range(len(words)), karma, tick_label=words)
#ax3 = fig.add_subplot(413)
#fig.subplots_adjust(top=0.85)
#ax3.set_xlabel('Word')
#y=ax3.set_ylabel('Karma')
#y.set_rotation(0)
#ax3.set_title('Total Word Karma across Posts')
# Generate Contributors
#plt.subplot(3, 1, 3)
#ax2 = fig.add_subplot(313)
#ax2.set_title('Contributors')
#lt.text(0.5, 0.5, contList, horizontalalignment='center', verticalalignment='center')
#fig.tight_layout()
else:
plt.text(0.5,0.5,'There are no posts for the selected search.\nDid you mean to search for /r?', horizontalalignment='center', verticalalignment='center')
#top post by subreddit
result=mpld3.fig_to_html(fig)
result+="<div style='padding:20px; width: 100%; overflow: hidden;' class='comment' >"
result+="<div style='width:75%; float: left;'> <h3> Top Post in: r/" +str(subreddit)
result+= " With a score of: " + str(next(funct(subreddit)).score) + "</h3> <p> <h3> User: u/" + str(next(funct(subreddit)).author) + "</h3>"
result+= "<h3> Title: </h3> <p>'"+ next(funct(subreddit)).title+ "'</p> </div>"
#result+= "<p><a href=\"https://www.reddit.com" + next(funct(subreddit)).permalink + "\"></a></p>"
result+= "</div>"
#contributors to subreddit
result+="<div style='padding:20px; width: 100%; overflow: auto; border: 3px solid #141414;' class='comment'>"
result+="<h3>Contributors:</h3>"
result+= contList
result+= "</div>"
return render_template('index.html', chart=result)
#word popularity by user
@app.route('/u/<user>/<category>')
def wordCountUser(user, category):
user = reddit.redditor(name=user)
comments = user.comments
submissions = user.submissions
funct = switch.get(category)
commentWords = funct(comments)
submissionWords = funct(submissions)
usersText = list()
subredditStrings = list()
for comment in commentWords:
usersText.append(comment.body)
subredditStrings.append(str(comment.subreddit))
for sub in submissionWords:
usersText.append(sub.selftext)
subredditStrings.append(str(sub.subreddit))
sortedWords = countWords(usersText, punctRm, excludeWordsList)
sortedWords = sortedWords[:50]
labels = list()
values = list()
for word in sortedWords:
labels.append(word[0])
values.append(word[1])
sortedSubreddits = countWords(subredditStrings, "~", [""])
srLabels = list()
srValues = list()
for word in sortedSubreddits:
srLabels.append(word[0])
srValues.append(word[1])
fig = plt.figure(figsize=(10,20))
plt.rcParams.update({'font.size': 20})
if sortedWords:
# Generate Chart
plt.subplot(3, 1, 2)
plt.bar(range(len(labels)), values, tick_label=labels)
ax1 = fig.add_subplot(312)
fig.subplots_adjust(top=0.85)
ax1.set_xlabel('Word')
y_rotate=ax1.set_ylabel('Instances')
y_rotate.set_rotation(0)
ax1.set_title('/u/' + str(user))
fig.tight_layout()
# Generate Word Cloud
plt.subplot(3, 1, 1)
text = str(sortedWords)
text = text.replace("'", "")
wordcloud = WordCloud(width=1000, height=1000, margin=0).generate(text)
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.margins(x=0, y=0)
plt.xticks([])
plt.yticks([])
#Generate plot of subreddits user is active in.
plt.subplot(3, 1, 3)
plt.bar(range(len(srLabels)), srValues, tick_label=srLabels)
ax2 = fig.add_subplot(313)
#fig.subplots_adjust(top=0.85)
ax2.set_xlabel('Subreddits')
y_rotate=ax2.set_ylabel('Posts')
y_rotate.set_rotation(0)
ax2.set_title('Posts per Subreddit')
fig.tight_layout()
else:
plt.text(0.5,0.5,'There are no posts for the selected search.\nDid you mean to search for /r?', horizontalalignment='center', verticalalignment='center')
#top post by user
result=mpld3.fig_to_html(fig)
result+="<div style='padding:20px; width: 100%; overflow: hidden;' class='comment'>"
result+= "<div style='width:100%; float: left;'> <h3>Best Comment of: u/" +str(user) +"</h3>"
result+= "<h3> Found in: r/"+ str(comment.subreddit) +" </h3> <p> '" + next(funct(user.comments)).body +"' </p> </div>"
result+= "<p><a href=\"https://www.reddit.com" + comment.permalink + "\">Link</a></p>"
result+= "</div>"
return render_template('index.html', chart=result)