From ceb55bec68e4d5a24159f386f19e22679105dd84 Mon Sep 17 00:00:00 2001 From: mcinj <98779161+mcinj@users.noreply.github.com> Date: Fri, 13 May 2022 11:43:59 -0400 Subject: [PATCH] assigne user agent from list --- src/ConfigReader.py | 23 +++++++++++++++++++---- src/SteamGifts.py | 7 ++++--- src/run.py | 6 +++--- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/ConfigReader.py b/src/ConfigReader.py index 1f64ed4..2d4714c 100644 --- a/src/ConfigReader.py +++ b/src/ConfigReader.py @@ -1,5 +1,5 @@ from configparser import ConfigParser -from random import randint +from random import randint, randrange import log @@ -10,9 +10,23 @@ class ConfigException(Exception): pass +def value_range(min, max): + return [str(x) for x in [*range(min, max + 1)]] + + +def choose_user_agent(): + user_agents = [ + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:99.0) Gecko/20100101 Firefox/99.0', + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36', + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36', + 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.4 Safari/605.1.15', + 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 Safari/537.36' + ] + return user_agents[randrange(0, len(user_agents))] + class ConfigReader(ConfigParser): - def value_range(min, max): - return [str(x) for x in [*range(min, max + 1)]] + required_values = { 'DEFAULT': { @@ -35,6 +49,7 @@ class ConfigReader(ConfigParser): default_values = { 'DEFAULT': { 'cookie': '', + 'user_agent': f"{choose_user_agent()}", 'enabled': 'true', 'minimum_points': f"{randint(20, 50)}", 'max_entries': f"{randint(1000, 2500)}", @@ -107,4 +122,4 @@ class ConfigReader(ConfigParser): if self[section][key] not in values: raise ConfigException(( 'Invalid value for "%s" under section "%s" in ' + - 'the config file') % (key, section)) \ No newline at end of file + 'the config file') % (key, section)) diff --git a/src/SteamGifts.py b/src/SteamGifts.py index 659af0f..08c01e8 100644 --- a/src/SteamGifts.py +++ b/src/SteamGifts.py @@ -19,7 +19,7 @@ class SteamGiftsException(Exception): class SteamGifts: - def __init__(self, cookie, gifts_type, pinned, min_points, max_entries, + def __init__(self, cookie, user_agent, gifts_type, pinned, min_points, max_entries, max_time_left, minimum_game_points, blacklist, notification): self.contributor_level = None self.xsrf_token = None @@ -27,6 +27,7 @@ class SteamGifts: self.cookie = { 'PHPSESSID': cookie } + self.user_agent = user_agent self.gifts_type = gifts_type self.pinned = pinned self.min_points = int(min_points) @@ -68,7 +69,7 @@ class SteamGifts: def get_soup_from_page(self, url): headers = { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', + 'User-Agent': self.user_agent } self.requests_retry_session().get(url, headers=headers) r = requests.get(url, cookies=self.cookie) @@ -144,7 +145,7 @@ class SteamGifts: def enter_giveaway(self, giveaway): headers = { - 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36', + 'User-Agent': self.user_agent } payload = {'xsrf_token': self.xsrf_token, 'do': 'entry_insert', 'code': giveaway.giveaway_game_id} entry = requests.post('https://www.steamgifts.com/ajax.php', data=payload, cookies=self.cookie, diff --git a/src/run.py b/src/run.py index 3276ed4..c861b3e 100644 --- a/src/run.py +++ b/src/run.py @@ -34,7 +34,7 @@ def run(): try: cookie = config['DEFAULT'].get('cookie') - + 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') @@ -42,7 +42,7 @@ def run(): 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, + all_page = SteamGifts(cookie, user_agent, 'All', False, minimum_points, max_entries, max_time_left, minimum_game_points, blacklist, notification) wishlist_page_enabled = config['WISHLIST'].getboolean('wishlist.enabled') @@ -50,7 +50,7 @@ def run(): 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_page = SteamGifts(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points, wishlist_max_entries, wishlist_max_time_left, 0, '', notification) if not main_page_enabled and not wishlist_page_enabled: