add reply feature (slow?)
This commit is contained in:
parent
eb0e85faa7
commit
0024f134e0
1 changed files with 51 additions and 43 deletions
92
delojza.py
92
delojza.py
|
@ -47,14 +47,13 @@ class DelojzaBot:
|
||||||
|
|
||||||
dp.add_handler(CommandHandler("start", self.tg_start))
|
dp.add_handler(CommandHandler("start", self.tg_start))
|
||||||
dp.add_error_handler(self.tg_error)
|
dp.add_error_handler(self.tg_error)
|
||||||
|
|
||||||
dp.add_handler(MessageHandler(Filters.entity(MessageEntity.URL), self.tg_handle_url))
|
|
||||||
dp.add_handler(
|
|
||||||
MessageHandler(
|
|
||||||
Filters.photo | Filters.video | Filters.video_note | Filters.audio | Filters.voice | Filters.document,
|
|
||||||
self.tg_handle_rest))
|
|
||||||
dp.add_handler(MessageHandler(Filters.entity(MessageEntity.HASHTAG), self.tg_handle_hashtag))
|
dp.add_handler(MessageHandler(Filters.entity(MessageEntity.HASHTAG), self.tg_handle_hashtag))
|
||||||
dp.add_handler(MessageHandler(Filters.text, self.tg_handle_text))
|
dp.add_handler(MessageHandler(Filters.text, self.tg_handle_text))
|
||||||
|
self.tg_url_handler = MessageHandler(Filters.entity(MessageEntity.URL), self.tg_handle_url)
|
||||||
|
dp.add_handler(self.tg_url_handler)
|
||||||
|
self.tg_rest_handler = MessageHandler(Filters.photo | Filters.video | Filters.video_note |
|
||||||
|
Filters.audio | Filters.voice | Filters.document, self.tg_handle_rest)
|
||||||
|
dp.add_handler(self.tg_rest_handler)
|
||||||
|
|
||||||
if tumblr_keys:
|
if tumblr_keys:
|
||||||
self.client = pytumblr.TumblrRestClient(*tumblr_keys)
|
self.client = pytumblr.TumblrRestClient(*tumblr_keys)
|
||||||
|
@ -114,34 +113,37 @@ class DelojzaBot:
|
||||||
filenames.append(local_filename)
|
filenames.append(local_filename)
|
||||||
return filenames
|
return filenames
|
||||||
|
|
||||||
def get_first_hashtag(self, message):
|
@staticmethod
|
||||||
|
def extract_first_hashtag(message):
|
||||||
hashtags = list(map(message.parse_entity,
|
hashtags = list(map(message.parse_entity,
|
||||||
list(filter(lambda e: e.type == 'hashtag', message.entities))))
|
list(filter(lambda e: e.type == 'hashtag', message.entities))))
|
||||||
hashtags += list(map(message.parse_caption_entity,
|
hashtags += list(map(message.parse_caption_entity,
|
||||||
list(filter(lambda e: e.type == 'hashtag', message.caption_entities))))
|
list(filter(lambda e: e.type == 'hashtag', message.caption_entities))))
|
||||||
if len(hashtags) == 0:
|
if len(hashtags) > 0:
|
||||||
if self.last_hashtag is not None and self.last_hashtag[0] == message.from_user:
|
hashtag = hashtags[0][1:].upper()
|
||||||
prehashtag = self.last_hashtag[1]
|
|
||||||
self.last_hashtag = None
|
|
||||||
else:
|
|
||||||
return None
|
|
||||||
else:
|
|
||||||
prehashtag = hashtags[0]
|
|
||||||
hashtag = prehashtag[1:].upper()
|
|
||||||
if "PRAS" in hashtag:
|
if "PRAS" in hashtag:
|
||||||
hashtag = "PRAS"
|
hashtag = "PRAS"
|
||||||
return hashtag
|
return hashtag
|
||||||
|
|
||||||
def tg_handle_hashtag(self, _, update):
|
def get_hashtag(self, message):
|
||||||
hashtags = list(map(update.message.parse_entity,
|
hashtag = self.extract_first_hashtag(message)
|
||||||
list(filter(lambda e: e.type == 'hashtag', update.message.entities))))
|
if hashtag is None:
|
||||||
if len(hashtags) > 0:
|
if self.last_hashtag is not None and self.last_hashtag[0] == message.from_user:
|
||||||
self.last_hashtag = (update.message.from_user, hashtags[0])
|
hashtag = self.last_hashtag[1]
|
||||||
|
return hashtag
|
||||||
|
|
||||||
|
def tg_handle_hashtag(self, bot, update):
|
||||||
|
hashtag = self.extract_first_hashtag(update.message)
|
||||||
|
|
||||||
|
if update.message.reply_to_message:
|
||||||
|
self.handle_tg_message(update.message.reply_to_message, bot, hashtag)
|
||||||
|
self.handle_urls(update.message.reply_to_message, hashtag)
|
||||||
|
else:
|
||||||
|
self.last_hashtag = (update.message.from_user, hashtag)
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
def handle(self, urls, message, download_fn, filename=None):
|
def handle(self, urls, message, hashtag, download_fn, filename=None):
|
||||||
try:
|
try:
|
||||||
hashtag = self.get_first_hashtag(message)
|
|
||||||
if hashtag is None:
|
if hashtag is None:
|
||||||
self.logger.info("Ignoring %s due to no hashtag present..." % urls)
|
self.logger.info("Ignoring %s due to no hashtag present..." % urls)
|
||||||
return
|
return
|
||||||
|
@ -173,39 +175,45 @@ class DelojzaBot:
|
||||||
message.reply_text("Something is FUCKED: %s" % exc_value)
|
message.reply_text("Something is FUCKED: %s" % exc_value)
|
||||||
|
|
||||||
def tg_handle_url(self, _, update):
|
def tg_handle_url(self, _, update):
|
||||||
urls = list(map(lambda e: update.message.parse_entity(e),
|
self.handle_urls(update.message, self.get_hashtag(update.message))
|
||||||
filter(lambda e: e.type == 'url', update.message.entities)))
|
|
||||||
|
def handle_urls(self, message, hashtag):
|
||||||
|
urls = list(map(lambda e: message.parse_entity(e),
|
||||||
|
filter(lambda e: e.type == 'url', message.entities)))
|
||||||
ytdl_urls = [url for url in urls if self.ytdl_can(url)]
|
ytdl_urls = [url for url in urls if self.ytdl_can(url)]
|
||||||
normal_urls = [url for url in urls if not self.ytdl_can(url)]
|
normal_urls = [url for url in urls if not self.ytdl_can(url)]
|
||||||
if len(ytdl_urls) > 0:
|
if len(ytdl_urls) > 0:
|
||||||
self.handle(ytdl_urls, update.message, self.download_ytdl)
|
self.handle(ytdl_urls, message, hashtag, self.download_ytdl)
|
||||||
if len(normal_urls) > 0:
|
if len(normal_urls) > 0:
|
||||||
image_urls = [url for url in normal_urls if "image" in requests.head(url).headers.get("Content-Type", "")]
|
image_urls = [url for url in normal_urls if "image" in requests.head(url).headers.get("Content-Type", "")]
|
||||||
if len(image_urls) > 0:
|
if len(image_urls) > 0:
|
||||||
self.handle(image_urls, update.message, self.download_raw)
|
self.handle(image_urls, message, hashtag, self.download_raw)
|
||||||
|
|
||||||
# noinspection PyBroadException
|
# noinspection PyBroadException
|
||||||
def tg_handle_rest(self, bot, update):
|
def tg_handle_rest(self, bot, update):
|
||||||
|
self.handle_tg_message(update.message, bot, self.get_hashtag(update.message))
|
||||||
|
|
||||||
|
def handle_tg_message(self, message, bot, hashtag):
|
||||||
file, filename, tumblr = None, None, False
|
file, filename, tumblr = None, None, False
|
||||||
if len(update.message.photo) > 0:
|
if len(message.photo) > 0:
|
||||||
photo = max(update.message.photo, key=lambda p: p.width)
|
photo = max(message.photo, key=lambda p: p.width)
|
||||||
file = photo.file_id
|
file = photo.file_id
|
||||||
elif update.message.document is not None:
|
elif message.document is not None:
|
||||||
filename = update.message.document.file_name
|
filename = message.document.file_name
|
||||||
file = update.message.document.file_id
|
file = message.document.file_id
|
||||||
elif update.message.audio is not None:
|
elif message.audio is not None:
|
||||||
filename = update.message.audio.title
|
filename = message.audio.title
|
||||||
file = update.message.audio.file_id
|
file = message.audio.file_id
|
||||||
elif update.message.video is not None:
|
elif message.video is not None:
|
||||||
file = update.message.video.file_id
|
file = message.video.file_id
|
||||||
elif update.message.video_note is not None:
|
elif message.video_note is not None:
|
||||||
file = update.message.video_note.file_id
|
file = message.video_note.file_id
|
||||||
elif update.message.voice is not None:
|
elif message.voice is not None:
|
||||||
file = update.message.voice.file_id
|
file = message.voice.file_id
|
||||||
|
|
||||||
if file is not None:
|
if file is not None:
|
||||||
url = bot.getFile(file).file_path
|
url = bot.getFile(file).file_path
|
||||||
self.handle([url], update.message, self.download_raw, filename=filename)
|
self.handle([url], message, hashtag, self.download_raw, filename=filename)
|
||||||
|
|
||||||
def tg_handle_text(self, _, update):
|
def tg_handle_text(self, _, update):
|
||||||
if self.markov:
|
if self.markov:
|
||||||
|
|
Loading…
Reference in a new issue