-
Notifications
You must be signed in to change notification settings - Fork 15
/
reddit-analyzer.py
190 lines (154 loc) · 5.13 KB
/
reddit-analyzer.py
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
import sys
import requests
from ascii_graph import Pyasciigraph
from datetime import datetime
from collections import Counter
import time
if len(sys.argv) == 2:
username = str(sys.argv[1])
else:
print('-----')
print('usage: reddit-analyzer.py USERNAME')
print('-----')
sys.exit()
# initialize variables
# username = 'spez'
lastaction = 0
headers = {'User-Agent': 'testbot'}
curts = int(time.time())
commentdata = []
linkdata = []
timelist = []
hourseconds = 3600
houroffset = -7
offset = hourseconds*houroffset
# let people know that it's working
print(' --- fetching data for user: '+username+' ---')
print(' ')
# fetch profile data
r3 = requests.get('https://www.reddit.com/user/'+username+'/about.json', headers=headers)
userdata = r3.json()['data']
# fetch comments
while True:
comurl = 'https://api.pushshift.io/reddit/search/comment/?author='+username+'&size=500&before='+str(curts)
r1 = requests.get(comurl, headers=headers)
tempdata = r1.json()['data']
commentdata += tempdata
try:
if tempdata[499]:
curts = tempdata[499]['created_utc']
except: break
# re-establish current time
curts = int(time.time())
# fetch posts/submissions
while True:
linkurl = 'https://api.pushshift.io/reddit/search/submission/?author='+username+'&size=500&before='+str(curts)
r2 = requests.get(linkurl, headers=headers)
postdata = r2.json()['data']
linkdata += postdata
try:
if postdata[499]:
curts = postdata[499]['created_utc']
except: break
# set last active time
lastcomment = commentdata[0]['created_utc']
lastpost = postdata[0]['created_utc']
if lastcomment > lastpost:
lastaction = lastcomment
else: lastaction = lastpost
# add all subreddits to a list
# add all timed activities to a list
subList = []
for x in commentdata:
subList.append(x['subreddit'].lower())
timelist.append(x['created_utc'])
for x in postdata:
subList.append(x['subreddit'].lower())
timelist.append(x['created_utc'])
# adjust time for offset
timelist = [x + offset for x in timelist]
# and create a set for comparison purposes
sublistset = set(subList)
# load subreddits from file and check them against comments
locList = [line.rstrip('\n').lower() for line in open('all-locations.txt')]
loclistset = set(locList)
def getProfile():
print('[+] username : '+str(userdata['name']))
print('[+] creation date : '+str(datetime.fromtimestamp(userdata['created_utc'])))
print('[+] last action : '+str(datetime.fromtimestamp(lastaction)))
print('[+] verified email : '+str(userdata['has_verified_email']))
print('---')
print('[+] total comments : '+str(len(commentdata)))
print('[+] comment karma : '+str(userdata['comment_karma']))
print('---')
print('[+] total links : '+str(len(linkdata)))
print('[+] link karma : '+str(userdata['link_karma']))
print('---')
print('[+] location based reddit(s): '+ str(sublistset.intersection(loclistset)))
def getComments():
# draw and print ascii graph
counter = Counter(subList)
gdata = counter.most_common()
graph = Pyasciigraph(
separator_length=4,
multivalue=False,
human_readable='si',
)
for line in graph.graph('Comment Activity', gdata):
print(line)
def timeGraph(timelist):
newtl = [] # hour list
wdlist = [] # weekday list
# fill newtl with HOURs
for x in timelist:
newtl.append(datetime.fromtimestamp(int(x)).hour)
# create hour name list
hournames = '00:00 01:00 02:00 03:00 04:00 05:00 06:00 07:00 08:00 09:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 17:00 18:00 19:00 20:00 21:00 22:00 23:00'.split()
# deal with HOUR counting
tgCounter = Counter(newtl)
tgdata = tgCounter.most_common()
# sort by HOUR not popularity
tgdata = sorted(tgdata)
d = []
e = 0
for g in hournames:
d.append(tuple([g, tgdata[e][1]]))
e+=1
tgdata = d
# draw HOUR graph
graph = Pyasciigraph(
separator_length=4,
multivalue=False,
human_readable='si',
)
for line in graph.graph('Time Activity', tgdata):
print(line)
print(' ')
# estabish weekday list (0 is Monday in Python-land)
weekdays = 'Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split()
for x in timelist:
wdlist.append(datetime.fromtimestamp(int(x)).weekday())
wdCounter = Counter(wdlist)
wddata = wdCounter.most_common()
wddata = sorted(wddata)
# change tuple weekday numbers to weekday names
y = []
c = 0
for z in weekdays:
y.append(tuple([z, wddata[c][1]]))
c+=1
wddata = y
# draw WEEKDAY graph
graph = Pyasciigraph(
separator_length=4,
multivalue=False,
human_readable='si',
)
for line in graph.graph('Day of the Week Activity', wddata):
print(line)
### PRINT INFO ###
getProfile()
print(' ')
getComments()
print(' ')
timeGraph(timelist)