Removed asyncio and telegram client, this doesnt need to listen for messages

This commit is contained in:
Joan
2023-08-16 15:08:43 +02:00
parent e191a5f400
commit 037907e331
3 changed files with 30 additions and 37 deletions

View File

@@ -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'])}"
@@ -85,10 +85,22 @@ async def send_deal(car, model):
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)
keyboard = {'inline_keyboard':[[{'text':'Ir a la web','url':f'https://www.tesla.com/es_ES/{model}/order/{car["VIN"]}'}]]}
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)
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)

View File

@@ -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()

View File

@@ -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)
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()
helpers.send_deal(deal, model)
time.sleep(5)
time.sleep(sleep_time)