From 18719f44356bf46baf7051b18c3d3e873d26cd7a Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sat, 10 Dec 2022 14:21:53 +0000 Subject: [PATCH] Now the code is clean and tidy --- main.py | 2 +- src/commands/command_freegame.py | 20 ++++++++++++++------ src/commands/command_soon.py | 15 +++++++++++---- src/commands/command_start.py | 4 ++-- src/commands/command_subscribe.py | 11 +++++------ src/events/check_game.py | 24 ++++++++++++------------ src/events/notification_game.py | 9 ++++----- 7 files changed, 49 insertions(+), 36 deletions(-) diff --git a/main.py b/main.py index 63e1aed..e998446 100644 --- a/main.py +++ b/main.py @@ -43,7 +43,7 @@ def freegame_command(message, bot=bot): #Command /subscribe @bot.message_handler(commands=['subscribe']) -def subscribe(message, bot=bot, collection_game=collection_game): +def subscribe(message, bot=bot, collection_game=collection_game, OBJECTID_idlist=OBJECTID_idlist): command_subscribe.subscribe_command(message, bot, collection_game) def event_game(collection_game=collection_game, collection_id=collection_id, bot=bot, OBJECTID_idlist=OBJECTID_idlist, OBJECTID_game1=OBJECTID_game1, OBJECTID_game2=OBJECTID_game2): diff --git a/src/commands/command_freegame.py b/src/commands/command_freegame.py index 608da70..7cc92fe 100644 --- a/src/commands/command_freegame.py +++ b/src/commands/command_freegame.py @@ -1,9 +1,12 @@ import requests def freegame_command(message, bot): - chat_id = message.chat.id - url = "https://api.plenusbot.xyz/epic_games?country=IT" - response = requests.get(url).json() + 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 @@ -11,6 +14,7 @@ def freegame_command(message, bot): current_games_endate1 = response['currentGames'][0]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] current_games_price1 = response['currentGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice'] + #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 @@ -18,24 +22,28 @@ def freegame_command(message, bot): current_games_endate2 = response['currentGames'][1]['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'] current_games_price2 = response['currentGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] - #Send notification to user + #Send notification to user (Current Game 1) title_description_1 = current_games1 + "\n\nAbout:\n" + current_games_description1 + "\n" + "\nStart Date:\n" + current_games_startdate1 + "\n" + "\nEnd Date:\n" + current_games_endate1 + "\n" + "\nPrice:\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 + #Send notification to user (Current Game 2) title_description_2 = current_games2 + "\n\nAbout:\n" + current_games_description2 + "\n" + "\nStart Date:\n" + current_games_startdate2 + "\n" + "\nEnd Date:\n" + current_games_endate2 + "\n" + "\nPrice:\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 + send_message = bot.send_message(chat_id, title_description_2, parse_mode="HTML") # Send all + #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\nAbout:\n" + current_games_description3 + "\n" + "\nStart Date:\n" + current_games_startdate3 + "\n" + "\nEnd Date:\n" + current_games_endate3 + "\n" + "\nPrice:\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 + send_message = bot.send_message(chat_id, title_description_3, parse_mode="HTML") # Send all except IndexError: print("No third game") \ No newline at end of file diff --git a/src/commands/command_soon.py b/src/commands/command_soon.py index 4824ecd..9aa33df 100644 --- a/src/commands/command_soon.py +++ b/src/commands/command_soon.py @@ -1,34 +1,41 @@ import requests def soon_command(message, bot): - chat_id = message.chat.id + chat_id = message.chat.id #Get chat id try: - url = "https://api.plenusbot.xyz/epic_games?country=IT" - response = requests.get(url).json() - # Title of future games + # Get data from API + url = "https://api.plenusbot.xyz/epic_games?country=IT" # URL API + response = requests.get(url).json() #API in JSON + + #Take data from API (Future Game 1) image_futuregames1 = response['nextGames'][0]['keyImages'][0]['url'] # First image future games future_games1 = response['nextGames'][0]['title'] # First title future games future_games_description1 = response['nextGames'][0]['description'] # First description future games future_games_startdate1 = response['nextGames'][0]['promotions']['upcomingPromotionalOffers'][0]['promotionalOffers'][0]['startDate'] future_games_price1 = response['nextGames'][0]['price']['totalPrice']['fmtPrice']['originalPrice'] + #Take data from API (Future Game 2) image_futuregames2 = response['nextGames'][1]['keyImages'][1]['url'] # Second image future games future_games2 = response['nextGames'][1]['title'] # Second title future games future_games_description2 = response['nextGames'][1]['description'] # Second description future games future_games_startdate2 = response['nextGames'][1]['promotions']['upcomingPromotionalOffers'][0]['promotionalOffers'][0]['startDate'] future_games_price2 = response['nextGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] + #Send notification to user (Future Game 1) title_description_1 = future_games1 + "\n\nAbout:\n" + future_games_description1 + "\n" + "\nStart Date:\n" + future_games_startdate1 + "\n" + "\nPrice:\n" + future_games_price1 + " → " + "Free" # Send title, description, start date and price first future game img_1 = bot.send_photo(message.chat.id, image_futuregames1) # Send image first future games send_message = bot.send_message(chat_id, title_description_1, parse_mode="HTML") # Send all + #Send notification to user (Future Game 2) title_description_2 = future_games2 + "\n\nAbout:\n" + future_games_description2 + "\n" + "\nStart Date:\n" + future_games_startdate2 + "\n" + "\nPrice:\n" + future_games_price2 + " → " + "Free" # Send title, description, start date and price second future game img_2 = bot.send_photo(message.chat.id, image_futuregames2) # Send image second future games send_message = bot.send_message(chat_id, title_description_2, parse_mode="HTML") # Send all except: bot.send_message(chat_id, "An error has occurred, probably new games have not been revealed or there are problems with the API. \nWe apologize for the inconvenience") + #Try search third game try: + #Take data from API (Future Game 3) image_futuregames3 = response['nextGames'][0]['keyImages'][2]['url'] # Third image future games future_games3 = response['nextGames'][2]['title'] # Third title future games future_games_description3 = response['nextGames'][2]['description'] # Third description future games diff --git a/src/commands/command_start.py b/src/commands/command_start.py index 2873810..183e99e 100644 --- a/src/commands/command_start.py +++ b/src/commands/command_start.py @@ -1,5 +1,5 @@ def start_command(message, bot): - print("Hei") - chat_id = message.chat.id + chat_id = message.chat.id #Get chat id + #Send message messageText = "✋ Welcome to Epic Games Notifier\n\n📱 You will be notified every week when there are new free games in the Epic Games Store. \n\n👨‍💻 Created and developed by @Stef58_Official" bot.send_message(chat_id, messageText, parse_mode="HTML") \ No newline at end of file diff --git a/src/commands/command_subscribe.py b/src/commands/command_subscribe.py index fc4e532..b2730e3 100644 --- a/src/commands/command_subscribe.py +++ b/src/commands/command_subscribe.py @@ -1,13 +1,12 @@ from bson.objectid import ObjectId import os -OBJECTID_idlist = os.getenv('OBJECTID_idlist') -def subscribe_command(message, bot, collection_id): - #Register the user - chat_id = message.chat.id - send_message = bot.send_message(chat_id, "✅ You have been successfully subscribed to Epic Games Store Free Games notifications!") +def subscribe_command(message, bot, collection_id, OBJECTID_idlist): + chat_id = message.chat.id #Get chat id + send_message = bot.send_message(chat_id, "✅ You have been successfully subscribed to Epic Games Store Free Games notifications!") #Send message #Get the ids of all users who write /subscribe - take_id = message.from_user.id + take_id = message.from_user.id #Get user id + #Insert the id of the user who writes /subscribe in the database collection_id.update_one( { "_id": ObjectId(OBJECTID_idlist)}, { diff --git a/src/events/check_game.py b/src/events/check_game.py index 9434911..abb577d 100644 --- a/src/events/check_game.py +++ b/src/events/check_game.py @@ -2,37 +2,37 @@ from . import notification_game import requests import time +#Function that waits 10 seconds before trying again def recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2): time.sleep(10) a(collection_game=collection_game, collection_id=collection_id, bot=bot, OBJECTID_idlist=OBJECTID_idlist, OBJECTID_game1=OBJECTID_game1, OBJECTID_game2=OBJECTID_game2) -#Function for send every week the notification +#Function for check new games, if there are a new game, send notification def a(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2): try: - #Check for new games - url = "https://api.plenusbot.xyz/epic_games?country=IT" - response = requests.get(url).json() + #Connect to API + url = "https://api.plenusbot.xyz/epic_games?country=IT" #URL API + response = requests.get(url).json() #API in JSON # Title of current games - current_games_title1 = response['currentGames'][0]['title'] - current_games_title2 = response['currentGames'][1]['title'] + current_games_title1 = response['currentGames'][0]['title'] #Title of first game + current_games_title2 = response['currentGames'][1]['title'] #Title of second game #Check first game - search_game1 = collection_game.find_one({"Game 1" : current_games_title1}) - search_game2 = collection_game.find_one({"Game 2" : current_games_title2}) - print(search_game1) - if search_game1 is None: + search_game1 = collection_game.find_one({"Game 1" : current_games_title1}) #Search game1 in MongoDB + search_game2 = collection_game.find_one({"Game 2" : current_games_title2}) #Search game2 in MongoDB + if search_game1 is None: #If game1 is not in MongoDB send notification #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, OBJECTID_idlist, OBJECTID_game1) - if search_game2 is None: + if search_game2 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_game2(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) else: - #If new game is changed recheck every 10 second + #If new game is not changed recheck every 10 second print("The game is not changed") recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) except: diff --git a/src/events/notification_game.py b/src/events/notification_game.py index 7983a2f..04b5018 100644 --- a/src/events/notification_game.py +++ b/src/events/notification_game.py @@ -46,13 +46,12 @@ def notification_game2(collection_game, collection_id, bot, OBJECTID_idlist, OBJ 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 - query = {"_id": ObjectId(OBJECTID_idlist)} + query = {"_id": ObjectId(OBJECTID_idlist)} #Take ObjectID for search ID user filter = {"_id": 0} - a = collection_id.find_one(query, filter) + collection_find = collection_id.find_one(query, filter) - #Get just the array - b = a["username"] - for i in b: + array_username = collection_find["username"] #Get the array + for i in array_username: title_description_2 = current_games_title2 + "\n\nAbout:\n" + current_games_description2 + "\n" + "\nStart Date:\n" + current_games_startdate2 + "\n" + "\nEnd Date:\n" + current_games_endate2 + "\n" + "\nPrice:\n" + current_games_price2 + " → " + "Free" # Send title, description, start date and price second current game img_2 = bot.send_photo(i, current_games_images2) # Send image second current games send_message = bot.send_message(i, title_description_2, parse_mode="HTML") # Send all