scheduling of the main giveaway code

This commit is contained in:
mcinj 2022-06-10 15:48:27 -04:00
parent 113e111e9d
commit 8dc2721efe
3 changed files with 75 additions and 51 deletions

View file

@ -1,14 +1,10 @@
import json
from random import randint
from time import sleep
import requests import requests
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from requests.adapters import HTTPAdapter from requests.adapters import HTTPAdapter
from urllib3.util import Retry from urllib3.util import Retry
from .database import GiveawayHelper
from .log import get_logger from .log import get_logger
from .database import NotificationHelper, GiveawayHelper
from .won_entry import WonEntry from .won_entry import WonEntry
logger = get_logger(__name__) logger = get_logger(__name__)
@ -85,6 +81,7 @@ class EvaluateWonGiveaways:
w = GiveawayHelper.get_by_giveaway_id(won_giveaway.giveaway_game_id) w = GiveawayHelper.get_by_giveaway_id(won_giveaway.giveaway_game_id)
logger.debug(f"Giveaway in db: {w}") logger.debug(f"Giveaway in db: {w}")
if w and not w.won and w.entered: if w and not w.won and w.entered:
logger.info(f"Found new won giveaway not already marked as won. Marking. {w}") logger.info(f"Marking {won_giveaway.game_name} as won.")
logger.debug(f"Marking: {w}")
GiveawayHelper.mark_game_as_won(won_giveaway.giveaway_game_id) GiveawayHelper.mark_game_as_won(won_giveaway.giveaway_game_id)

View file

