2022-05-09 14:01:03 +00:00
|
|
|
"""
|
|
|
|
script for sending daily challenges
|
|
|
|
"""
|
|
|
|
__author__ = "Florian Kellermann, Linus Eickhoff, Florian Kaiser"
|
|
|
|
__date__ = "09.05.2022"
|
|
|
|
__version__ = "0.0.1"
|
|
|
|
__license__ = "None"
|
|
|
|
|
|
|
|
|
2022-05-26 09:13:44 +00:00
|
|
|
import time
|
2022-05-09 14:01:03 +00:00
|
|
|
import sys
|
2022-05-09 16:02:17 +00:00
|
|
|
import pandas
|
2022-05-27 21:36:23 +00:00
|
|
|
import random
|
2022-05-28 09:44:23 +00:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
|
|
|
|
from bot import bot
|
2022-05-30 13:47:35 +00:00
|
|
|
from db import User, session, Product
|
2022-05-09 14:01:03 +00:00
|
|
|
|
2022-05-09 16:02:17 +00:00
|
|
|
CHALLENGE_READY = "0 8 * * *"
|
|
|
|
CHALLENGE_OVER = "0 22 * * *"
|
2022-05-27 21:36:23 +00:00
|
|
|
GET_NEW_PRODUCT = "0 0 * * *"
|
2022-05-09 14:01:03 +00:00
|
|
|
|
|
|
|
def start_challenges():
|
|
|
|
"""Start the daily challenges, read the crontag codes and send messages
|
|
|
|
"""
|
|
|
|
ready_split = CHALLENGE_READY.split(" ")
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
over_split = CHALLENGE_OVER.split(" ")
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-27 21:36:23 +00:00
|
|
|
new_product_split = GET_NEW_PRODUCT.split(" ")
|
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
my_scheduler = BackgroundScheduler()
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-27 21:36:23 +00:00
|
|
|
# Sending the current event at the given crontags
|
|
|
|
# Challenge ready
|
2022-05-09 14:01:03 +00:00
|
|
|
my_scheduler.add_job(send_current_event, 'cron'\
|
2022-05-27 21:36:23 +00:00
|
|
|
, day_of_week = ready_split[4] \
|
|
|
|
, hour= ready_split[1] \
|
|
|
|
, minute = ready_split[0]\
|
|
|
|
, month= ready_split[3] \
|
|
|
|
, day=ready_split[2]\
|
2022-05-09 14:01:03 +00:00
|
|
|
,args = ("start", ))
|
2022-05-27 21:36:23 +00:00
|
|
|
|
|
|
|
# Challenge over
|
2022-05-09 14:01:03 +00:00
|
|
|
my_scheduler.add_job(send_current_event, 'cron'\
|
2022-05-27 21:36:23 +00:00
|
|
|
, day_of_week = over_split[4] \
|
|
|
|
, hour= over_split[1] \
|
|
|
|
, minute = over_split[0]\
|
|
|
|
, month= over_split[3] \
|
|
|
|
, day=over_split[2]\
|
2022-05-09 14:01:03 +00:00
|
|
|
,args = ("over", ))
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-27 21:36:23 +00:00
|
|
|
# Get new product
|
2022-05-28 09:44:23 +00:00
|
|
|
my_scheduler.add_job(set_todays_product, 'cron'\
|
2022-05-27 21:36:23 +00:00
|
|
|
,day_of_week = new_product_split[4] \
|
|
|
|
, hour= new_product_split[1] \
|
|
|
|
, minute = new_product_split[0]\
|
|
|
|
, month= new_product_split[3]\
|
|
|
|
, day=new_product_split[2])
|
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
my_scheduler.start()
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
time.sleep( 3600 )
|
|
|
|
my_scheduler.shutdown()
|
|
|
|
start_challenges()
|
2022-05-26 09:13:44 +00:00
|
|
|
|
2022-05-28 09:44:23 +00:00
|
|
|
|
|
|
|
def set_todays_product():
|
2022-05-27 21:36:23 +00:00
|
|
|
"""Get a random product from the database
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Product: random product
|
|
|
|
"""
|
2022-05-28 09:44:23 +00:00
|
|
|
|
|
|
|
# Find old product and set todays_product false
|
|
|
|
|
2022-05-27 21:36:23 +00:00
|
|
|
products = []
|
|
|
|
for element in session.query(Product).all():
|
2022-05-28 09:44:23 +00:00
|
|
|
if element.todays_product:
|
|
|
|
element.todays_product = False
|
|
|
|
session.commit()
|
|
|
|
products.append(element)
|
|
|
|
else:
|
|
|
|
products.append(element)
|
2022-05-27 21:36:23 +00:00
|
|
|
|
|
|
|
# choose random product
|
|
|
|
random_product = products[int(len(products) * random.random())]
|
|
|
|
|
2022-05-28 09:44:23 +00:00
|
|
|
# find product_id in db and delete element
|
2022-05-28 15:37:10 +00:00
|
|
|
session.query(Product).filter(Product.product_id == random_product.product_id).update({'todays_product': True})
|
2022-05-28 09:44:23 +00:00
|
|
|
session.commit()
|
2022-05-09 14:01:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
def send_current_event(str_event):
|
|
|
|
"""Sending the current event at the given crontabs
|
|
|
|
|
|
|
|
Args:
|
|
|
|
str_event (String): event to send
|
|
|
|
"""
|
2022-05-09 16:02:17 +00:00
|
|
|
all_users = pandas.DataFrame(session.query(User.telegram_id).all())
|
2022-05-27 21:36:23 +00:00
|
|
|
|
2022-05-09 14:01:03 +00:00
|
|
|
if str_event == "start":
|
2022-05-09 16:02:17 +00:00
|
|
|
for element in all_users["telegram_id"]:
|
2022-05-27 21:36:23 +00:00
|
|
|
bot.send_message(chat_id=int(element), text="Todays challenge is available!\n"\
|
|
|
|
"Try /daily to give it a try :)")
|
2022-05-09 14:01:03 +00:00
|
|
|
elif str_event == "over":
|
2022-05-29 18:31:52 +00:00
|
|
|
product_today = find_todays_product_from_db()
|
2022-05-09 16:34:46 +00:00
|
|
|
for element in all_users["telegram_id"]:
|
2022-05-27 21:36:23 +00:00
|
|
|
bot.send_message(chat_id=int(element), text="Todays challenge is over!\n"\
|
2022-05-29 18:31:52 +00:00
|
|
|
"The correct price is: " + str(product_today.price) + "€\n"\
|
2022-05-27 21:36:23 +00:00
|
|
|
"Check the /scoreboard to see the leaderboard!")
|
2022-05-09 14:01:03 +00:00
|
|
|
else:
|
|
|
|
sys.exit(-1)
|
|
|
|
|
2022-05-29 18:31:52 +00:00
|
|
|
def find_todays_product_from_db():
|
|
|
|
"""Find todays product from db based on todays_product
|
|
|
|
"""
|
|
|
|
product = None
|
|
|
|
for element in session.query(Product).all():
|
|
|
|
if element.todays_product:
|
|
|
|
product = element
|
|
|
|
break
|
|
|
|
return product
|
2022-05-09 14:01:03 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-05-28 09:44:23 +00:00
|
|
|
set_todays_product()
|
2022-05-09 14:01:03 +00:00
|
|
|
try:
|
|
|
|
start_challenges()
|
|
|
|
sys.exit(-1)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print("Ending")
|
|
|
|
sys.exit(-1)
|