So now the bot can't crash

This commit is contained in:
Stefano Assenzo 2022-12-18 16:54:18 +00:00
parent b1540a2370
commit 2256b7b3c6
3 changed files with 88 additions and 45 deletions

View File

@ -3,48 +3,50 @@ import requests
def freegame_command(message, bot):
chat_id = message.chat.id #Get chat id
#Connect to API
url = "https://api.plenusbot.xyz/epic_games?country=IT" #URL API
response = requests.get(url).json() #API in JSON
#Take data from API (Current Game 1)
image_currentgames1 = response['currentGames'][0]['keyImages'][0]['url'] # First image current games
current_games1 = 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']
current_games_endate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price1 = response['currentGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice']
#Send notification to user (Current Game 1)
title_description_1 = current_games1 + "\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(message.chat.id, image_currentgames1) # Send image first current games
send_message = bot.send_message(chat_id, title_description_1, parse_mode="HTML") # Send all
try:
url = "https://api.plenusbot.xyz/epic_games?country=IT" #URL API
response = requests.get(url).json() #API in JSON
#Take data from API (Current Game 1)
image_currentgames1 = response['currentGames'][0]['keyImages'][0]['url'] # First image current games
current_games1 = 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']
current_games_endate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price1 = response['currentGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice']
#Send notification to user (Current Game 1)
title_description_1 = current_games1 + "\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(message.chat.id, image_currentgames1) # Send image first current games
send_message = bot.send_message(chat_id, title_description_1, parse_mode="HTML") # Send all
try:
#Take data from API (Current Game 2)
image_currentgames2 = response['currentGames'][1]['keyImages'][0]['url'] # First image current games
current_games2 = response['currentGames'][1]['title'] # First title current games
current_games_description2 = response['currentGames'][1]['description'] # First description current games
current_games_startdate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate']
current_games_endate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price2 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice']
try:
#Take data from API (Current Game 2)
image_currentgames2 = response['currentGames'][1]['keyImages'][0]['url'] # First image current games
current_games2 = response['currentGames'][1]['title'] # First title current games
current_games_description2 = response['currentGames'][1]['description'] # First description current games
current_games_startdate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate']
current_games_endate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price2 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice']
title_description_2 = current_games2 + "\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(message.chat.id, image_currentgames2) # Send image second current games
send_message = bot.send_message(chat_id, title_description_2, parse_mode="HTML") # Send all
title_description_2 = current_games2 + "\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(message.chat.id, image_currentgames2) # Send image second current games
send_message = bot.send_message(chat_id, title_description_2, parse_mode="HTML") # Send all
except:
print("Second current game not found")
#Try search third game
try:
#Take data from API (Current Game 3)
image_currentgames3 = response['currentGames'][2]['keyImages'][0]['url'] # First image current games
current_games3 = response['currentGames'][2]['title'] # First title current games
current_games_description3 = response['currentGames'][2]['description'] # First description current games
current_games_startdate3 = response['currentGames'][2]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate']
current_games_endate3 = response['currentGames'][2]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price3 = response['currentGames'][2]['price']['totalPrice']['fmtPrice']['originalPrice']
#Send notification to user (Current Game 3)
title_description_3 = current_games3 + "\n\n<b>About:</b>\n" + current_games_description3 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate3 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate3 + "\n" + "\n<b>Price:</b>\n" + current_games_price3 + "" + "Free" # Send title, description, start date and price second current game
img_3 = bot.send_photo(message.chat.id, image_currentgames3) # Send image second current games
send_message = bot.send_message(chat_id, title_description_3, parse_mode="HTML") # Send all
except IndexError:
print("No third game")
except:
print("Second current game not found")
#Try search third game
try:
#Take data from API (Current Game 3)
image_currentgames3 = response['currentGames'][2]['keyImages'][0]['url'] # First image current games
current_games3 = response['currentGames'][2]['title'] # First title current games
current_games_description3 = response['currentGames'][2]['description'] # First description current games
current_games_startdate3 = response['currentGames'][2]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate']
current_games_endate3 = response['currentGames'][2]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate']
current_games_price3 = response['currentGames'][2]['price']['totalPrice']['fmtPrice']['originalPrice']
#Send notification to user (Current Game 3)
title_description_3 = current_games3 + "\n\n<b>About:</b>\n" + current_games_description3 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate3 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate3 + "\n" + "\n<b>Price:</b>\n" + current_games_price3 + "" + "Free" # Send title, description, start date and price second current game
img_3 = bot.send_photo(message.chat.id, image_currentgames3) # Send image second current games
send_message = bot.send_message(chat_id, title_description_3, parse_mode="HTML") # Send all
except IndexError:
print("No third game")
bot.send_message(chat_id, "An error occurred")

