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