removed cli and added filtering
This commit is contained in:
parent
9e481fc2e3
commit
1cbe40c8c8
9 changed files with 149 additions and 167 deletions
76
src/main.py
76
src/main.py
|
@ -1,27 +1,27 @@
|
|||
import sys
|
||||
import configparser
|
||||
import requests
|
||||
import json
|
||||
import threading
|
||||
import re
|
||||
from random import randint
|
||||
from time import sleep
|
||||
|
||||
import requests
|
||||
from bs4 import BeautifulSoup
|
||||
from requests.adapters import HTTPAdapter
|
||||
from urllib3.util import Retry
|
||||
from time import sleep
|
||||
from random import randint
|
||||
from requests import RequestException
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
from cli import log
|
||||
import log
|
||||
|
||||
logger = log.get_logger(__name__)
|
||||
|
||||
class SteamGifts:
|
||||
def __init__(self, cookie, gifts_type, pinned, min_points):
|
||||
def __init__(self, cookie, gifts_type, pinned, min_points, max_entries, max_time_left):
|
||||
self.cookie = {
|
||||
'PHPSESSID': cookie
|
||||
}
|
||||
self.gifts_type = gifts_type
|
||||
self.pinned = pinned
|
||||
self.min_points = int(min_points)
|
||||
self.max_entries = int(max_entries)
|
||||
self.max_time_left = int(max_time_left)
|
||||
|
||||
self.base = "https://www.steamgifts.com"
|
||||
self.session = requests.Session()
|
||||
|
@ -66,15 +66,36 @@ class SteamGifts:
|
|||
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")
|
||||
logger.error("⛔ Cookie is not valid.")
|
||||
sleep(10)
|
||||
exit()
|
||||
|
||||
def determine_time_in_minutes(self, string_time):
|
||||
if not string_time:
|
||||
logger.error(f"Could not determine time from string {string_time}")
|
||||
return None
|
||||
match = re.search('(?P<number>[0-9]+) (?P<time_unit>(hour|day|minute|second))', string_time)
|
||||
if match:
|
||||
number = int(match.group('number'))
|
||||
time_unit = match.group('time_unit')
|
||||
if time_unit == 'hour':
|
||||
return number * 60
|
||||
elif time_unit == 'day':
|
||||
return number * 24 * 60
|
||||
elif time_unit == 'minute':
|
||||
return number
|
||||
elif time_unit == 'second':
|
||||
return 1
|
||||
else:
|
||||
return None
|
||||
else:
|
||||
return None
|
||||
|
||||
def get_game_content(self, page=1):
|
||||
n = page
|
||||
while True:
|
||||
txt = "⚙️ Retrieving games from %d page." % n
|
||||
log(txt, "magenta")
|
||||
logger.info(txt)
|
||||
|
||||
filtered_url = self.filter_url[self.gifts_type] % n
|
||||
paginated_url = f"{self.base}/giveaways/{filtered_url}"
|
||||
|
@ -86,7 +107,7 @@ class SteamGifts:
|
|||
if not len(game_list):
|
||||
random_seconds = randint(900, 1400)
|
||||
txt = f"We have run out of gifts to consider. Trying again in {random_seconds} seconds."
|
||||
log(txt, "yellow")
|
||||
logger.info(txt)
|
||||
sleep(random_seconds)
|
||||
self.start()
|
||||
continue
|
||||
|
@ -98,7 +119,7 @@ class SteamGifts:
|
|||
if self.points == 0 or self.points < self.min_points:
|
||||
random_seconds = randint(900, 1400)
|
||||
txt = f"🛋️ Sleeping {random_seconds} seconds to get more points. We have {self.points} points, but we need {self.min_points} to start."
|
||||
log(txt, "yellow")
|
||||
logger.info(txt)
|
||||
sleep(random_seconds)
|
||||
self.start()
|
||||
break
|
||||
|
@ -112,29 +133,38 @@ class SteamGifts:
|
|||
continue
|
||||
times = item.select('div span[data-timestamp]')
|
||||
game_remaining = times[0].text
|
||||
game_remaining_in_minutes = self.determine_time_in_minutes(game_remaining)
|
||||
game_created = times[1].text
|
||||
game_entries = item.select('div.giveaway__links span')[0].text
|
||||
game_created_in_minutes = self.determine_time_in_minutes(game_created)
|
||||
game_entries = int(item.select('div.giveaway__links span')[0].text.split(' ')[0].replace(',' ,''))
|
||||
|
||||
txt = f"{game_name} {game_cost} - {game_entries} - Created {game_created} ago with {game_remaining} remaining."
|
||||
log(txt, 'grey')
|
||||
logger.debug(txt)
|
||||
|
||||
if self.points - int(game_cost) < 0:
|
||||
txt = f"⛔ Not enough points to enter: {game_name}"
|
||||
log(txt, "red")
|
||||
logger.info(txt)
|
||||
continue
|
||||
|
||||
elif self.points - int(game_cost) >= 0:
|
||||
if self.max_time_left > game_remaining_in_minutes:
|
||||
txt = f"Game {game_name} has {game_remaining_in_minutes} left and is above your cutoff of {self.max_time_left} minutes."
|
||||
logger.info(txt)
|
||||
continue
|
||||
if self.max_entries > game_entries:
|
||||
txt = f"Game {game_name} has {game_entries} entries is above your cutoff of {self.max_entries} entries."
|
||||
logger.info(txt)
|
||||
continue
|
||||
# defensive move
|
||||
if self.points - int(game_cost) >= 0:
|
||||
res = self.entry_gift(game_id)
|
||||
if res:
|
||||
self.points -= int(game_cost)
|
||||
txt = f"🎉 One more game! Has just entered {game_name}"
|
||||
log(txt, "green")
|
||||
logger.info(txt)
|
||||
sleep(randint(3, 7))
|
||||
|
||||
n = n+1
|
||||
|
||||
|
||||
log("🛋️ List of games is ended. Waiting 2 mins to update...", "yellow")
|
||||
logger.info("🛋️ List of games is ended. Waiting 2 mins to update...")
|
||||
sleep(120)
|
||||
self.start()
|
||||
|
||||
|
@ -151,6 +181,6 @@ class SteamGifts:
|
|||
|
||||
if self.points > 0:
|
||||
txt = "🤖 Hoho! I am back! You have %d points. Lets hack." % self.points
|
||||
log(txt, "blue")
|
||||
logger.info(txt)
|
||||
|
||||
self.get_game_content()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue