Now it doesn't crash anymore but it doesn't respond to other commands

This commit is contained in:
Stefano Assenzo 2022-10-01 09:44:54 +02:00
parent d0b082239b
commit 394add3c4b
1 changed files with 53 additions and 46 deletions

99
main.py
View File

@ -170,67 +170,74 @@ def a():
#schedule.every().thursday.do(freenow)
#Check for new games
url = "https://api.plenusbot.xyz/epic_games?country=IT"
response = requests.get(url).json()
try:
url = "https://api.plenusbot.xyz/epic_games?country=IT"
global response
response = requests.get(url).json()
# Title of current games
current_games_title1 = response['currentGames'][0]['title']
current_games_title2 = response['currentGames'][1]['title']
#Check first game
search_game1 = collection.find_one({"Game 1" : current_games_title1})
if search_game1 is None:
current_games_title1 = response['currentGames'][0]['title']
current_games_title2 = response['currentGames'][1]['title']
search_game1 = collection.find_one({"Game 1" : current_games_title1})
if search_game1 is None:
#Send notification if title game is changed
print("There is a new game!")
print("Now I'm sending the notification to everyone.")
send_automatically1()
else:
print("There is a new game!")
print("Now I'm sending the notification to everyone.")
send_automatically1()
else:
#If new game is changed recheck every 10 second
print("The game is not changed")
print("The game is not changed")
recheck_game()
except:
print("Ghe se un problema")
else:
recheck_game()
def send_automatically1():
logging.info("Triggered AUTOMATICALLY SEND MESSAGE.")
#Connect to the api
url = "https://api.plenusbot.xyz/epic_games?country=IT"
response = requests.get(url).json()
try:
url = "https://api.plenusbot.xyz/epic_games?country=IT"
global response
response = requests.get(url).json()
#All information for first game
current_games_title1 = response['currentGames'][0]['title'] # First title current games
current_games_description1 = response['currentGames'][0]['description'] # First description current games
current_games_startdate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate'] # Public release first game
current_games_endate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] # End public release first game
current_games_price1 = response['currentGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice']
current_games_images1 = response['currentGames'][0]['keyImages'][0]['url'] # First image current games
current_games_title1 = response['currentGames'][0]['title'] # First title current games
current_games_description1 = response['currentGames'][0]['description'] # First description current games
current_games_startdate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate'] # Public release first game
current_games_endate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] # End public release first game
current_games_price1 = response['currentGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice']
current_games_images1 = response['currentGames'][0]['keyImages'][0]['url'] # First image current games
#All information for second game
current_games_title2 = response['currentGames'][1]['title'] # Second title current game
current_games_images2 = response['currentGames'][1]['keyImages'][0]['url'] # Second image current game
current_games_description2 = response['currentGames'][1]['description'] # Second description current game
current_games_startdate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate'] #Public release second game
current_games_endate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] # End public release second game
current_games_price2 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] # Original price second game
#All information for second game
current_games_title2 = response['currentGames'][1]['title'] # Second title current game
current_games_images2 = response['currentGames'][1]['keyImages'][0]['url'] # Second image current game
current_games_description2 = response['currentGames'][1]['description'] # Second description current game
current_games_startdate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate'] #Public release second game
current_games_endate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] # End public release second game
current_games_price2 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] # Original price second game
with open('/app/data/readme.txt', 'r') as f:
for line in f:
y = line.split()
a = (', '.join(y))
with open('/app/data/readme.txt', 'r') as f:
for line in f:
y = line.split()
a = (', '.join(y))
#Send first and second message when the free game title change
title_description_1 = current_games_title1 + "\n\n<b>About:</b>\n" + current_games_description1 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate1 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate1 + "\n" + "\n<b>Price:</b>\n" + current_games_price1 + "" + "Free" # Send title, description, start date and price first current game
img_1 = bot.send_photo(a, current_games_images1) # Send image first current games
send_message = bot.send_message(a, title_description_1, parse_mode="HTML") # Send all
title_description_1 = current_games_title1 + "\n\n<b>About:</b>\n" + current_games_description1 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate1 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate1 + "\n" + "\n<b>Price:</b>\n" + current_games_price1 + "" + "Free" # Send title, description, start date and price first current game
img_1 = bot.send_photo(a, current_games_images1) # Send image first current games
send_message = bot.send_message(a, title_description_1, parse_mode="HTML") # Send all
title_description_2 = current_games_title2 + "\n\n<b>About:</b>\n" + current_games_description2 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate2 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate2 + "\n" + "\n<b>Price:</b>\n" + current_games_price2 + "" + "Free" # Send title, description, start date and price second current game
img_2 = bot.send_photo(a, current_games_images2) # Send image second current games
send_message = bot.send_message(a, title_description_2, parse_mode="HTML") # Send all
title_description_2 = current_games_title2 + "\n\n<b>About:</b>\n" + current_games_description2 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate2 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate2 + "\n" + "\n<b>Price:</b>\n" + current_games_price2 + "" + "Free" # Send title, description, start date and price second current game
img_2 = bot.send_photo(a, current_games_images2) # Send image second current games
send_message = bot.send_message(a, title_description_2, parse_mode="HTML") # Send all
#Update Collection with new game
send_game1 = {"Game 1": current_games_title1}
senddata = collection.update_one({'_id':ObjectId("6319beba54b4d66a40e2d3eb")}, {"$set": send_game1}, upsert=False)
#Update Collection with new game
send_game1 = {"Game 1": current_games_title1}
senddata = collection.update_one({'_id':ObjectId("6319beba54b4d66a40e2d3eb")}, {"$set": send_game1}, upsert=False)
send_game2 = {"Game 2": current_games_title2}
senddata = collection.update_one({'_id':ObjectId("6319c0ac2ffd38ae32cd9ffa")}, {"$set": send_game2}, upsert=False)
send_game2 = {"Game 2": current_games_title2}
senddata = collection.update_one({'_id':ObjectId("6319c0ac2ffd38ae32cd9ffa")}, {"$set": send_game2}, upsert=False)
except:
print("Ghe se un problema")
else:
recheck_game()
schedule.every(10).seconds.do(recheck_game)
bot.polling()