@ -1,15 +1,12 @@
import datetime import datetime
import threading import threading
from datetime import timedelta, datetime from datetime import timedelta, datetime
from random import randint
from threading import Thread from threading import Thread
from time import sleep from time import sleep
from dateutil import tz from .enter_giveaways import EnterGiveaways
from .evaluate_won_giveaways import EvaluateWonGiveaways from .evaluate_won_giveaways import EvaluateWonGiveaways
from .log import get_logger from .log import get_logger
from .enter_giveaways import EnterGiveaways
from .scheduler import Scheduler from .scheduler import Scheduler
logger = get_logger(__name__) logger = get_logger(__name__)
@ -22,63 +19,71 @@ class GiveawayThread(threading.Thread):
self.exc = None self.exc = None
self.config = config self.config = config
self.notification = notification self.notification = notification
logger.debug("Creating scheduler")
self._scheduler = Scheduler()
self.won_giveaway_job_id = 'eval_won_giveaways'
self.evaluate_giveaway_job_id = 'eval_giveaways'
def run_steam_gifts(self, config, notification): if config['DEFAULT'].getboolean('enabled'):
cookie = config['DEFAULT'].get('cookie') cookie = config['DEFAULT'].get('cookie')
user_agent = config['DEFAULT'].get('user_agent') user_agent = config['DEFAULT'].get('user_agent')
main_page_enabled = config['DEFAULT'].getboolean('enabled')
minimum_points = config['DEFAULT'].getint('minimum_points')
max_entries = config['DEFAULT'].getint('max_entries')
max_time_left = config['DEFAULT'].getint('max_time_left')
minimum_game_points = config['DEFAULT'].getint('minimum_game_points')
blacklist = config['DEFAULT'].get('blacklist_keywords')
all_page = EnterGiveaways(cookie, user_agent, 'All', False, minimum_points, max_entries, minimum_points = config['DEFAULT'].getint('minimum_points')
max_time_left, minimum_game_points, blacklist, notification) max_entries = config['DEFAULT'].getint('max_entries')
max_time_left = config['DEFAULT'].getint('max_time_left')
minimum_game_points = config['DEFAULT'].getint('minimum_game_points')
blacklist = config['DEFAULT'].get('blacklist_keywords')
wishlist_page_enabled = config['WISHLIST'].getboolean('wishlist.enabled') self._all_page = EnterGiveaways(cookie, user_agent, 'All', False, minimum_points, max_entries,
wishlist_minimum_points = config['WISHLIST'].getint('wishlist.minimum_points') max_time_left, minimum_game_points, blacklist, notification)
wishlist_max_entries = config['WISHLIST'].getint('wishlist.max_entries')
wishlist_max_time_left = config['WISHLIST'].getint('wishlist.max_time_left')
wishlist_page = EnterGiveaways(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points, if config['WISHLIST'].getboolean('wishlist.enabled'):
wishlist_max_entries, wishlist_max_time_left, 0, '', notification) wishlist_minimum_points = config['WISHLIST'].getint('wishlist.minimum_points')
wishlist_max_entries = config['WISHLIST'].getint('wishlist.max_entries')
wishlist_max_time_left = config['WISHLIST'].getint('wishlist.max_time_left')
if not main_page_enabled and not wishlist_page_enabled: self._wishlist_page = EnterGiveaways(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points,
wishlist_max_entries, wishlist_max_time_left, 0, '', notification)
if not self._all_page and not self._wishlist_page:
logger.error("⁉️ Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...") logger.error("⁉️ Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...")
sleep(10) sleep(10)
exit(-1) exit(-1)
logger.debug("Creating scheduler") self._won_page_job_function = EvaluateWonGiveaways(cookie, user_agent, notification).start
scheduler = Scheduler() won_giveaway_job = self._scheduler.get_job(job_id=self.won_giveaway_job_id)
won_giveaway_job = scheduler.get_job(job_id='eval_giveaways')
if won_giveaway_job: if won_giveaway_job:
logger.debug("Previous won giveaway evaluator job exists. Removing.") logger.debug("Previous won giveaway evaluator job exists. Removing.")
won_giveaway_job.remove() won_giveaway_job.remove()
scheduler.add_job(EvaluateWonGiveaways(cookie, user_agent, notification).start, self._scheduler.add_job(self._won_page_job_function,
id='eval_giveaways', trigger='interval', max_instances=1, replace_existing=True, days=1, id=self.won_giveaway_job_id,
next_run_time=datetime.now() + timedelta(minutes=1)) trigger='interval',
scheduler.start() max_instances=1,
replace_existing=True,
hours=12,
next_run_time=datetime.now() + timedelta(minutes=5),
jitter=8000)
while True: evaluate_giveaway_job = self._scheduler.get_job(job_id=self.evaluate_giveaway_job_id)
logger.info("🟢 Evaluating giveaways.") if evaluate_giveaway_job:
if wishlist_page_enabled: logger.debug("Previous giveaway evaluator job exists. Removing.")
wishlist_page.start() evaluate_giveaway_job.remove()
if main_page_enabled: runner = GiveawayThread.GiveawayRunner(self._wishlist_page, self._all_page,
all_page.start() self.evaluate_giveaway_job_id)
self._scheduler.add_job(runner.run,
logger.info("🔴 All giveaways evaluated.") id=self.evaluate_giveaway_job_id,
random_seconds = randint(1740, 3540) # sometime between 29-59 minutes trigger='interval',
when_to_start_again = datetime.now(tz=tz.tzlocal()) + timedelta(seconds=random_seconds) max_instances=1,
logger.info(f"🛋 Going to sleep for {random_seconds / 60} minutes. " replace_existing=True,
f"Will start again at {when_to_start_again}") minutes=44,
sleep(random_seconds) next_run_time=datetime.now(),
jitter=900)
def run(self): def run(self):
# Variable that stores the exception, if raised by someFunction # Variable that stores the exception, if raised by someFunction
self.exc = None self.exc = None
try: try:
self.run_steam_gifts(self.config, self.notification) self._scheduler.start()
except BaseException as e: except BaseException as e:
self.exc = e self.exc = e
@ -89,3 +94,25 @@ class GiveawayThread(threading.Thread):
# if any was caught # if any was caught
if self.exc: if self.exc:
raise self.exc raise self.exc
class GiveawayRunner:
def __init__(self, wishlist_page, all_page, job_id):
self._wishlist_page = wishlist_page
self._all_page = all_page
self._job_id = job_id
def run(self):
logger.info("🟢 Evaluating giveaways.")
if self._wishlist_page:
self._wishlist_page.start()
if self._all_page:
self._all_page.start()
logger.info("🔴 All giveaways evaluated.")
scheduler = Scheduler()
evaluate_giveaway_job = scheduler.get_job(job_id=self._job_id)
if evaluate_giveaway_job:
when_to_start_again = evaluate_giveaway_job.next_run_time
logger.info(f"🛋 Going to sleep. Will start again at {when_to_start_again}")
else:
logger.info("No set time to evaluate giveaways again.")

View file

@ -1,7 +1,7 @@
import os import os
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.schedulers.background import BackgroundScheduler from apscheduler.schedulers.blocking import BlockingScheduler
from .log import get_logger from .log import get_logger
@ -18,7 +18,7 @@ class Scheduler:
'coalesce': True, 'coalesce': True,
'max_instances': 3 'max_instances': 3
} }
self.scheduler = BackgroundScheduler(jobstores=jobstores, job_defaults=job_defaults) self.scheduler = BlockingScheduler(jobstores=jobstores, job_defaults=job_defaults)
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self.scheduler, name) return getattr(self.scheduler, name)