i need to understand why when i run this script this happens.
The script selects n number of words from the list of keywords and starts making the request to the API of the service and once it has obtained the answer it posts the result on telegram.
To make a new request to the API, it must wait a number of minutes specified in the PAUSE_MINUTES variable
The script pauses at the hours defined by the MIN_HOUR and variables MAX_HOUR
I would like that once made the request to the API and after waiting for the minutes I have established, it would make a new request with new keywords.
What happens instead is that if the first request was made with the word "Bosch", the following ones will also be made with this word.
In short, the script never leaves the loop.
How can I modify the script in such a way as to obtain a new keyword for each new request, respecting the waiting times between one call and another and the night sleep time?
from amazon_api import search_items
from create_messages import create_item_html
import time
from datetime import datetime
from itertools import chain
import random
# ***** Telegram TOKEN and parameters Definition *****
TOKEN = 'mytoken'
CHANNEL_NAME = '@mychannel'
# ***** BOT ACTIVITY TIME *****
MIN_HOUR = 8
MAX_HOUR = 24
PAUSE_MINUTES = 5
# ***** SEARCH CATEGORY DEFINITION *****
CATEGORY = "ToolsAndHomeImprovement"
# Create the bot instance
bot = telegram.Bot(token=TOKEN)
# Search keywords definition
keywords = ["Bosch", "Beta", "Dewalt", "USAG", "Karcher", "Bahco"]
#print (keywords)
# run bot function
def run_bot(bot, keywords):
random.shuffle(keywords)
while True:
try:
items_full = []
# shuffling keywords list
random.shuffle(keywords)
number_of_keywords = 1
for el in keywords[:number_of_keywords]:
print (keywords[:number_of_keywords:])
for i in range(1, 10):
items = search_items(el, "ToolsAndHomeImprovement", item_page=i)
time.sleep(1)
items_full.append(items)
items_full = list(chain(*items_full))
print(f'{5 * "*"} Requests Completed {5 * "*"}')
random.shuffle(items_full)
random.shuffle(items_full)
res = create_item_html(items_full)
while len(res) > 3:
now = datetime.now().time()
if MIN_HOUR - 2 < now.hour < MAX_HOUR - 2:
try:
print(f'{5*"*"} Sending posts to channel {5*"*"}')
bot.send_message(chat_id="@mychannel", text=res[0], reply_markup=res[1],parse_mode=telegram.ParseMode.HTML)
bot.send_message(chat_id="@mychannel", text=res[2], reply_markup=res[3],parse_mode=telegram.ParseMode.HTML)
res.pop(0)
res.pop(0)
res.pop(0)
res.pop(0)
except Exception as e:
print(e)
res.pop(0)
res.pop(0)
res.pop(0)
res.pop(0)
continue
time.sleep(60* PAUSE_MINUTES)
else:
print(f'{5 * "*"} Inactive Bot, between {MIN_HOUR}AM and {MAX_HOUR}PM {5 * "*"}')
time.sleep(60 * 5)
except Exception as e:
print(e)
# running bot
run_bot(bot, keywords)
Aucun commentaire:
Enregistrer un commentaire