-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
390 lines (293 loc) · 15.6 KB
/
bot.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import os
import re
import praw
import calendar
import requests
import urllib
from pymongo import MongoClient
from bson.objectid import ObjectId
from io import BytesIO
from PIL import Image
from datetime import datetime
from colorcube import ColorCube
class objdict(dict):
def __getattr__(self, name):
if name in self:
return self[name]
else:
raise AttributeError("No such attribute: " + name)
def __setattr__(self, name, value):
self[name] = value
def __delattr__(self, name):
if name in self:
del self[name]
else:
raise AttributeError("No such attribute: " + name)
# DATABASE
DB_HOST = os.environ.get("DB_HOST")
DB_PORT = os.environ.get("DB_PORT")
DB_DB = os.environ.get("DB_DB")
DB_USER = os.environ.get("DB_USER")
DB_PASSWORD = os.environ.get("DB_PASSWORD")
# REDDIT
REDDIT_CLIENT_ID = os.environ.get("REDDIT_CLIENT_ID")
REDDIT_CLIENT_SECRET = os.environ.get("REDDIT_CLIENT_SECRET")
REDDIT_CLIENT_REFRESH_TOKEN = os.environ.get("REDDIT_CLIENT_REFRESH_TOKEN")
# IMGUR
IMGUR_CLIENT_ID = os.environ.get("IMGUR_CLIENT_ID")
# SUBREDDIT
SUBREDDIT = os.environ.get("SUBREDDIT")
mongo = MongoClient(host=DB_HOST, port=int(DB_PORT), username=DB_USER, password=DB_PASSWORD, authSource=DB_DB, authMechanism='SCRAM-SHA-256')
db = mongo[DB_DB]
reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
refresh_token=REDDIT_CLIENT_REFRESH_TOKEN,
user_agent='/r/low_poly bot by /u/RenegadeAI')
print('Successfully logged into reddit as {}'.format(reddit.user.me()))
FLAIR_LABELS = ['Blender', 'Unity', 'Modo', '3DS Max', 'Cinema 4D', 'Maya', '<other>']
REPORT = {
"to": "RenegadeAI",
"subject": "yo dawg low_poly is broke!1!!1",
"message": "<plz replace this with a description of the error, links, screenshots, mothers maiden name, SSN, credit card number and pin, name of first pet, etc thx bye>"
}
LINKS = [
f'[**Report a Problem**](https://old.reddit.com/message/compose?{urllib.parse.urlencode(REPORT, quote_via=urllib.parse.quote)})',
"[**Github**](https://github.com/notderw/lowpoly)",
"[**Discord**](https://discord.gg/9aHrFvr)"
]
FOOTER = " \n \n ___\n [](#BOT_FOOTER)^" + " | ".join(LINKS).replace(" ", " ")
subreddit = reddit.subreddit(SUBREDDIT)
meta = objdict(db.meta.find_one({}))
now = datetime.utcnow()
monthrange = calendar.monthrange(now.year,now.month)[1]
month = now.month
day = now.day
month_name = calendar.month_name[month]
time = now.time().replace(microsecond=0)
next_month_name = calendar.month_name[month % 12 + 1]
last_month = month - 1 or 12
last_month_name = calendar.month_name[last_month]
last_month_year = now.year - 1 if last_month == 12 else now.year
def update_meta(data):
try:
db.meta.update_one({}, {"$set": data})
except Exception as e:
print('Error updating meta {}'.format(e))
def upload_submissions():
# reddit won't fuck off with changing search, so now we have to
# nake it look ugly and have an identifier in the flair text
# since they deprecated searching by css class
submissions = [{
'image': get_image(submission),
'url': submission.shortlink,
'title': re.sub("\[[^]]*\]", '', submission.title),
'author': submission.author.name
} for submission in reddit.subreddit("low_poly").search('flair:"*{} {} SUBMISSION"'.format(last_month_name, last_month_year))]
db.submissions.delete_many({})
if submissions: db.submissions.insert_many(submissions)
def get_image(submission):
url = submission.url.replace('http:', 'https:')
match = re.match('(https?)\:\/\/(www\.)?(?:m\.)?imgur\.com/a/([a-zA-Z0-9]+)(#[0-9]+)?', url)
if match:
album_id = match.group(3)
response = requests.get('https://api.imgur.com/3/album/{}'.format(album_id), headers={'Authorization': 'Client-ID {}'.format(IMGUR_CLIENT_ID)})
if response.status_code not in [404, 429, 500]:
return response.json()['data']['images'][0]['link']
match = re.match('^https?://(?:www\.)?imgur\.com/gallery/([a-zA-Z0-9]+)', url)
if match:
gallery_id = match.group(1)
response = requests.get('https://api.imgur.com/3/gallery/{}'.format(gallery_id), headers={'Authorization': 'Client-ID {}'.format(IMGUR_CLIENT_ID)})
if response.status_code not in [404, 429, 500]:
return response.json()['data']['link']
match = re.match(r"(?:https?\:\/\/)?(?:www\.)?(?:m\.)?(?:i\.)?imgur\.com\/([a-zA-Z0-9]+)", url)
if match:
image_id = match.group(1)
response = requests.get('https://api.imgur.com/3/image/{}'.format(image_id), headers={'Authorization': 'Client-ID {}'.format(IMGUR_CLIENT_ID)})
if response.status_code not in [404, 429, 500]:
return response.json()['data']['link']
if(url.endswith(tuple(['.png','jpg']))):
return url
def update_flairs():
subreddit.flair.link_templates.clear()
# This is ugly but it works so fuck it
for label in FLAIR_LABELS + ['{} \\\\ {} {} SUBMISSION'.format(label, month_name , now.year) for label in FLAIR_LABELS]:
css = label.lower().split(' ')[0]
editable = False
if label.startswith('<other>'):
css = ''
editable = True
subreddit.flair.link_templates.add(label, css_class=css, text_editable=editable)
def get_monthly_theme():
submission = reddit.submission(meta.theme_voting)
submission.comment_sort = 'top'
return sorted(submission.comments, key=lambda k: k.score, reverse=True)[0]
def get_winner():
winner = db.votes.aggregate([
{
"$group": {
"_id": "$vote",
"count": {"$sum": 1}
}
},
{"$sort": { "count": -1}}
])
return objdict(db.submissions.find_one({"_id": ObjectId(list(winner)[0]["_id"])}))
def substitute_content(original_content, new, marker):
content = re.sub(r'(\[\]\(#' + marker + '\)).*(\[\]\(/' + marker + '\))', '\\1\\2', original_content, flags=re.DOTALL)
opening_marker = "[](#" + marker + ")"
try:
marker_pos = content.index(opening_marker) + len(opening_marker)
return content[:marker_pos] + new + content[marker_pos:]
except ValueError:
# Substring not found
print("Marker {} not found in content".format(opening_marker))
return original_content
def clamp(val, minimum=0, maximum=255):
if val < minimum:
return minimum
if val > maximum:
return maximum
return val
def colorscale(hexstr, scalefactor):
"""
Scales a hex string by ``scalefactor``. Returns scaled hex string.
To darken the color, use a float value between 0 and 1.
To brighten the color, use a float value greater than 1.
>>> colorscale("#DF3C3C", .5)
#6F1E1E
>>> colorscale("#52D24F", 1.6)
#83FF7E
>>> colorscale("#4F75D2", 1)
#4F75D2
"""
hexstr = hexstr.strip('#')
if scalefactor < 0 or len(hexstr) != 6:
return hexstr
r, g, b = int(hexstr[:2], 16), int(hexstr[2:4], 16), int(hexstr[4:], 16)
r = clamp(r * scalefactor)
g = clamp(g * scalefactor)
b = clamp(b * scalefactor)
return "#%02x%02x%02x" % (int(r), int(g), int(b))
def update_theme(winner):
new_sidebar = substitute_content(subreddit.mod.settings()['description'],
(
"\n"
"> *{0} monthly winner:* \n"
"[{1.title:.30}]({2}) by /u/{1.author}\n"
"\n"
).format(last_month_name, winner, requests.utils.unquote(winner.url)),
'BOTWINNER'
)
subreddit.mod.update(description=new_sidebar)
#################################################################
# Download the winner image, resize it, and upload it to reddit
#################################################################
response = requests.get(requests.utils.unquote(winner.image), stream=True, headers={'User-agent': 'Mozilla/5.0'})
response.raw.decode_content = True
header_image = Image.open(response.raw).convert('RGB')
ratio = 1920 / header_image.size[0]
header_image = header_image.resize((int(header_image.size[0] * ratio), header_image.size[1] * 1), Image.ANTIALIAS)
top = int(header_image.size[1] / 2) - 260
header_image = header_image.crop((0, top, 1920, top + 416))
# If we just use im.tostring() we get a massive image that won't upload
def toJPEG(im):
with BytesIO() as f:
im.save(f, format='JPEG')
return f.getvalue()
# PRAW doesn't support raw image uploads so we will do it ourselves
# https://github.com/praw-dev/praw/blob/a75ebcf934fb49a6966a04d172fa00e957836958/praw/models/reddit/subreddit.py#L1874
url = praw.const.API_PATH['upload_image'].format(subreddit=subreddit.display_name)
subreddit._reddit.post(url,
data={
'name': 'headerimg',
'upload_type': 'img',
'img_type': 'jpg'
},
files={'file': toJPEG(header_image)})
#################################################################
#Calulate colors, and upload the custom stylesheet bit
#################################################################
cc = ColorCube(avoid_color=[255, 255, 255], distinct_threshold=0.8)
image = header_image.resize((50, 50))
primary = '#%02x%02x%02x' % tuple(cc.get_colors(image)[0])
bot_style = (
".side .titlebox .md h3 a, .drop-choices a.choice:hover, .submit-page #newlink.submit.content ul.tabmenu.formtab,"
".submit_text.enabled.roundfield, body .btn, body button, .content .infobar,"
"form input[type=checkbox]:checked + label:before, .pretty-form input[type=checkbox]:checked + label:before,"
".titlebox .fancy-toggle-button .active.add,"
".reddit-infobar.with-icon.locked-infobar, .reddit-infobar.with-icon.locked-infobar:before,"
".flair, .side .md>blockquote:first-of-type a:hover:after {{background-color: {color.primary}}}"
".side .titlebox .md h3 a:hover, .btn:hover, body button:hover, .titlebox .fancy-toggle-button .active.add:hover {{background-color: {color.hover};}}"
".side .titlebox .md h3 a:active , .btn:active , body button:active, .titlebox .fancy-toggle-button .active.add:active {{background-color: {color.active};}}"
".thing .title.loggedin.click, .thing .title.click, .thing .title.loggedin, .thing .title, .link .entry .buttons li a.comments,"
".link .entry .buttons li a.flairselectbtn, .link .entry .buttons li a:hover, .titlebox .tagline a.flairselectbtn, .md a,"
".side .titlebox .md h4 a, .wiki-page .wiki-page-content .md.wiki h4, .sidebox.create .morelink a, a, .side:after, .usertext .bottom-area a.reddiquette,"
".wiki-page .pageactions .wikiaction-current, .tagline .submitter, .combined-search-page .search-result .search-result-header .search-title, "
".combined-search-page .search-result a, .combined-search-page .search-result a>mark, .combined-search-page .search-result .search-comments, "
".flairselector h2, .linefield .title, body .content .sitetable .link .title a:hover, .link .entry .tagline a:hover, .comment .author:hover,"
".morelink a, .morelink:hover a, #header .tabmenu li.selected a, form input[type=checkbox]:checked + label, .pretty-form input[type=checkbox]:checked + label, .side .md>blockquote:first-of-type a {{color: {color.primary};}}"
"form input[type=checkbox]:checked + label:before, .pretty-form input[type=checkbox]:checked + label:before {{ border-color: {color.primary};}}"
"body .content .roundfield textarea:focus, body .content input[type=text]:focus, body .content input[type=url]:focus, .roundfield input[type=password]:focus, .roundfield input[type=number]:focus {{ border-color: {color.primary}; box-shadow: 0 1px 0 0 {color.primary};}}"
).format(color = objdict({
'primary': primary,
'hover': colorscale(primary, 1.1),
'active': colorscale(primary, .75),
}))
stylesheet = substitute_content(subreddit.stylesheet().stylesheet,
bot_style,
'POLYGONAUTOMATON'
)
subreddit.stylesheet.update(stylesheet)
if __name__ == '__main__':
if(day == monthrange - 7):
submission = subreddit.submit("{} theme voting".format(next_month_name),
selftext = ("Hello everyone, please comment your suggestion for next months theme.\n\n"
"The deadline for voting will be the end of {0} {1}.\n\n"
"Please limit your response to only your theme idea, I am not a smart bot."
+ FOOTER
).format(month_name, monthrange)
)
submission.mod.contest_mode()
submission.mod.sticky()
update_meta({'theme_voting': submission.id})
elif(day == 1):
try:
reddit.submission(meta.monthly_winner).mod.sticky(state=False)
reddit.submission(meta.theme_voting).mod.sticky(state=False)
reddit.submission(meta.theme_voting).mod.lock()
except Exception as e:
print('ERROR: {}'.format(e))
upload_submissions()
submission = subreddit.submit("{} voting now open! Click here to pick your favourite submission!".format(last_month_name),
url = "https://lowpoly.derw.xyz/"
)
submission.mod.sticky()
update_meta({'voting': submission.id})
winner_comment = get_monthly_theme()
submission = subreddit.submit("{0} monthly theme: {1}".format(month_name, winner_comment.body),
selftext = (
"This months theme is {0.body} as suggested by /u/{0.author}\n"
"\n"
"Submissions will be due at the end of the month"
+ FOOTER
).format(winner_comment)
)
submission.mod.sticky()
update_meta({'theme': submission.id})
update_flairs()
elif(day == 8):
winner = get_winner()
submission = subreddit.submit("{0} monthly winner: {1}".format(last_month_name, winner.author),
selftext = (
"Thanks to everyone who participated in last month's challenge.\n"
"\n"
"{0}'s winner is /u/{1.author}, with their submission: [{1.title}]({2})"
+ FOOTER
).format(last_month_name, winner, requests.utils.unquote(winner.url))
)
try:
submission.mod.sticky(bottom=False)
except Exception as e:
print('ERROR: {}'.format(e))
update_meta({'monthly_winner': submission.id})
update_theme(winner)