-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
230 lines (205 loc) · 8.86 KB
/
script.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
220
221
222
223
224
225
226
227
228
229
230
import praw
import sys
import xlsxwriter
from time import time
reddit = praw.Reddit(client_id='XXXXXXXXX',
client_secret='XXXXXXXX', password='XXXXXXX',
user_agent='XXXXXX', username='XXXXXX')
non_bmp_map = dict.fromkeys(range(0x10000, sys.maxunicode + 1), 0xfffd)
InitialTime = time()
subreddit = reddit.subreddit('XXXXXXXX')
i=0
ArrayCommnent=[]
ArrayPost=[]
ArrayFlair=[]
WorstCommentUpvote=99999
WorstComment=''
WorstAuthor=''
WorstCommentPost=''
BestCommentUpvote=-99999
BestComment=''
BestAuthor=''
BestCommentPost=''
BestPost=''
BestPostUpvote=-99999
WorstPost=''
WorstPostUpvote=99999
PostQuantity=0
CommentQuantity=0
TotalPostUpvotes=0
TotalPostDownvotes=0
TotalCommentUpvotes=0
new_python = subreddit.new(limit=1500)
for submission in new_python:
try:
if int('1551398400')< int(submission.created_utc)<int('1551916800'):
ratio = submission.upvote_ratio
ups = round((ratio*submission.score)/(2*ratio - 1)) if ratio != 0.5 else round(submission.score/2)
downs = ups - submission.score
TotalPostUpvotes+=submission.ups
TotalPostDownvotes+=downs
UserPostQuantity=0
PostQuantity+=1
if BestPostUpvote<ups:
BestPostUpvote = ups
BestPost= submission.title
if WorstPostUpvote>downs:
WorstPostUpvote = ups
WorstPost= submission.title
if '{}'.format(submission.author) not in ArrayPost:
ArrayPost.insert(i,'{}'.format(submission.author))
ArrayPost.insert(i+1,submission.ups-1)
ArrayPost.insert(i+2,downs)
ArrayPost.insert(i+3,UserPostQuantity+1)
i+=4
else:
index=ArrayPost.index('{}'.format(submission.author))
SumQuantityUpvotePost=int(ArrayPost[index+1])+ int(submission.ups-1)
ArrayPost.insert(index+1,str(SumQuantityUpvotePost))
del ArrayPost[index+2]
SumQuantityDownvotePost=int(ArrayPost[index+2])+ downs
ArrayPost.insert(index+2,str(SumQuantityDownvotePost))
del ArrayPost[index+3]
SumQuantityPostUser=int(ArrayPost[index+3])+ 1
ArrayPost.insert(index+3,str(SumQuantityPostUser))
del ArrayPost[index+4]
i+=4
if '{}'.format(submission.link_flair_text) not in ArrayFlair:
ArrayFlair.insert(i,'{}'.format(submission.link_flair_text))
ArrayFlair.insert(i+1,1)
i+=2
else:
index=ArrayFlair.index('{}'.format(submission.link_flair_text))
SumQuantityFlair=int(ArrayFlair[index+1])+ 1
ArrayFlair.insert(index+1,str(SumQuantityFlair))
del ArrayFlair[index+2]
i+=2
UserCommentQuantity=0
submission.comments.replace_more(limit=0)
for comment in submission.comments.list():
TotalCommentUpvotes+=comment.ups
CommentQuantity+=1
if WorstCommentUpvote>comment.ups:
WorstComment=comment.body
WorstCommentUpvote=comment.ups
WorstAuthor='{}'.format(comment.author)
WorstCommentPost='{}'.format(submission.title)
if BestCommentUpvote<comment.ups:
BestComment=comment.body
BestCommentUpvote=comment.ups
BestAuthor='{}'.format(comment.author)
BestCommentPost='{}'.format(submission.title)
if '{}'.format(comment.author) not in ArrayCommnent:
ArrayCommnent.insert(i,'{}'.format(comment.author))
ArrayCommnent.insert(i+1,comment.ups-1)
ArrayCommnent.insert(i+2,UserCommentQuantity+1)
i+=3
else:
index=ArrayCommnent.index('{}'.format(comment.author))
suma=int(ArrayCommnent[index+1])+ int(comment.ups-1)
ArrayCommnent.insert(index+1,str(suma))
del ArrayCommnent[index+2]
SumQuantityCommentUser=int(ArrayCommnent[index+2])+ 1
ArrayCommnent.insert(index+2,str(SumQuantityCommentUser))
del ArrayCommnent[index+3]
i+=3
except UnicodeEncodeError:
print('Unicode Encode Error')
def GenerateDocuments():
GeneratePostDocument()
GenerateCommentDocument()
GenerateFlairDocument()
GenerateStatisticDocument()
def GeneratePostDocument():
workbook = xlsxwriter.Workbook('Posts.xlsx')
worksheet = workbook.add_worksheet()
row = 0
column = 0
worksheet.write(row, column, "User")
worksheet.write(row, column+1, "Upvotes")
worksheet.write(row, column+2, "Downvotes Posts")
worksheet.write(row, column+3, "Quantity Posts")
for item in ArrayPost:
worksheet.write(row+1, column, item)
column += 1
if (column% 4) == 0:
column = 0
row += 1
workbook.close()
def GenerateCommentDocument():
workbook = xlsxwriter.Workbook('Comments.xlsx')
worksheet = workbook.add_worksheet()
row = 0
column = 0
worksheet.write(row, column, "User")
worksheet.write(row, column+1, "Upvotes")
worksheet.write(row, column+2, "Quantity Comments")
for item in ArrayCommnent:
worksheet.write(row+1, column, item)
column += 1
if (column % 3) == 0:
column = 0
row += 1
workbook.close()
def GenerateFlairDocument():
workbook = xlsxwriter.Workbook('Flairs.xlsx')
worksheet = workbook.add_worksheet()
row = 0
column = 0
worksheet.write(row, column, "Flairs")
worksheet.write(row, column+1, "Quantity")
for item in ArrayFlair:
worksheet.write(row+1, column, item)
column += 1
if (column % 2) == 0:
column = 0
row += 1
workbook.close()
def GenerateStatisticDocument():
workbook = xlsxwriter.Workbook('Statistics.xlsx')
worksheet = workbook.add_worksheet()
row = 0
column = 0
worksheet.write(row, column, "Statistics")
worksheet.write(row, column+1, "Quantity")
worksheet.write(row+1, column, "Total Post Upvotes")
worksheet.write(row+1, column+1, TotalPostUpvotes)
worksheet.write(row+2, column, "Total Post Downvotes")
worksheet.write(row+2, column+1, TotalPostDownvotes)
worksheet.write(row+3, column, "Total Post Average Upvotes")
worksheet.write(row+3, column+1, round(TotalPostUpvotes/PostQuantity,2))
worksheet.write(row+4, column, "Total Post Average Downvote")
worksheet.write(row+4, column+1, round(TotalPostDownvotes/PostQuantity,2))
worksheet.write(row+5, column, "Comment Quantity")
worksheet.write(row+5, column+1, CommentQuantity)
worksheet.write(row+6, column, "Post Quantity")
worksheet.write(row+6, column+1, PostQuantity)
worksheet.write(row+7, column, "Total Comment Upvotes")
worksheet.write(row+7, column+1, TotalCommentUpvotes)
worksheet.write(row+8, column, "Average Upvotes Comment")
worksheet.write(row+8, column+1, round(TotalCommentUpvotes/PostQuantity,2))
worksheet.write(row+9, column, "The worst comment is")
worksheet.write(row+9, column+1, WorstComment.translate(non_bmp_map))
worksheet.write(row+10, column, "With")
worksheet.write(row+10, column+1, WorstCommentUpvote)
worksheet.write(row+11, column, "upvotes of the author")
worksheet.write(row+11, column+1, WorstAuthor)
worksheet.write(row+12, column, "of the post")
worksheet.write(row+12, column+1, WorstCommentPost.translate(non_bmp_map))
worksheet.write(row+13, column, "The best comment is")
worksheet.write(row+13, column+1, BestComment.translate(non_bmp_map))
worksheet.write(row+14, column, "With")
worksheet.write(row+14, column+1, BestCommentUpvote)
worksheet.write(row+15, column, "upvotes of the author")
worksheet.write(row+15, column+1, BestAuthor)
worksheet.write(row+16, column, "of the post")
worksheet.write(row+16, column+1, BestCommentPost.translate(non_bmp_map))
worksheet.write(row+17, column, "Best Post")
worksheet.write(row+17, column+1, BestPost.translate(non_bmp_map))
worksheet.write(row+18, column, "Worst Post")
worksheet.write(row+18, column+1, WorstPost.translate(non_bmp_map))
workbook.close()
GenerateDocuments()
FinalTime = time()
ExecutionTime = FinalTime - InitialTime
print ('Execution Time:',round(ExecutionTime ,2),'seconds ó',round(ExecutionTime/60,2),'minutes')