-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget_data.py
219 lines (181 loc) · 7.13 KB
/
get_data.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
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
import praw
import pickle
import copy
class histogram:
def __init__(self, dictionary=None):
self.frequencies = {}
if dictionary is not None:
self.frequencies = copy.deepcopy(dictionary)
def get_sum(self):
the_sum = 0
for e in self.frequencies:
the_sum += self.frequencies[e]
return the_sum
def add_frequency(self, key, value):
if key in self.frequencies:
self.frequencies[key] += value
else:
self.frequencies[key] = value
def add_by_frequencies(self,frequencies):
for key in frequencies.frequencies:
self.add_frequency(key, frequencies.frequencies[key])
def multiply_frequency(self, key, value):
if key in self.frequencies:
self.frequencies[key] *= value
else:
self.frequencies[key] = 0.0
def multiply_by_frequencies(self, frequencies):
for key in frequencies.frequencies:
self.multiply_frequency(key, frequencies.frequencies[key])
def multiply_by_scalar(self, scalar):
for key in self.frequencies:
self.multiply_frequency(key,scalar)
def divide_frequency(self, key, value):
if key in self.frequencies:
if value != 0:
if self.frequencies[key] == 0:
self.frequencies[key] = 1.0
else:
self.frequencies[key] /= (0.0 + value)
else:
if self.frequencies[key] == 0:
self.frequencies[key] = 1.0
else:
self.frequencies[key] = float('inf')
else:
if value > 0:
self.frequencies[key] = 0.0
else:
self.frequencies[key] = 1.0
def divide_by_frequencies(self, frequencies):
for key in frequencies.frequencies:
self.divide_frequency(key, frequencies.frequencies[key])
class comment:
def __init__(self, comment):
if comment is not None and hasattr(comment,'author') and comment.author is not None and hasattr(comment.author, 'name'):
self.author_name = comment.author.name
else:
self.author_name = ''
self.subreddit = str(comment.subreddit.display_name.strip(' ').lower())
class user:
@staticmethod
def get_histogram(comments, author_name):
total_comments_by_author = 0
the_histogram = histogram()
for comment in comments:
if comment.author_name == author_name:
total_comments_by_author += 1
the_histogram.add_frequency(comment.subreddit, 1)
the_histogram.multiply_by_scalar(1.0 / total_comments_by_author)
#print author_name, " ", the_histogram.get_sum()
return the_histogram.frequencies
class community:
@staticmethod
def get_histogram(comments, subreddit_name):
total_comments_in_subreddit = 0
the_histogram = histogram()
for comment in comments:
if comment.subreddit == subreddit_name:
total_comments_in_subreddit += 1
the_histogram.add_frequency(comment.author_name, 1)
the_histogram.multiply_by_scalar(1.0 / total_comments_in_subreddit)
return the_histogram.frequencies
class data:
def __init__(self, comments, x_subs):
self.comments = comments
self.x_subs = x_subs
def remove_sub_data(subredditName):
the_data = pickle.load(open('data.pkl', 'rb'))
comments = the_data.comments
x_subs = the_data.x_subs
comments = [x for x in comments if x.subreddit.lower() != subredditName]
x_subs = [x for x in x_subs if x != subredditName]
the_data = data(comments, x_subs )
print x_subs
output = open('data.pkl', 'wb')
pickle.dump(the_data,output)
output.close()
def add_sub_data(subredditName, num_redditors):
user_agent = ("Testing Reddit Functionality by /u/Reddit_Projector https://github.com/joshlemer/RedditProject")
reddit = praw.Reddit(user_agent)
subreddit_object = reddit.get_subreddit(subredditName)
the_data = pickle.load(open('data.pkl', 'rb'))
comments = the_data.comments
x_subs = the_data.x_subs
y_comments = [comment(a) for a in subreddit_object.get_comments(limit=num_redditors)]
z_comments = []
redditors = []
i = 0
for y_com in y_comments:
print y_com.subreddit, " z = ", i
redditor = y_com.author_name
if redditor not in redditors:
try:
z_comments += [comment(a) for a in reddit.get_redditor(y_com.author_name).get_comments(limit=100)]
redditors.append(redditor)
except:
print "oops, that user is weird"
i += 1
comments += list(z_comments)
print "COMMENTS LENGTH: ", len(comments)
the_data = data(comments, x_subs + [subredditName] )
output = open('data.pkl', 'wb')
pickle.dump(the_data,output)
output.close()
if __name__ == "__main__":
user_agent = ("Testing Reddit Functionality by /u/Reddit_Projector https://github.com/joshlemer/RedditProject")
reddit = praw.Reddit(user_agent)
subredditName = 'all'
subreddit_object = reddit.get_subreddit(subredditName)
y = 5 #Comments per subreddit inspected
z = 100 #Comments per user inspected
#List of subreddits to be analyzed
# x_subs = [
# 'hiphopheads',
# 'metal',
# 'postrock',
# 'letstalkmusic' ]
#Commented code below is for pulling our x_subs from the most recent comments in /r/all
# x_comments = [comment(a) for a in subreddit_object.get_comments(limit=x)]
# i = 0
# for c in x_comments:
# print "x = ", i
# if c.subreddit not in x_subs:
# x_subs.append(c.subreddit)
# i += 1
#List of subreddits to be analyzed
x_subs = [
'hiphopheads',
'metal',
'postrock',
'letstalkmusic' ]
y_comments = []
i = 0
print "Getting ", y, " comments from each of the ", len(x_subs), " subreddits"
for x_sub in x_subs:
print "\tRetrieving ", 5, " comments from /r/", x_sub
subreddit_object = reddit.get_subreddit(x_sub)
y_comments += [comment(a) for a in subreddit_object.get_comments(limit=y)]
i += 1
z_comments = []
redditors = []
i = 0
print "Following commenters from original subs to gather their other reddit activity"
for y_com in y_comments:
redditor = y_com.author_name
print "\tAnalyzing user ", redditor, " (user ", i, "/", len(y_comments), ")"
if redditor not in redditors:
try:
z_comments += [comment(a) for a in reddit.get_redditor(y_com.author_name).get_comments(limit=z)]
redditors.append(redditor)
except:
print "\t\toops, that user is weird\n\t\tprobably deleted their comment or profile or something"
else:
print "\t\tAlready looked at this user, no need to make an other call."
i += 1
comments = list(z_comments)
print "COMMENTS LENGTH: ", len(comments)
the_data = data(comments, x_subs)
output = open('data.pkl', 'wb')
pickle.dump(the_data,output)
output.close()