diff --git a/wallamanta/helpers.py b/wallamanta/helpers.py index 1f09ddc..523d62a 100644 --- a/wallamanta/helpers.py +++ b/wallamanta/helpers.py @@ -3,9 +3,10 @@ import random import requests import logging import constants +import pytz from PIL import Image, ImageDraw, ImageFont -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application from telegram.constants import ParseMode @@ -113,10 +114,19 @@ def create_image(article): # guardamos la imagen con otro nombre image.save(f"/app/data/images/products/{article['id']}_composed.png", quality=95) +def get_publish_date(article): + article_date = article['created_at'] + return datetime.fromtimestamp(int(int(article_date)/1000)).astimezone(pytz.timezone("Europe/Madrid")).strftime("%d/%m/%Y - %H:%M:%S") + async def send_article(article, product): application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_TOKEN).build() create_image(article) - text = f"*{telegram_escape_characters(article['title'])}*\n\n*Descripción*: {telegram_escape_characters(article['description'])}\n\n*Encontrado por la búsqueda de:*{telegram_escape_characters(product['product_name'])}\n\n*Precio*: {telegram_escape_characters(str(article['price']))} {telegram_escape_characters(article['currency'])}" + title = f"*{telegram_escape_characters(article['title'])}*" + description = f"*Descripción*: {telegram_escape_characters(article['description'])}" + found_by = f"*Encontrado por la búsqueda de:* {telegram_escape_characters(product['product_name'])}" + created_at = f"*Fecha de publicación:* {telegram_escape_characters(get_publish_date(article))}" + price = f"*Precio*: {telegram_escape_characters(str(article['price']))} {telegram_escape_characters(article['currency'])}" + text = f"{title}\n\n{description}\n\n{found_by}\n\n{created_at}\n\n{price}" #url = f"https://api.telegram.org/bot{constants.TELEGRAM_TOKEN}/sendPhoto?chat_id={product['telegram_user_id']}&caption={text}&parse_mode=MarkdownV2" #files = {'photo':open(f"/app/data/images/products/{article['id']}_composed.png", 'rb')} keyboard = [[InlineKeyboardButton("Ir al anuncio", url=f"https://es.wallapop.com/item/{article['web_slug']}")]]