-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemosybot.py
99 lines (90 loc) · 3.6 KB
/
memosybot.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
# -*- coding: utf-8 -*-
import settings
import video_parsers
import json
import logging
from telegram.error import BadRequest, NetworkError
from telegram.ext import MessageHandler, ApplicationBuilder, filters, CallbackContext, Updater
from telegram import Update, Bot, MessageEntity, Message
from os import path
import sys
import queue
here = path.dirname(path.realpath(__file__))
sys.path.append(path.join(here, "./vendored"))
async def url_parse(update: Update, context: CallbackContext):
bot = context.bot
if not update.message:
return
if update.message.entities:
for entity in update.message.entities:
url = update.message.parse_entity(entity)
if '9gag.com' in url and not url.endswith('.mp4'):
await try_function(video_parsers.ninegag_parse, update, bot, url,)
elif 'coub.com' in url or 'coub.ru' in url:
await try_function(video_parsers.coub_parse, update, bot, url,)
elif 'youtu.be' in url or 'youtube.com' in url:
# if 'clip' in url:
# try_function(video_parsers.youtube_clip_parse, update,
# bot, url, update=update, bot=bot
# )
# else:
await try_function(video_parsers.youtube_parse, update, bot, url)
# if 'reddit.com' in url or 'v.redd.it' in url:
# await try_function(video_parsers.reddit_parse, update, bot, url)
if 'x.com' in url:
url = url.replace('x.com', 'twitter.com')
await try_function(video_parsers.youtube_parse, update, bot, url)
else:
await try_function(video_parsers.youtube_parse, update, bot, url)
async def error_message(*args, update: Update | None = None):
import traceback
bot = Bot(settings.token)
if update is not None and update.effective_chat is not None:
await bot.send_message(
chat_id=update.effective_chat.id,
text="Сорян, сегодня не мой день ¯\\_(ツ)_/¯",
reply_to_message_id=update.message.message_id
)
bot = Bot(settings.token)
await bot.send_message(
chat_id=settings.debug_chat_id,
text="WTF: %s\n Additional info: %s" % (traceback.format_exc(), args))
async def try_function(function, *args):
# loading_message = None
# if update.effective_chat is not None:
# loading_message = await context.bot.send_sticker(
# chat_id=update.effective_chat.id,
# sticker='CAACAgIAAxkBAAIBzGOHjfCGEPoiv6G2LCxCLUHjemF8AAJBAQACzRswCPHwYhjf9pZYKwQ',
# disable_notification=True,
# )
# x = 0
# while x < 10:
# print("Attempt %s" % x)
try:
await function(*args)
except BadRequest:
pass
# break
except NetworkError:
pass
# continue
except:
# break
print("Unexpected error:", sys.exc_info())
pass
await error_message(*args)
# break
# else:
# await error_message(
# *args, update=update, context=context)
# if loading_message is not None:
# await loading_message.delete()
if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
settings.init()
app =ApplicationBuilder().token(settings.token).build()
url_handler = MessageHandler(filters.TEXT & (
~filters.COMMAND) & filters.Entity(MessageEntity.URL), url_parse)
app.add_handler(url_handler)
app.run_polling()