From a420a498dd728e3a20558345a2483fef3a2837c2 Mon Sep 17 00:00:00 2001 From: stilmaniac Date: Sat, 27 Jun 2020 12:05:58 +0300 Subject: [PATCH] Add to configuration: minimum points and pinned games --- README.md | 4 ++-- src/cli.py | 30 +++++++++++++++++++++++++++--- src/main.py | 39 +++++++++++++++++++++++++++------------ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 673739b..e24df00 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![](https://i.imgur.com/dKA3udT.png) +![](https://i.imgur.com/oCob3wQ.gif) ### About The bot is specially designed for [SteamGifts.com](https://www.steamgifts.com/) @@ -22,7 +22,7 @@ The bot is specially designed for [SteamGifts.com](https://www.steamgifts.com/) python -m venv env source env/bin/activate pip install -r requirements.txt -python cli.py +python src/cli.py ``` ### Help diff --git a/src/cli.py b/src/cli.py index 2214754..bbd7f3d 100644 --- a/src/cli.py +++ b/src/cli.py @@ -1,10 +1,12 @@ import six import configparser +import re from pyfiglet import figlet_format from pyconfigstore import ConfigStore from PyInquirer import (Token, ValidationError, Validator, print_json, prompt, style_from_dict) +from prompt_toolkit import document try: import colorama @@ -37,13 +39,26 @@ def log(string, color, font="slant", figlet=False): six.print_(string) -def ask(type, name, message, validator=None, choices=[]): +class PointValidator(Validator): + def validate(self, document: document.Document): + value = document.text + try: + value = int(value) + except Exception: + raise ValidationError(message = 'Value should be greater than 0', cursor_position = len(document.text)) + + if value <= 0: + raise ValidationError(message = 'Value should be greater than 0', cursor_position = len(document.text)) + return True + + +def ask(type, name, message, validate=None, choices=[]): questions = [ { 'type': type, 'name': name, 'message': message, - 'validator': validator, + 'validate': validate, }, ] if choices: @@ -83,6 +98,10 @@ def run(): else: cookie = config['DEFAULT'].get('cookie') + pinned_games = ask(type='confirm', + name='pinned', + message='Should bot enter pinned games?')['pinned'] + gift_type = ask(type='list', name='gift_type', message='Select type:', @@ -95,7 +114,12 @@ def run(): 'New' ])['gift_type'] - s = SG(cookie, gift_type) + min_points = ask(type='input', + name='min_points', + message='Enter minimum points to start working (bot will try to enter giveaways until minimum value is reached):', + validate=PointValidator)['min_points'] + + s = SG(cookie, gift_type, pinned_games, min_points) s.start() diff --git a/src/main.py b/src/main.py index fa59e4c..4e0bcea 100644 --- a/src/main.py +++ b/src/main.py @@ -15,11 +15,13 @@ from cli import log class SteamGifts: - def __init__(self, cookie, gifts_type): + def __init__(self, cookie, gifts_type, pinned, min_points): self.cookie = { 'PHPSESSID': cookie } self.gifts_type = gifts_type + self.pinned = pinned + self.min_points = int(min_points) self.base = "https://www.steamgifts.com" self.session = requests.Session() @@ -60,8 +62,13 @@ class SteamGifts: def update_info(self): soup = self.get_soup_from_page(self.base) - self.xsrf_token = soup.find('input', {'name': 'xsrf_token'})['value'] - self.points = int(soup.find('span', {'class': 'nav__points'}).text) # storage points + try: + self.xsrf_token = soup.find('input', {'name': 'xsrf_token'})['value'] + self.points = int(soup.find('span', {'class': 'nav__points'}).text) # storage points + except TypeError: + log("⛔ Cookie is not valid.", "red") + sleep(10) + exit() def get_game_content(self, page=1): n = page @@ -74,13 +81,22 @@ class SteamGifts: soup = self.get_soup_from_page(paginated_url) - game_list = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['giveaway__row-inner-wrap']) + game_list = soup.find_all('div', {'class': 'giveaway__row-inner-wrap'}) + + if not len(game_list): + log("⛔ Page is empty. Please, select another type.", "red") + sleep(10) + exit() for item in game_list: - if self.points == 0: - log("🛋️ Sleeping to get 6 points", "yellow") + if len(item.get('class', [])) == 2 and not self.pinned: + continue + + if self.points == 0 or self.points < self.min_points: + txt = f"🛋️ Sleeping to get 6 points. We have {self.points} points, but we need {self.min_points} to start." + log(txt, "yellow") sleep(900) - self.get_game_content(page=n) + self.start() break game_cost = item.find_all('span', {'class': 'giveaway__heading__thin'})[-1] @@ -101,14 +117,15 @@ class SteamGifts: game_id = item.find('a', {'class': 'giveaway__heading__name'})['href'].split('/')[2] res = self.entry_gift(game_id) if res: - txt = f"🎉 One more game! Have just entered {game_name}" + self.points -= int(game_cost) + txt = f"🎉 One more game! Has just entered {game_name}" log(txt, "green") - # sleep(randint(10, 30)) + sleep(randint(3, 7)) n = n+1 - log("🛋️ List of games is ended. Waiting 2 min to update...", "yellow") + log("🛋️ List of games is ended. Waiting 2 mins to update...", "yellow") sleep(120) self.start() @@ -117,8 +134,6 @@ class SteamGifts: entry = requests.post('https://www.steamgifts.com/ajax.php', data=payload, cookies=self.cookie) json_data = json.loads(entry.text) - self.update_info() - if json_data['type'] == 'success': return True