I removed almost all ObjectIDs (just missing the /subscribe command)
This commit is contained in:
parent
af9c683380
commit
782445f50a
11
main.py
11
main.py
|
@ -13,9 +13,6 @@ load_dotenv() #Load .env file
|
|||
API_TOKEN = os.getenv('BOT_TOKEN') #Token for Telegram bot
|
||||
PASSWORD_MONGO = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
|
||||
URL_MONGO = os.getenv('URL_MONGODB') #URL for MongoDB
|
||||
OBJECTID_game1 = os.getenv('OBJECTID_game1') #ObjectID for game1 in MongoDB
|
||||
OBJECTID_game2 = os.getenv('OBJECTID_game2') #ObjectID for game2 in MongoDB
|
||||
OBJECTID_idlist = os.getenv('OBJECTID_idlist') #ObjectID for idlist in MongoDB
|
||||
|
||||
#Connect to MongoDB
|
||||
mongo_url = "mongodb+srv://stefano:" + urllib.parse.quote_plus(PASSWORD_MONGO) + URL_MONGO #URL for MongoDB (with password)
|
||||
|
@ -43,11 +40,11 @@ def freegame_command(message, bot=bot):
|
|||
|
||||
#Command /subscribe
|
||||
@bot.message_handler(commands=['subscribe'])
|
||||
def subscribe(message, bot=bot, collection_game=collection_game, OBJECTID_idlist=OBJECTID_idlist):
|
||||
command_subscribe.subscribe_command(message, bot, collection_game, OBJECTID_idlist)
|
||||
def subscribe(message, bot=bot, collection_game=collection_game):
|
||||
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):
|
||||
check_game.a(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2)
|
||||
def event_game(collection_game=collection_game, collection_id=collection_id, bot=bot):
|
||||
check_game.a(collection_game, collection_id, bot)
|
||||
|
||||
#Threading for check game every 5 seconds
|
||||
t1 = threading.Thread(target=event_game, args=())
|
||||
|
|
|
@ -1,14 +1,17 @@
|
|||
from bson.objectid import ObjectId
|
||||
import os
|
||||
|
||||
def subscribe_command(message, bot, collection_id, OBJECTID_idlist):
|
||||
def subscribe_command(message, bot, collection_id):
|
||||
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 #Get user id
|
||||
collection_find_username = list(collection_id.find({}, {"username": 1,})) #Find all username in collection
|
||||
print(collection_find_username)
|
||||
array_username = list(collection_find_username[0]["username"]) #Get just the array
|
||||
print(array_username)
|
||||
#Insert the id of the user who writes /subscribe in the database
|
||||
collection_id.update_one(
|
||||
{ "_id": ObjectId(OBJECTID_idlist)},
|
||||
{ "_id": ObjectId(array_username)},
|
||||
{
|
||||
"$push": { "username": take_id }
|
||||
}
|
||||
|
|
|
@ -3,12 +3,12 @@ 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):
|
||||
def recheck_game(collection_game, collection_id, bot):
|
||||
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)
|
||||
a(collection_game=collection_game, collection_id=collection_id, bot=bot)
|
||||
|
||||
#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):
|
||||
def a(collection_game, collection_id, bot):
|
||||
try:
|
||||
#Connect to API
|
||||
url = "https://api.plenusbot.xyz/epic_games?country=IT" #URL API
|
||||
|
@ -25,18 +25,18 @@ def a(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJE
|
|||
#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)
|
||||
notification_game.notification_game1(collection_game, collection_id, bot)
|
||||
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)
|
||||
notification_game.notification_game2(collection_game, collection_id, bot)
|
||||
else:
|
||||
#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)
|
||||
recheck_game(collection_game, collection_id, bot)
|
||||
except:
|
||||
print("An error occurred")
|
||||
recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2)
|
||||
recheck_game(collection_game, collection_id, bot)
|
||||
else:
|
||||
recheck_game(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2)
|
||||
recheck_game(collection_game, collection_id, bot)
|
|
@ -4,7 +4,7 @@ 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):
|
||||
def notification_game1(collection_game, collection_id, bot):
|
||||
try:
|
||||
print("Try to send first game")
|
||||
|
||||
|
@ -16,26 +16,25 @@ def notification_game1(collection_game, collection_id, bot, OBJECTID_idlist, OBJ
|
|||
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)
|
||||
collection_find_username = list(collection_id.find({}, {"username": 1,})) #Find all username in collection
|
||||
array_username = list(collection_find_username[0]['username']) #Get just the array
|
||||
|
||||
#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\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(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)
|
||||
find_document_game1 = list(collection_game.find({}, {"_id": 1}))
|
||||
array_game1 = find_document_game1[0]["_id"]
|
||||
sendata1 = collection_game.update_one({'_id':ObjectId(array_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)
|
||||
recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot)
|
||||
|
||||
def notification_game2(collection_game, collection_id, bot, OBJECTID_idlist, OBJECTID_game1, OBJECTID_game2):
|
||||
def notification_game2(collection_game, collection_id, bot,):
|
||||
try:
|
||||
print("Try to send second game")
|
||||
#All information for second game
|
||||
|
@ -46,11 +45,8 @@ 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)} #Take ObjectID for search ID user
|
||||
filter = {"_id": 0}
|
||||
collection_find = collection_id.find_one(query, filter)
|
||||
|
||||
array_username = collection_find["username"] #Get the array
|
||||
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_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(i, current_games_images2) # Send image second current games
|
||||
|
@ -58,8 +54,9 @@ def notification_game2(collection_game, collection_id, bot, OBJECTID_idlist, OBJ
|
|||
|
||||
#Update Collection with new game
|
||||
send_game2 = {"Game 2": current_games_title2}
|
||||
z = collection_game.update_one({'_id':ObjectId(OBJECTID_game2)}, {"$set": send_game2})
|
||||
|
||||
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, OBJECTID_idlist=OBJECTID_idlist, OBJECTID_game1 = OBJECTID_game1, OBJECTID_game2=OBJECTID_game2)
|
||||
recheck_game(collection_game=collection_game, collection_id=collection_id, bot=bot)
|
Loading…
Reference in New Issue