diff --git a/inventtesla/helpers.py b/inventtesla/helpers.py index 4fecd35..8972fa7 100644 --- a/inventtesla/helpers.py +++ b/inventtesla/helpers.py @@ -2,8 +2,8 @@ import time import requests import logging import constants +import json -from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram.ext import Application from telegram.constants import ParseMode @@ -63,7 +63,7 @@ def check_inventory(new_inventory, old_inventory): new_deals.append(new_car) return new_deals -async def send_deal(car, model): +def send_deal(car, model): application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_TOKEN).build() title = telegram_escape_characters(car['TrimName']) price = f"*💰 Precio:* {telegram_escape_characters(car['Price'])}€" @@ -84,11 +84,23 @@ async def send_deal(car, model): message = f"{title}\n\n{odometer}\n\n{options}\n\n{specs}\n\n{callouts}\n\n{price}" image = get_image(car['OptionCodeList'], model) - - keyboard = [[InlineKeyboardButton("Ir a la web", url=f"https://www.tesla.com/es_ES/{model}/order/{car['VIN']}")]] - markup = InlineKeyboardMarkup(keyboard) - await application.bot.send_photo(chat_id=constants.TELEGRAM_GROUP_ID, photo=image, caption=message, parse_mode=ParseMode.MARKDOWN_V2, reply_markup=markup) -async def send_message(telegram_user_id, message): - application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_TOKEN).build() - await application.bot.send_message(chat_id=telegram_user_id, text=message) + keyboard = {'inline_keyboard':[[{'text':'Ir a la web','url':f'https://www.tesla.com/es_ES/{model}/order/{car["VIN"]}'}]]} + + files = { + 'chat_id': (None, constants.TELEGRAM_GROUP_ID), + 'photo': image, + 'caption': (None, message), + 'parse_mode': (None, ParseMode.MARKDOWN_V2), + 'reply_markup': (None, json.dumps(keyboard)), + } + + response = requests.post(f'https://api.telegram.org/bot{constants.TELEGRAM_TOKEN}/sendPhoto', files=files) + logging.info(response) + +def send_message(telegram_user_id, message): + files = { + 'chat_id': (None, telegram_user_id), + 'text': (None, message), + } + requests.post(f'https://api.telegram.org/bot{constants.TELEGRAM_TOKEN}/sendMessage', files=files) \ No newline at end of file diff --git a/inventtesla/inventtesla.py b/inventtesla/inventtesla.py index 2080288..3937ffa 100644 --- a/inventtesla/inventtesla.py +++ b/inventtesla/inventtesla.py @@ -1,12 +1,7 @@ -import threading import logging import constants import inventtesla_checker -from telegram.ext import ( - Application -) - # Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO @@ -15,14 +10,7 @@ logging.basicConfig( logger = logging.getLogger(__name__) def main()->None: - p = threading.Thread(target=inventtesla_checker.inventtesla_checker, args=(constants.SLEEP_TIME, )) - p.start() - - """Start the bot.""" - # Create the Application and pass it your bot's token. - application = Application.builder().get_updates_http_version('1.1').http_version('1.1').token(constants.TELEGRAM_TOKEN).build() - - application.run_polling() + inventtesla_checker.work(constants.SLEEP_TIME) if __name__ == "__main__": main() \ No newline at end of file diff --git a/inventtesla/inventtesla_checker.py b/inventtesla/inventtesla_checker.py index 54584a5..6c192d9 100644 --- a/inventtesla/inventtesla_checker.py +++ b/inventtesla/inventtesla_checker.py @@ -1,4 +1,4 @@ -import asyncio +import time import logging import helpers import constants @@ -8,12 +8,14 @@ logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO ) -async def work(sleep_time): +def work(sleep_time): + logging.info(f"Tesla checker starting... Checking every {sleep_time} seconds") inventories = {} for model in constants.MODELS: inventories[model] = helpers.get_inventory(model) if type(inventories[model]) is list: - inventories[model].pop(0) + pass + #inventories[model].pop(0) else: inventories[model] = list() while True: @@ -25,16 +27,7 @@ async def work(sleep_time): inventories[model] = new_inventory for deal in new_deals: - await helpers.send_deal(deal, model) - await asyncio.sleep(5) + helpers.send_deal(deal, model) + time.sleep(5) - await asyncio.sleep(sleep_time) - -def inventtesla_checker(sleep_time): - logging.info(f"Tesla checker starting... Checking every {sleep_time} seconds") - while True: - loop = asyncio.new_event_loop() - asyncio.set_event_loop(loop) - loop.run_until_complete(work(sleep_time)) - loop.close() - + time.sleep(sleep_time)