diff --git a/config/config.ini.example b/config/config.ini.example index a5a68fc..90bd411 100644 --- a/config/config.ini.example +++ b/config/config.ini.example @@ -9,15 +9,22 @@ max_entries = 2000 # time left in minutes of a giveaway for it to be considered max_time_left = 300 # the minimum point value for a giveaway to be considered -minimum_game_points = 5 +minimum_game_points = 50 # a comma separated list of keywords in game titles to ignore blacklist_keywords = hentai,adult [WISHLIST] # should we consider giveaways on the 'Wishlist' page? wishlist.enabled = true # minimum number of points in your account before entering into giveaways -wishlist.minimum_points = 50 +wishlist.minimum_points = 1 # max number of entries in a giveaway for it to be considered -wishlist.max_entries = 2000 +wishlist.max_entries = 10000 # time left in minutes of a giveaway for it to be considered -wishlist.max_time_left = 300 \ No newline at end of file +wishlist.max_time_left = 300 +[NOTIFICATIONS] +# if we should send notifications via pushover +pushover.enabled = false +# your specific pushover token +pushover.token = +# your specific pushover user key +pushover.user_key = \ No newline at end of file diff --git a/src/ConfigReader.py b/src/ConfigReader.py index 8c13261..305c6ec 100644 --- a/src/ConfigReader.py +++ b/src/ConfigReader.py @@ -4,6 +4,7 @@ import log logger = log.get_logger(__name__) + class ConfigException(Exception): pass @@ -25,6 +26,9 @@ class ConfigReader(ConfigParser): 'wishlist.minimum_points': '%s' % (value_range(0, 400)), 'wishlist.max_entries': '%s' % (value_range(0, 100000)), 'wishlist.max_time_left': '%s' % (value_range(0, 21600)) + }, + 'NOTIFICATIONS': { + 'pushover.enabled': ('true', 'false'), } } default_values = { @@ -34,14 +38,20 @@ class ConfigReader(ConfigParser): 'minimum_points': f"{randint(20, 100)}", 'max_entries': f"{randint(1000, 2500)}", 'max_time_left': f"{randint(180,500)}", - 'minimum_game_points': '1', + 'minimum_game_points': f"{randint(20, 100)}", 'blacklist_keywords': 'hentai,adult' }, 'WISHLIST': { 'wishlist.enabled': 'true', - 'wishlist.minimum_points': f"{randint(20, 100)}", + 'wishlist.minimum_points': '1', 'wishlist.max_entries': f"{randint(10000, 100000)}", 'wishlist.max_time_left': f"{randint(180,500)}" + }, + 'NOTIFICATIONS': { + 'pushover.enabled': 'false', + 'pushover.token': '', + 'pushover.user_key': '', + } } deprecated_values = { diff --git a/src/SteamGifts.py b/src/SteamGifts.py index fe4a979..62d0681 100644 --- a/src/SteamGifts.py +++ b/src/SteamGifts.py @@ -13,9 +13,15 @@ from giveaway import Giveaway logger = log.get_logger(__name__) +class SteamGiftsException(Exception): + pass + + class SteamGifts: def __init__(self, cookie, gifts_type, pinned, min_points, max_entries, - max_time_left, minimum_game_points, blacklist): + max_time_left, minimum_game_points, blacklist, notification): + self.xsrf_token = None + self.points = None self.cookie = { 'PHPSESSID': cookie } @@ -26,6 +32,7 @@ class SteamGifts: self.max_time_left = int(max_time_left) self.minimum_game_points = int(minimum_game_points) self.blacklist = blacklist.split(',') + self.notification = notification self.base = "https://www.steamgifts.com" self.session = requests.Session() @@ -71,8 +78,7 @@ class SteamGifts: self.points = int(soup.find('span', {'class': 'nav__points'}).text) # storage points except TypeError: logger.error("⛔ Cookie is not valid.") - sleep(10) - exit() + raise SteamGiftsException("Cookie is not valid.") def should_we_enter_giveaway(self, giveaway): if giveaway.time_remaining_in_minutes is None: @@ -178,7 +184,6 @@ class SteamGifts: def start(self): self.update_info() - if self.points >= self.min_points: txt = f"🤖 You have {self.points} points. Evaluating '{self.gifts_type}' giveaways..." logger.info(txt) @@ -187,5 +192,3 @@ class SteamGifts: txt = f"You have {self.points} points which is below your minimum point threshold of " \ f"{self.min_points} points for '{self.gifts_type}' giveaways. Not evaluating right now." logger.info(txt) - - diff --git a/src/notification.py b/src/notification.py new file mode 100644 index 0000000..b110581 --- /dev/null +++ b/src/notification.py @@ -0,0 +1,42 @@ +import http.client +import urllib + +import log + +logger = log.get_logger(__name__) + + +class Notification: + + def __init__(self): + self.pushover = False + self.pushover_token = None + self.pushover_user_key = None + self.message_prefix = "SG-bot: " + + def send(self, message): + logger.debug(f"Attempting to notify: {message}") + if self.pushover: + logger.debug("Pushover enabled. Sending message.") + self.__pushover(message) + + def enable_pushover(self, token, user_key): + logger.debug("Enabling pushover notifications.") + self.pushover = True + self.pushover_token = token + self.pushover_user_key = user_key + + def __pushover(self, message): + conn = http.client.HTTPSConnection("api.pushover.net:443") + conn.request("POST", "/1/messages.json", + urllib.parse.urlencode({ + "token": self.pushover_token, + "user": self.pushover_user_key, + "message": f"{self.message_prefix}{message}", + }), {"Content-type": "application/x-www-form-urlencoded"}) + response = conn.getresponse() + logger.debug(f"Pushover response code: {response.getcode()}") + if response.getcode() != 200: + logger.error(f"Pushover notification failed. Code {response.getcode()}: {response.read().decode()}") + + diff --git a/src/run.py b/src/run.py index b655b6f..e74038b 100644 --- a/src/run.py +++ b/src/run.py @@ -3,7 +3,8 @@ from time import sleep import log from ConfigReader import ConfigReader, ConfigException -from SteamGifts import SteamGifts +from SteamGifts import SteamGifts, SteamGiftsException +from notification import Notification logger = log.get_logger(__name__) @@ -23,39 +24,58 @@ def run(): exit(-1) config.read(file_name) - cookie = config['DEFAULT'].get('cookie') - 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 = SteamGifts(cookie, 'All', False, minimum_points, max_entries, - max_time_left, minimum_game_points, blacklist) + notification = Notification() + pushover_enabled = config['NOTIFICATIONS'].getboolean('pushover.enabled') + pushover_token = config['NOTIFICATIONS'].get('pushover.token') + pushover_user_key = config['NOTIFICATIONS'].get('pushover.user_key') + if pushover_enabled: + notification.enable_pushover(pushover_token, pushover_user_key) - wishlist_enabled = config['WISHLIST'].getboolean('wishlist.enabled') - 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') + try: + cookie = config['DEFAULT'].get('cookie') - wishlist_page = SteamGifts(cookie, 'Wishlist', False, wishlist_minimum_points, - wishlist_max_entries, wishlist_max_time_left, 0, '') + 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') - if not enabled and not wishlist_enabled: - logger.error("Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...") - sleep(10) + all_page = SteamGifts(cookie, 'All', False, minimum_points, max_entries, + max_time_left, minimum_game_points, blacklist, notification) + + wishlist_enabled = config['WISHLIST'].getboolean('wishlist.enabled') + 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') + + wishlist_page = SteamGifts(cookie, 'Wishlist', False, wishlist_minimum_points, + wishlist_max_entries, wishlist_max_time_left, 0, '', notification) + + if not enabled and not wishlist_enabled: + logger.error("Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...") + sleep(10) + exit(-1) + + while True: + if wishlist_enabled: + wishlist_page.start() + if enabled: + all_page.start() + + random_seconds = randint(1740, 3540) # sometime between 29-59 minutes + logger.info(f"Going to sleep for {random_seconds / 60} minutes.") + sleep(random_seconds) + except SteamGiftsException as e: + notification.send(e) + sleep(5) + exit(-1) + except Exception as e: + logger.error(e) + notification.send("Something happened and the bot had to quit!") + sleep(5) exit(-1) - - while True: - if wishlist_enabled: - wishlist_page.start() - if enabled: - all_page.start() - - random_seconds = randint(1740, 3540) # sometime between 29-59 minutes - logger.info(f"Going to sleep for {random_seconds / 60} minutes.") - sleep(random_seconds) if __name__ == '__main__':