From e1f01f63fcf7e02a49e841a8d4729384a1c64863 Mon Sep 17 00:00:00 2001 From: Stefano Assenzo Date: Sat, 10 Dec 2022 13:49:04 +0000 Subject: [PATCH] Thanks to ItzLightyHD I was able to put the bot on multiple files. Now I just have to clean up the code and finally put it on Docker. --- .vscode/settings.json | 6 + main.py | 252 +++--------------------------- src/commands/command_freegame.py | 41 +++++ src/commands/soon_command.py | 42 +++++ src/commands/start_command.py | 5 + src/commands/subscribe_command.py | 16 ++ src/commands/unsubscribe.py | 0 src/events/check_game.py | 42 +++++ src/events/logging_config.py | 0 src/events/notification_game.py | 66 ++++++++ 10 files changed, 238 insertions(+), 232 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 src/commands/command_freegame.py create mode 100644 src/commands/soon_command.py create mode 100644 src/commands/start_command.py create mode 100644 src/commands/subscribe_command.py create mode 100644 src/commands/unsubscribe.py create mode 100644 src/events/check_game.py create mode 100644 src/events/logging_config.py create mode 100644 src/events/notification_game.py diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8c265fb --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "python.analysis.extraPaths": [ + "./src/commands", + "./src/commands" + ] +} \ No newline at end of file diff --git a/main.py b/main.py index f3ec4d3..b87f274 100644 --- a/main.py +++ b/main.py @@ -1,36 +1,17 @@ #Libraries for Telegram bot +from src.commands import soon_command, subscribe_command, start_command, command_freegame +from src.events import check_game import telebot from telebot import telebot -#Libraries for logging on console -import logging -#Libraries for get BOT_TOKEN import os -#Boh -from dotenv import load_dotenv -#Libraries for use API -import requests -#Libraries for send message at a certain moment -import schedule -#Libraries for connect to Mongodb -import urllib -#Libraries for connect to Mongodb -import pymongo -#Libraries for search collection by Id -from bson.objectid import ObjectId -#Libraries for research game on db -import time -#Boh import sys +import urllib +import pymongo +from bson.objectid import ObjectId +from dotenv import load_dotenv import threading -logging.basicConfig( - level=logging.INFO, - format="%(asctime)s [%(levelname)s] %(message)s", - handlers=[ - logging.FileHandler("debug.log"), - logging.StreamHandler(sys.stdout) - ] -) + load_dotenv() API_TOKEN = os.getenv('BOT_TOKEN') PASSWORD_MONGO = os.getenv('PASSWORD_MONGODB') @@ -39,230 +20,37 @@ OBJECTID_game1 = os.getenv('OBJECTID_game1') OBJECTID_game2 = os.getenv('OBJECTID_game2') OBJECTID_idlist = os.getenv('OBJECTID_idlist') bot = telebot.TeleBot(API_TOKEN) - mongo_url = "mongodb+srv://stefano:" + urllib.parse.quote_plus(PASSWORD_MONGO) + URL_MONGO client = pymongo.MongoClient(mongo_url) database = client["epicgames-notifier"] collection_id = database["id-user"] collection_game = database["list-game"] -logging.info("The bot started successfully.") - #Command /start @bot.message_handler(commands=['start']) -def start_command(message): - chat_id = message.chat.id - logging.info("Triggered command START.") - 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") +def start(message, bot=bot): + start_command.start_command(message, bot) + #Command /comingsoon @bot.message_handler(commands=['comingsoon']) -def comingsoon(message): - logging.info("Triggered command COMING SOON.") - chat_id = message.chat.id - logging.info("Triggered command COMING SOON") - url = "https://api.plenusbot.xyz/epic_games?country=IT" - response = requests.get(url).json() - # Title of future games - 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'] - - 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'] - - try: - 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 - future_games_startdate3 = response['nextGames'][1]['promotions']['upcomingPromotionalOffers'][0]['promotionalOffers'][0]['startDate'] - future_games_price3 = response['nextGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] - title_description_3 = future_games3 + "\n\nAbout:\n" + future_games_description3 + "\n" + "\nStart Date:\n" + future_games_startdate3 + "\n" + "\nPrice:\n" + future_games_price3 + " ā†’ " + "Free" # Send title, description, start date and price second future game - img_3 = bot.send_photo(message.chat.id, image_futuregames3) # Send image third future games - send_message = bot.send_message(chat_id, title_description_3, parse_mode="HTML") # Send all - except IndexError: - print("No third game") - - #Send notification to user - 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 - - 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 +def comingsoon(message, bot=bot): + soon_command.soon_command(message, bot) #Command /freenow @bot.message_handler(commands=['freenow']) -def freenow(message): - logging.info("Triggered command FREENOW.") - chat_id = message.chat.id - url = "https://api.plenusbot.xyz/epic_games?country=IT" - response = requests.get(url).json() - 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'] - - 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'] - - #Send notification to user - 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 - - 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 - - try: - 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'] - 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 - except IndexError: - print("No third game") +def freegame_command(message, bot=bot): + command_freegame.freegame_command(message, bot) #Command /subscribe @bot.message_handler(commands=['subscribe']) -def subscribe(message): - logging.info("Triggered command SUBSCRIBE.") - #Register the user - global chat_id - chat_id = message.chat.id - send_message = bot.send_message(chat_id, "āœ… You have been successfully subscribed to Epic Games Store Free Games notifications!") - #Get the ids of all users who write /subscribe - take_id = message.from_user.id - collection_id.update_one( - { "_id": ObjectId(OBJECTID_idlist)}, - { - "$push": { "username": take_id } - } - ) +def subscribe(message, bot=bot, collection_game=collection_game): + subscribe_command.subscribe_command(message, bot, collection_game) -def recheck_game(): - logging.info("Triggered RECHECK GAME.") - time.sleep(10) - a() +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): + check_game.a(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) -#Function for send every week the notification -def a(): - logging.info("Triggered CHECK GAME.") - #Read all id on readme.txt - #schedule.every().thursday.do(freenow) - try: - #Check for new games - url = "https://api.plenusbot.xyz/epic_games?country=IT" - 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_game.find_one({"Game 1" : current_games_title1}) - print(search_game1) - 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: - #If new game is changed recheck every 10 second - print("The game is not changed") - recheck_game() - except: - print("An error occurred") - recheck_game() - else: - print("An error occurred") - recheck_game() - -def send_automatically1(): - logging.info("Triggered AUTOMATICALLY SEND MESSAGE.") - try: - print("Try to send first game") - #Connect to the api - url = "https://api.plenusbot.xyz/epic_games?country=IT" - 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 - - query = {"_id": ObjectId(OBJECTID_idlist)} - filter = {"_id": 0} - a = collection_id.find_one(query, filter) - - #Get just the array - b = a["username"] - for i in b: - #Send first and second message when the free game title change - title_description_1 = current_games_title1 + "\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(i, current_games_images1) # Send image first current games - send_message = bot.send_message(i, title_description_1, parse_mode="HTML") # Send all - - #Update Collection with new game - send_game1 = {"Game 1": current_games_title1} - x = collection_game.update_one({'_id':ObjectId(OBJECTID_game1)}, {"$set": send_game1}, upsert=False) - print("nice") - except: - print("An error occurred") - recheck_game() - else: - print("no ok") - try: - print("Try to send 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 - - query = {"_id": ObjectId(OBJECTID_idlist)} - filter = {"_id": 0} - a = collection_id.find_one(query, filter) - - #Get just the array - b = a["username"] - for i in b: - 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 - - #Update Collection with new game - send_game2 = {"Game 2": current_games_title2} - z = collection_game.update_one({'_id':ObjectId(OBJECTID_game2)}, {"$set": send_game2}) - - except: - print("An error occurred") - recheck_game() - -t1 = threading.Thread(target=recheck_game, args=()) +t1 = threading.Thread(target=event_game, args=()) t1.start() -bot.polling() \ No newline at end of file +bot.polling() diff --git a/src/commands/command_freegame.py b/src/commands/command_freegame.py new file mode 100644 index 0000000..608da70 --- /dev/null +++ b/src/commands/command_freegame.py @@ -0,0 +1,41 @@ +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() + 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'] + + 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'] + + #Send notification to user + 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 + + 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 + + try: + 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'] + 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 + except IndexError: + print("No third game") \ No newline at end of file diff --git a/src/commands/soon_command.py b/src/commands/soon_command.py new file mode 100644 index 0000000..4824ecd --- /dev/null +++ b/src/commands/soon_command.py @@ -0,0 +1,42 @@ +import requests + +def soon_command(message, bot): + chat_id = message.chat.id + try: + url = "https://api.plenusbot.xyz/epic_games?country=IT" + response = requests.get(url).json() + # Title of future games + 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'] + + 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'] + + 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 + + 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: + 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 + future_games_startdate3 = response['nextGames'][1]['promotions']['upcomingPromotionalOffers'][0]['promotionalOffers'][0]['startDate'] + future_games_price3 = response['nextGames'][1]['price']['totalPrice']['fmtPrice']['originalPrice'] + title_description_3 = future_games3 + "\n\nAbout:\n" + future_games_description3 + "\n" + "\nStart Date:\n" + future_games_startdate3 + "\n" + "\nPrice:\n" + future_games_price3 + " ā†’ " + "Free" # Send title, description, start date and price second future game + + img_3 = bot.send_photo(message.chat.id, image_futuregames3) # Send image third future games + 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/start_command.py b/src/commands/start_command.py new file mode 100644 index 0000000..2873810 --- /dev/null +++ b/src/commands/start_command.py @@ -0,0 +1,5 @@ +def start_command(message, bot): + print("Hei") + chat_id = message.chat.id + 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/subscribe_command.py b/src/commands/subscribe_command.py new file mode 100644 index 0000000..fc4e532 --- /dev/null +++ b/src/commands/subscribe_command.py @@ -0,0 +1,16 @@ +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!") + #Get the ids of all users who write /subscribe + take_id = message.from_user.id + collection_id.update_one( + { "_id": ObjectId(OBJECTID_idlist)}, + { + "$push": { "username": take_id } + } + ) \ No newline at end of file diff --git a/src/commands/unsubscribe.py b/src/commands/unsubscribe.py new file mode 100644 index 0000000..e69de29 diff --git a/src/events/check_game.py b/src/events/check_game.py new file mode 100644 index 0000000..9434911 --- /dev/null +++ b/src/events/check_game.py @@ -0,0 +1,42 @@ +from . import notification_game +import requests +import time + +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 +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() + + # Title of current games + current_games_title1 = response['currentGames'][0]['title'] + current_games_title2 = response['currentGames'][1]['title'] + + #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: + #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: + #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 + print("The game is not changed") + recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) + except: + print("An error occurred") + recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) + else: + recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2) \ No newline at end of file diff --git a/src/events/logging_config.py b/src/events/logging_config.py new file mode 100644 index 0000000..e69de29 diff --git a/src/events/notification_game.py b/src/events/notification_game.py new file mode 100644 index 0000000..7983a2f --- /dev/null +++ b/src/events/notification_game.py @@ -0,0 +1,66 @@ +import requests +import os +from bson.objectid import ObjectId +url = "https://api.plenusbot.xyz/epic_games?country=IT" +response = requests.get(url).json() + +def notification_game1(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1): + try: + print("Try to send first game") + + #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 + + query = {"_id": ObjectId(OBJECTID_idlist)} + filter = {"_id": 0} + collection_find_username = collection_id.find_one(query, filter) + + #Get just the array + array_username = collection_find_username["username"] + for i in array_username: + #Send first and second message when the free game title change + title_description_1 = current_games_title1 + "\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(i, current_games_images1) # Send image first current games + send_message = bot.send_message(i, title_description_1, parse_mode="HTML") # Send all + + #Update Collection with new game + send_game1 = {"Game 1": current_games_title1} + x = collection_game.update_one({'_id':ObjectId(OBJECTID_game1)}, {"$set": send_game1}, upsert=False) + except: + print("An error occurred") + recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot, OBJECTID_idlist=OBJECTID_idlist, OBJECTID_game1=OBJECTID_game1) + +def notification_game2(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2): + try: + print("Try to send 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 + + query = {"_id": ObjectId(OBJECTID_idlist)} + filter = {"_id": 0} + a = collection_id.find_one(query, filter) + + #Get just the array + b = a["username"] + for i in b: + 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 + + #Update Collection with new game + send_game2 = {"Game 2": current_games_title2} + z = collection_game.update_one({'_id':ObjectId(OBJECTID_game2)}, {"$set": send_game2}) + + except: + print("An error occurred") + recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot, OBJECTID_idlist=OBJECTID_idlist, OBJECTID_game1 = OBJECTID_game1, OBJECTID_game2=OBJECTID_game2) \ No newline at end of file