-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
462 lines (374 loc) · 13.9 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python
import logging
import os.path
import random
import re
import textwrap
import time
import traceback
from datetime import datetime
from pathlib import Path
import praw
import youtube_dl
from praw.models import MoreComments
from prawcore.exceptions import PrawcoreException
from telegram.ext import Updater
from youtube_dl.utils import YoutubeDLError
import utils
from ffmpeg_util import scale_video, is_file_image
from utils import lprint
posts_path = Path('data/posts.json')
used_posts_path = Path('data/used_posts.json')
accepted_content_types = ['image/gif', 'video/mp4']
config_path = Path('config.json')
min_score = "min_score"
wait_from = "wait_from" # minutes
wait_to = "wait_to" # minutes
posts_to_get_every_time = "posts_to_get_every_time"
max_posts_every_time = "max_posts_every_time"
no_posts_wait_time = "no_posts_wait_time" # minutes
min_comment_score = "min_comment_score"
main_channel_id = "main_channel_id"
comments_channel_id = "comments_channel_id"
max_line_chars = "max_line_chars"
c_telegram_api_key = "telegram_api_key"
c_reddit_client_id = "reddit_client_id"
c_reddit_client_secret = "reddit_client_secret"
c_reddit_user_agent = "reddit_user_agent"
c_max_comments_message_length = "max_comments_message_length"
c_min_time_since_created = "min_time_since_created" # minutes
c_subreddits = "subreddits"
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
config = utils.load_json(config_path, {})
def error(bot, update, error):
logger.warning('Update "%s" caused error "%s"' % (update, error))
def reload_config():
global config
lprint('reloading config')
config = utils.load_json(config_path, {})
def get_new_posts(max_count_to_get):
used_posts_ids = list(post['id'] for post in utils.load_json(used_posts_path, []))
print('{}: getting new posts...'.format(utils.beautiful_now()))
reddit = praw.Reddit(
client_id=config[c_reddit_client_id],
client_secret=config[c_reddit_client_secret],
user_agent=config[c_reddit_user_agent],
)
posts = []
print(
'{}: getting max {} hot submissions'.format(
utils.beautiful_now(), max_count_to_get
)
)
subreddits = (
[
"aww",
"WatchPeopleDieInside",
"funny",
"gifs",
"IdiotsInCars",
"instantkarma",
"Whatcouldgowrong",
]
if c_subreddits not in config
else config[c_subreddits]
)
for sr in subreddits:
try:
for submission in reddit.subreddit(sr).hot(limit=max_count_to_get):
if submission.distinguished and submission.distinguished == 'moderator':
lprint(
f'skipping moderator\t '
f'submission {submission.id} {submission}'
)
continue
# if submission.over_18:
# lprint(
# f'skipping over 18\t submission {submission.id} '
# f'{submission.title}'
# )
# continue
if submission.score < config[min_score]:
lprint(
f'skipping min score\t submission {submission.id} '
f'{submission.title} with score {submission.score}'
)
continue
if submission.id in used_posts_ids:
lprint(
f'skipping used\t\t submission {submission.id} '
f'{submission.title}'
)
continue
if int(datetime.today().timestamp() - submission.created_utc) < (
60 * config[c_min_time_since_created]
):
lprint(
f'skipping very new\t submission {submission.id} '
f'{submission.title}'
)
continue
posts.append(submission)
except PrawcoreException:
lprint(f'failed to get reddit submissions. will print stacktrace...')
lprint(f'{traceback.format_exc()}')
lprint(f'got {len(posts)} hot submissions')
posts.sort(key=lambda x: x.score, reverse=True)
posts = posts[: config[max_posts_every_time]]
lprint(f'using first {config[max_posts_every_time]} hot submissions')
return posts
def send_to_telegram(post):
post_id = post.id
post_title = post.title
sr_name = post.subreddit.display_name
print(
'{}: sending {} {} to telegram channel...'.format(
utils.beautiful_now(), post_id, post_title
)
)
updater = Updater(config[c_telegram_api_key])
ydl_opts = {
'outtmpl': f'downloaded_videos/{youtube_dl.DEFAULT_OUTTMPL}',
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
url = post.url
print(
'{}: {} downloading {} with youtube-dl ...'.format(
utils.beautiful_now(), post_id, url
)
)
try:
info = ydl.extract_info(url, download=True)
file_name = ydl.prepare_filename(info)
print(
'{}: {} {} youtube-dl result {}'.format(
utils.beautiful_now(), post_id, url, file_name
)
)
except YoutubeDLError:
print(
'{}: {} {} youtube-dl failed with exception.\n{}'.format(
utils.beautiful_now(), post_id, url, traceback.format_exc()
)
)
return False
is_image = is_file_image(file_name)
if not is_image:
scaled_path = '{}-scaled.mp4'.format(file_name)
scaled, has_audio = scale_video(file_name, scaled_path, 360)
if not scaled:
print(
'{}: failed to scale {} with file {}'.format(
utils.beautiful_now(), post_id, file_name
)
)
return False
print(
'{}: {} scaled {} to {}'.format(
utils.beautiful_now(),
post_id,
os.path.getsize(file_name),
os.path.getsize(scaled_path),
)
)
os.remove(file_name)
file_name = scaled_path
chat_id = f'@{config[main_channel_id]}'
caption = (
f'✌ #{sr_name}\n'
f'💬 {post.title}\n\n'
f'🔥 <code>{utils.human_format(post.score)}</code>\n'
f'🔗 {post.shortlink}\n\n'
f'{chat_id}'
)
uploaded = False
with open(file_name, 'rb') as fo:
try:
print(
'{}: sending {} {} sized {} to telegram channel @{}...'.format(
utils.beautiful_now(),
post_id,
post_title,
os.path.getsize(file_name),
config[main_channel_id],
)
)
if is_image:
result = updater.bot.send_photo(
chat_id=chat_id,
photo=fo,
caption=caption,
timeout=60,
parse_mode='HTML',
)
elif has_audio:
result = updater.bot.send_video(
chat_id=chat_id,
video=fo,
caption=caption,
timeout=60,
parse_mode='HTML',
)
else:
result = updater.bot.send_animation(
chat_id=chat_id,
animation=fo,
caption=caption,
timeout=60,
parse_mode='HTML',
)
print(
'{}: uploaded {} {} to telegram channel @{}.'.format(
utils.beautiful_now(), post_id, post_title, config[main_channel_id]
)
)
uploaded = True
print(
'{}: forwarding {} to @{}...'.format(
utils.beautiful_now(), post_id, config[comments_channel_id]
)
)
_ = updater.bot.forward_message(
f'@{config[comments_channel_id]}',
f'@{config[main_channel_id]}',
result.message_id,
)
print(
'{}: forwarded {} to @{}.'.format(
utils.beautiful_now(), post_id, config[comments_channel_id]
)
)
comments_header = 'Good comments from 👆🏿:'
def process_comment_forest(forest, current_length=0):
forest_result = ''
if current_length == 0:
forest_result = comments_header
for comment in forest:
if isinstance(comment, MoreComments):
continue
if comment.depth > 2:
continue
if not (
(comment.depth + 1) * comment.score >= config[min_comment_score]
):
continue
# skip comment if it contains xml tags
if len(re.findall('<[a-z][\s\S]*>', comment.body)):
continue
line_start = "| " * comment.depth
author = (
''
if not comment.author
else f'<a href="https://www.reddit.com/user/{comment.author.name}">'
f'{comment.author.name}</a>'
)
comment_formatted = (
f'{line_start}'
f'{author}'
f' <code>{utils.human_format(comment.score)}</code>'
)
line_chars = config[max_line_chars] - len(line_start)
comment_body = textwrap.fill(
comment.body, line_chars, break_long_words=False
)
for line in comment_body.splitlines():
comment_formatted = comment_formatted + f'\n{line_start}{line}'
line_break = f'\n{line_start}\n' * (
1 if len(forest_result) or current_length else 0
)
addition = f'{line_break}{comment_formatted}'
length_after_adding_current_comment = (
current_length + len(forest_result) + len(addition)
)
if (
length_after_adding_current_comment
> config[c_max_comments_message_length]
):
break
if comment.replies:
replies_formatted = process_comment_forest(
comment.replies,
current_length=length_after_adding_current_comment,
)
forest_result += addition + replies_formatted
else:
forest_result += addition
return forest_result
comments = process_comment_forest(post.comments)
if len(comments) > len(comments_header):
print(
'{}: {} sending comments to @{}.'.format(
utils.beautiful_now(), post_id, config[comments_channel_id]
)
)
updater.bot.send_message(
chat_id=f'@{config[comments_channel_id]}',
text=comments,
parse_mode='HTML',
disable_web_page_preview=True,
)
print(
'{}: {} sent comments to @{}.'.format(
utils.beautiful_now(), post_id, config[comments_channel_id]
)
)
except Exception:
lprint(
f'failed to get send {post_id} {post_title} to telegram. will print stacktrace...'
)
lprint(f'{traceback.format_exc()}')
pass
os.remove(file_name)
if uploaded:
return post
return False
def get_post_to_send():
posts = []
while not len(posts):
posts = get_new_posts(config[posts_to_get_every_time])
if not len(posts):
lprint('no posts to send!')
utils.sleep_until(60 * config[no_posts_wait_time])
reload_config()
return posts[0]
def send_a_gif():
used_posts = utils.load_json(used_posts_path, [])
while True:
post = get_post_to_send()
sent = send_to_telegram(post)
used_posts.append({'id': post.id, 'send_time': int(datetime.now().timestamp())})
utils.save_json(used_posts_path, used_posts)
if sent:
break
seconds = 60
print(
'{}: failed to send post {}, sleeping {} seconds...'.format(
utils.beautiful_now(), post.id, seconds
)
)
time.sleep(seconds)
now = int(datetime.now().timestamp())
for post in used_posts[:]:
if (now - post['send_time']) > utils.ONE_WEEK_SECONDS:
used_posts.remove(post)
utils.save_json(used_posts_path, used_posts)
def main():
print('{}: hi!'.format(utils.beautiful_now()))
while True:
send_a_gif()
sleep_time = 60 * random.randint(config[wait_from], config[wait_to])
now = datetime.now()
until = datetime.fromtimestamp(int(now.timestamp()) + sleep_time)
print(
'{}: sleeping for {} seconds until {}'.format(
utils.beautiful_date(now), sleep_time, utils.beautiful_date(until)
)
)
time.sleep(sleep_time)
reload_config()
if __name__ == '__main__':
main()