-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
328 lines (298 loc) · 12.6 KB
/
main.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
import praw
import time
from datetime import datetime
import json
import imgkit
import pprint
from helpers import get_config
from upload import uploadsnapshot
config = get_config()
config.read('auth.ini')
client_id = config.get('bot', 'client_id')
client_secret=config.get('bot', 'client_secret')
username=config.get('bot', 'username')
password=config.get('bot', 'password')
user_agent=config.get('bot', 'user_agent')
reddit = praw.Reddit(
client_id=client_id,
client_secret=client_secret,
username=username,
password=password,
user_agent=user_agent,
)
param = 2
def get_price(award):
return award.get('coin_price')
def human_format(num):
magnitude = 0
while abs(num) >= 1000:
magnitude += 1
num /= 1000.0
# add more suffixes if you need them
return '%.0f%s' % (num, ['', 'K', 'M', 'G', 'T', 'P'][magnitude])
def generateImage(HTML, totalcomment):
commentwidth = 550
pagewidth = commentwidth + ((totalcomment + 1)*22) +20
options = {
'crop-w': pagewidth,
}
css = ['commentstyle.css']
imgkit.from_string(HTML, 'out.jpg', options,css=css)
def getCommentHtml(call, index):
#dir(object)
comment = call
usericon = comment.author.icon_img
username = comment.author
isop = ''
if comment.is_submitter == True:
isop = """<span class="isop">OP</span>"""
datec = datetime.fromtimestamp(comment.created_utc)
isedited = comment.edited
edited = ""
if isedited:
edited = """<i class="isedited"> - edited %s</i>"""%(datetime.fromtimestamp(isedited))
json_object = json.dumps(comment.all_awardings, indent = 4)
#print(json_object)
awardcount = 0
awardcount2 = 0
awardings = comment.all_awardings
awardings.sort(key=get_price, reverse=True)
moreAwardsHTML = ""
awardsHTML = ""
if len(awardings) > 0:
iterator = 0
for i in awardings:
awardcount = awardcount + i['count']
#print( i['coin_price'], i['resized_icons'][1]['url'], i['count'], end='\n\n')
if iterator < 3:
awardcount2 = awardcount2 + i['count']
awardtemplate = """
<span class="award" >
<img
alt="award-name"
src="%s"
/>
<span class="awardcount">%s</span>
</span>
"""%(i['resized_icons'][1]['url'], i['count'])
awardsHTML = awardsHTML + awardtemplate
iterator = iterator + 1
if awardcount != awardcount2:
moreAwardsHTML = """
<i class="moreawards">& %s more</i>
"""%(awardcount)
#print('Total Award => ', awardcount)
body = comment.body_html
score = human_format(comment.score)
commentTemplate = """
<div class="comment">
<div class="block-comment" style="padding-left:%spx">
<div class="comment-user-info">
<div class="comment-user-img">
<img src="%s" alt="" />
</div>
<span class="username">%s</span>
%s
<span class="date"> - %s</span>
%s
<div class="awards">
%s
%s
</div>
</div>
<div class="comment-content">
%s
<div class="comment-bottom">
<div class="vote-arrows">
<button>
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<title></title>
<g id="icomoon-ignore">
</g>
<path d="M665.6 979.2h-307.2c-31.788-0.058-57.542-25.812-57.6-57.594v-352.62h-192.051c-0.003 0-0.006 0-0.009 0-31.812 0-57.6-25.788-57.6-57.6 0-14.851 5.62-28.389 14.85-38.604l-0.045 0.050 403.302-447.59c10.83-11.18 25.981-18.12 42.752-18.12s31.922 6.94 42.737 18.104l0.015 0.016 403.405 447.693c9.213 10.171 14.852 23.73 14.852 38.605 0 31.777-25.732 57.544-57.496 57.6l-192.313 0.41v352.051c-0.058 31.788-25.812 57.542-57.594 57.6h-0.006zM364.8 915.2h294.4v-409.6l241.869-0.512-389.069-431.667-389.12 431.565h241.92zM516.71 68.147v0z"></path>
</span>
</button>
<div class="votecount">%s</div>
<button>
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<title></title>
<g id="icomoon-ignore1">
</g>
<path d="M512 1017.754c-0.008 0-0.017 0-0.026 0-16.929 0-32.151-7.31-42.682-18.946l-0.044-0.049-403.405-447.693c-9.213-10.171-14.852-23.73-14.852-38.605 0-31.777 25.732-57.544 57.496-57.6l192.313-0.41v-352.051c0.058-31.788 25.812-57.542 57.594-57.6h307.206c31.788 0.058 57.542 25.812 57.6 57.594v352.62h192.051c0.003 0 0.006 0 0.009 0 31.812 0 57.6 25.788 57.6 57.6 0 14.851-5.62 28.389-14.85 38.604l0.045-0.050-403.302 447.59c-10.574 11.685-25.796 18.995-42.726 18.995-0.009 0-0.019 0-0.028 0h0.001zM507.29 955.802v0zM122.88 518.861l389.12 431.718 389.12-431.565h-241.92v-410.214h-294.4v409.6z"></path>
</svg>
</button>
</div>
</div>
</div>
</div>
</div>
"""%((index + 1)* 21, usericon,username,isop,datec,edited,awardsHTML,moreAwardsHTML, body,score)
return commentTemplate
def getSubmissionHtml(submission):
sr = submission.subreddit
sr_image = sr.icon_img
sr_name = sr.display_name
poster = submission.author
datec = datetime.fromtimestamp(submission.created_utc)
isedited = submission.edited
edited = ""
if isedited:
edited = """<i class="isedited"> - edited %s</i>"""%(datetime.fromtimestamp(isedited))
#awards
awardcount = 0
awardcount2 = 0
awardings = submission.all_awardings
awardings.sort(key=get_price, reverse=True)
moreAwardsHTML = ""
awardsHTML = ""
if len(awardings) > 0:
iterator = 0
for i in awardings:
awardcount = awardcount + i['count']
#print( i['coin_price'], i['resized_icons'][1]['url'], i['count'], end='\n\n')
if iterator < 3:
awardcount2 = awardcount2 + i['count']
awardtemplate = """
<span class="award" >
<img
alt="award-name"
src="%s"
/>
<span class="awardcount">%s</span>
</span>
"""%(i['resized_icons'][1]['url'], i['count'])
awardsHTML = awardsHTML + awardtemplate
iterator = iterator + 1
if awardcount != awardcount2:
moreAwardsHTML = """
<i class="moreawards">& %s more</i>
"""%(awardcount)
post_title = submission.title
post_content = ""
if submission.selftext != "":
post_content = submission.selftext_html
else:
post_content = """
<img class="post-img" src="%s">
"""%(submission.preview['images'][0]['resolutions'][len(submission.preview['images'][0]['resolutions']) - 1]['url'])
score = human_format(submission.score)
comments_count = submission.num_comments
submissionTemplate = """
<div class="comment post">
<div class="block-comment">
<div class="comment-user-info post-user-info">
<div class="comment-user-img">
<img src="%s" alt="" />
</div>
<span class="subreddit-name">r/%s</span>
<span class="date">Posted by u/%s</span>
<span class="date"> - %s</span>
%s
<div class="awards">
%s
%s
</div>
</div>
<div class="comment-content">
<div class="post-title">
<h1>%s</h1>
</div>
<div class="post-content-container">
%s
</div>
<div class="comment-bottom" style="padding:8px 4px 8px 0">
<div class="vote-arrows">
<button>
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<title></title>
<g id="icomoon-ignore"></g>
<path d="M665.6 979.2h-307.2c-31.788-0.058-57.542-25.812-57.6-57.594v-352.62h-192.051c-0.003 0-0.006 0-0.009 0-31.812 0-57.6-25.788-57.6-57.6 0-14.851 5.62-28.389 14.85-38.604l-0.045 0.050 403.302-447.59c10.83-11.18 25.981-18.12 42.752-18.12s31.922 6.94 42.737 18.104l0.015 0.016 403.405 447.693c9.213 10.171 14.852 23.73 14.852 38.605 0 31.777-25.732 57.544-57.496 57.6l-192.313 0.41v352.051c-0.058 31.788-25.812 57.542-57.594 57.6h-0.006zM364.8 915.2h294.4v-409.6l241.869-0.512-389.069-431.667-389.12 431.565h241.92zM516.71 68.147v0z"></path>
</svg>
</button>
<div class="votecount">%s</div>
<button>
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<title></title>
<g id="icomoon-ignore1"></g>
<path d="M512 1017.754c-0.008 0-0.017 0-0.026 0-16.929 0-32.151-7.31-42.682-18.946l-0.044-0.049-403.405-447.693c-9.213-10.171-14.852-23.73-14.852-38.605 0-31.777 25.732-57.544 57.496-57.6l192.313-0.41v-352.051c0.058-31.788 25.812-57.542 57.594-57.6h307.206c31.788 0.058 57.542 25.812 57.6 57.594v352.62h192.051c0.003 0 0.006 0 0.009 0 31.812 0 57.6 25.788 57.6 57.6 0 14.851-5.62 28.389-14.85 38.604l0.045-0.050-403.302 447.59c-10.574 11.685-25.796 18.995-42.726 18.995-0.009 0-0.019 0-0.028 0h0.001zM507.29 955.802v0zM122.88 518.861l389.12 431.718 389.12-431.565h-241.92v-410.214h-294.4v409.6z"></path>
</svg>
</button>
</div>
<div class="vote-arrows" style="margin-left:20px;">
<button>
<!-- Generated by IcoMoon.io -->
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1024 1024">
<title></title>
<g id="icomoon-ignore"></g>
<path d="M389.12 1024c-20.479-0.029-37.069-16.637-37.069-37.12v0-161.28h-121.651c-102.457-0.116-185.484-83.143-185.6-185.589v-409.611c0.116-102.457 83.143-185.484 185.589-185.6h563.211c102.457 0.116 185.484 83.143 185.6 185.589v409.611c-0.116 102.457-83.143 185.484-185.589 185.6h-167.026l-212.787 189.133c-6.548 5.755-15.191 9.267-24.654 9.267-0.009 0-0.017 0-0.026 0h0.001zM230.4 108.8c-67.134 0.058-121.542 54.466-121.6 121.594v409.606c0.058 67.134 54.466 121.542 121.594 121.6h185.606v165.53l186.214-165.53h191.386c67.134-0.058 121.542-54.466 121.6-121.594v-409.606c-0.058-67.134-54.466-121.542-121.594-121.6h-0.006z"></path>
</svg>
</button>
<div class="votecount">%s</div>
</div>
</div>
<div class="divider"></div>
</div>
</div>
</div>
"""%(sr_image, sr_name, poster, datec, edited, awardsHTML, moreAwardsHTML, post_title, post_content, score, comments_count)
return submissionTemplate
#to generate reply for different kind of requests in the future
def generateReply(link1, link2):
link1template = ""
if link1 != "":
link1template =f'[Parent comment]({link1})'
link2template = ""
if link2 != "":
link2template = f'[All upper chain]({link2})'
template = f'Hi, here is your snapshot:\n\n {link1template} \n\n {link2template} \n\n_Still in Beta - [github link to contribute](https://github.com/ouzkagan/redditsnapshotbot)_'
return template
def snapshotbot():
keywords = ['!snapshot', '!snapshotbot', 'u/snapshotterbot','u/snapshotbot','u/snapshotterbot']
params = ['all', 'top']
# Checks each comment in the generated stream of new comments
# Skips bot calls that were made before the bot was running
#for comment in reddit.subreddit('memes').comments(limit=None):
for comment in reddit.subreddit('all').stream.comments():
howmany = ""
for keyword in keywords:
if (keyword in comment.body.lower()):
for param in params:
if param in comment.body.lower():
howmany = param
break
allparents = []
parent = comment.parent()
onlysubmission = ""
if parent.name.startswith('t3'):
onlysubmission = getSubmissionHtml(comment.parent())
generateImage(onlysubmission, 1)
link2 = uploadsnapshot('out.jpg',parent.title, parent.id, comment.permalink,comment.permalink, "reddit posts")
comment.reply(generateReply("", link2))
return
while(parent.name.startswith('t1')):
allparents.append(parent)
parent = parent.parent()
submission = getSubmissionHtml(parent)
parentcount = len(allparents) #for page size
allparents = allparents[::-1]
count = 0
chaintotop = ""
for p in allparents:
chaintotop += getCommentHtml(p, count)
count = count + 1
totalcomment = count
allofhtml = submission + chaintotop
generateImage(allofhtml, totalcomment)
link2 = uploadsnapshot('out.jpg',parent.title, parent.id, comment.id,comment.permalink, "reddit comments")
generateImage(getCommentHtml(comment.parent(), 0), 0)
link1 = uploadsnapshot('out.jpg',parent.title, parent.id, comment.id,comment.permalink,"reddit posts")
comment.reply(generateReply(link1, link2))
print("commented on post!!")
comment.save()
return
while True:
snapshotbot()