feat: default values, general item excludes, images, queueManager to manage multi worker messaging to telegram to prevent too many connections
Signed-off-by: Omar Sánchez Pizarro <omar.sanchez@pistacero.net>
This commit is contained in:
@@ -1,21 +1,38 @@
|
||||
import asyncio
|
||||
import yaml
|
||||
import telegram
|
||||
import re
|
||||
import html
|
||||
import telegram.ext
|
||||
|
||||
ITEM_TEXT = "- *Artículo*: {}\n" \
|
||||
"- *Descripción*: {}\n" \
|
||||
"- *Localidad*: {}\n" \
|
||||
"- *Precio*: {} {}\n" \
|
||||
"- *Acepta envíos*: {}\n" \
|
||||
"[Ir al anuncio](https://es.wallapop.com/item/{})"
|
||||
ITEM_HTML = """
|
||||
<b>Resultados para:</b> {search_name}
|
||||
<b>Artículo:</b> {title}
|
||||
|
||||
{description}
|
||||
|
||||
<b>Localidad:</b> {location}
|
||||
<b>Acepta envíos:</b> {shipping}
|
||||
<b>Modificado el:</b> {modified_at}
|
||||
|
||||
<b>{price} {currency}</b>
|
||||
|
||||
<a href="https://es.wallapop.com/item/{url}">Ir al anuncio</a>
|
||||
"""
|
||||
|
||||
class TelegramManager:
|
||||
def __init__(self):
|
||||
token, channel = self.get_config()
|
||||
token, channel, chat_id = self.get_config()
|
||||
self._channel = channel
|
||||
self._bot = telegram.Bot(token=token)
|
||||
self._chat_id = chat_id
|
||||
# Use ApplicationBuilder to create the bot application with increased timeouts
|
||||
self._application = telegram.ext.ApplicationBuilder() \
|
||||
.token(token) \
|
||||
.connect_timeout(60) \
|
||||
.read_timeout(60) \
|
||||
.write_timeout(60) \
|
||||
.build()
|
||||
self._bot = self._application.bot
|
||||
|
||||
self._loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(self._loop)
|
||||
|
||||
@@ -25,21 +42,47 @@ class TelegramManager:
|
||||
config = yaml.safe_load(file)
|
||||
token = config['telegram_token']
|
||||
telegram_channel = config['telegram_channel']
|
||||
return token, telegram_channel
|
||||
telegram_chat_id = config['telegram_chat_id']
|
||||
return token, telegram_channel, telegram_chat_id
|
||||
|
||||
|
||||
def escape_markdown(self, text):
|
||||
special_chars = r'_[\]()~`>#\+\-=|{}.!]'
|
||||
escaped_text = re.sub(f'([{re.escape(special_chars)}])', r'\\\1', text)
|
||||
return escaped_text
|
||||
def escape_html(self, text):
|
||||
return html.escape(str(text))
|
||||
|
||||
def send_telegram_article(self, article):
|
||||
self._loop.run_until_complete(self.send_telegram_article_async(article))
|
||||
def send_telegram_article(self, search_name, article):
|
||||
self._loop.run_until_complete(self.send_telegram_article_async(search_name, article))
|
||||
|
||||
async def send_telegram_article_async(self, article):
|
||||
message = ITEM_TEXT.format(article.get_title(), self.escape_markdown(article.get_description()),
|
||||
self.escape_markdown(article.get_location()), article.get_price(),
|
||||
article.get_currency(), article.get_allows_shipping(),
|
||||
article.get_url())
|
||||
escaped_message = self.escape_markdown(message)
|
||||
await self._bot.send_message(self._channel, text=escaped_message, parse_mode="MarkdownV2")
|
||||
async def send_telegram_article_async(self, search_name, article):
|
||||
message = ITEM_HTML.format(
|
||||
search_name=self.escape_html(search_name),
|
||||
title=self.escape_html(article.get_title()),
|
||||
description=self.escape_html(article.get_description()),
|
||||
location=self.escape_html(article.get_location()),
|
||||
price=self.escape_html(article.get_price()),
|
||||
currency=self.escape_html(article.get_currency()),
|
||||
shipping=self.escape_html(article.get_allows_shipping()),
|
||||
modified_at=self.escape_html(article.get_modified_at()),
|
||||
url=self.escape_html(article.get_url())
|
||||
)
|
||||
images_url = article.get_images()
|
||||
|
||||
media = []
|
||||
for idx, image_url in enumerate(images_url):
|
||||
if idx == 0:
|
||||
media.append(
|
||||
telegram.InputMediaPhoto(
|
||||
media=image_url,
|
||||
caption=message,
|
||||
parse_mode="HTML"
|
||||
)
|
||||
)
|
||||
else:
|
||||
media.append(
|
||||
telegram.InputMediaPhoto(
|
||||
media=image_url
|
||||
)
|
||||
)
|
||||
|
||||
await self._bot.send_media_group(
|
||||
chat_id=self._chat_id,
|
||||
media=media
|
||||
)
|
||||
Reference in New Issue
Block a user