View File

@ -17,16 +17,18 @@ def a(collection_game, collection_id, bot):
# Title of current games
try:
current_games_title1 = response['currentGames'][0]['title'] #Title of first game
search_game1 = collection_game.find_one({"Game 1" : current_games_title1}) #Search game1 in MongoDB
search_game1 = collection_game.find_one({"Game 1": current_games_title1}) #Search game1 in MongoDB
if search_game1 is None: #If game1 is not in MongoDB send notification
#Send notification if title game is changed
#Send notification if title game is changed
print("Found a new 1 game!")
print("Now I'm sending the notification to everyone.")
notification_game.notification_game1(collection_game, collection_id, bot)
else:
print("Current Game 1 not found")
except:
print("Current Game 1 not found")
recheck_game(collection_game, collection_id, bot)
#recheck_game(collection_game, collection_id, bot)
try:
current_games_title2 = response['currentGames'][1]['title'] #Title of second game
@ -38,6 +40,18 @@ def a(collection_game, collection_id, bot):
notification_game.notification_game2(collection_game, collection_id, bot)
except:
print("Current Game 2 not found")
#recheck_game(collection_game, collection_id, bot)
try:
current_games_title3 = response['currentGames'][2]['title']
search_game3 = collection_game.find_one({"Game 3" : current_games_title3})
if search_game3 is None: #If game2 is not in MongoDB send notification
#Send notification if title game is changed
print("Found a new 2 game!")
print("Now I'm sending the notification to everyone.")
notification_game.notification_game3(collection_game, collection_id, bot)
except:
print("Current Game 3 not found")
recheck_game(collection_game, collection_id, bot)
except:
print("An error occurred")

View File

@ -58,6 +58,33 @@ def notification_game2(collection_game, collection_id, bot,):
find_document_game2 = list(collection_game.find({}, {"_id": 1}))
array_game2 = find_document_game2[1]["_id"]
sendata2 = collection_game.update_one({'_id':ObjectId(array_game2)}, {"$set": send_game2}, upsert=False)
except:
print("An error occurred")
recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot)
def notification_game3(collection_game, collection_id, bot,):
try:
print("Try to send third game")
#All information for second game
current_games_title3 = response['currentGames'][1]['title'] # Third title current game
current_games_images3 = response['currentGames'][1]['keyImages'][0]['url'] # Third image current game
current_games_description3 = response['currentGames'][1]['description'] # Third description current game
current_games_startdate3 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['startDate'] #Public release Third game
current_games_endate3 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] # End public release Third game
current_games_price3 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] # Original price Third game
collection_find_username = list(collection_id.find({}, {"username": 1,})) #Find all username in collection
array_username = list(collection_find_username[1]['username']) #Get the array
for i in array_username:
title_description_3 = current_games_title3 + "\n\n<b>About:</b>\n" + current_games_description3 + "\n" + "\n<b>Start Date:</b>\n" + current_games_startdate3 + "\n" + "\n<b>End Date:</b>\n" + current_games_endate3 + "\n" + "\n<b>Price:</b>\n" + current_games_price3 + "" + "Free" # Send title, description, start date and price Third current game
img_3 = bot.send_photo(i, current_games_images3) # Send image Third current games
send_message = bot.send_message(i, title_description_3, parse_mode="HTML") # Send all
#Update Collection with new game
send_game3 = {"Game 3": current_games_title3}
find_document_game3 = list(collection_game.find({}, {"_id": 1}))
array_game3 = find_document_game3[2]["_id"]
sendata2 = collection_game.update_one({'_id':ObjectId(array_game3)}, {"$set": send_game3}, upsert=False)
except:
print("An error occurred")
recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot)