-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredditinfo.py
124 lines (102 loc) · 3.6 KB
/
redditinfo.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
import praw
import slurs
"""userAgent = ('Subreddit analyzer for RedditGraph by /u/augzodia')
r = praw.Reddit(userAgent)
subreddit = r.get_subreddit('videos')
numSubmissions = 10
top = subreddit.get_top_from_all(limit = numSubmissions)
numComments = 0
for submission in top:
print submission.title
print "-",submission.score
numComments += len(submission.comments)
#for comment in submission.comments():
# numComments += 1
print "-",len(submission.comments)
print "Average:", numComments/numSubmissions"""
"""
data to get:
num upvotes in top
avg upvotes in top
num downvotes in top
avg downvotes in top
upvote/downvote ratio in top
"""
class SubredditAnalyzer:
userAgent = ('Subreddit analyzer for RedditGraph by /u/augzodia')
def __init__(self):
self.reddit = praw.Reddit(self.userAgent)
self.currentSubredditName = None
self.currentSubreddit = None
self.currentSubredditTop = None
self.currentNumSubmissions = 0
def getTotalCommentsInTop(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
numComments = 0
for submission in top:
#print numComments
numComments += submission.num_comments
return numComments
def getAvgCommentsInTop(self, subredditName, numSubmissions = 10):
numComments = self.getTotalCommentsInTop(subredditName, numSubmissions)
return numComments/float(numSubmissions)
def getTotalUpvotesInTop(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
numUpvotes = 0
for submission in top:
#print numUpvotes
numUpvotes += submission.ups
return numUpvotes
def getAvgUpvotesInTop(self, subredditName, numSubmissions = 10):
numUpvotes = self.getTotalUpvotesInTop(subredditName, numSubmissions)
return numUpvotes/float(numSubmissions)
def getUpvoteRatioInTop(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
upvoteRatio = 0.0
try:
for submission in top:
ups = submission.ups
total = ups + submission.downs
upvoteRatio += ups/float(total)
#print submission.title
#print ups, total, ups/float(total)
except:
print "din't work"
return -1.0
return upvoteRatio/numSubmissions
def getNSFWRatioInTop(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
numNSFW = 0
for submission in top:
#print numUpvotes
numNSFW += submission.over_18
return numNSFW/float(numSubmissions)
def getSlursInComments(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
numSlurs = 0
for submission in top:
comments = praw.helpers.flatten_tree(submission.comments)
#if containsSlur(submission.title):
# numSlurs += 1;
for comment in comments:
if not isinstance(comment,praw.objects.MoreComments):
s = comment.body
if slurs.containsSlur(s):
numSlurs += 1;
print numSlurs
def getStatsFromTop(self, subredditName, numSubmissions = 10):
subreddit = self.reddit.get_subreddit(subredditName)
top = subreddit.get_top_from_all(limit = numSubmissions)
#should eventually just do everything
"""s = SubredditAnalyzer()
print s.getUpvoteRatioInTop('happy'), "percent approval"
print s.getTotalUpvotesInTop('happy'), "upvotes in top"
"""
if __name__ == "__main__":
a = SubredditAnalyzer()
a.getSlursInComments("pics")