Refactoring
This commit is contained in:
parent
2b541fde2c
commit
0c047fbaa8
5 changed files with 113 additions and 126 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
env/
|
env/
|
||||||
.ini
|
*.ini
|
||||||
|
__pycache__/
|
|
@ -1,16 +0,0 @@
|
||||||
astroid==2.4.2
|
|
||||||
isort==4.3.21
|
|
||||||
lazy-object-proxy==1.4.3
|
|
||||||
mccabe==0.6.1
|
|
||||||
prompt-toolkit==1.0.14
|
|
||||||
pyconfigstore3==1.0.1
|
|
||||||
pyfiglet==0.8.post1
|
|
||||||
Pygments==2.6.1
|
|
||||||
PyInquirer==1.0.3
|
|
||||||
pylint==2.5.3
|
|
||||||
regex==2020.6.8
|
|
||||||
six==1.15.0
|
|
||||||
termcolor==1.1.0
|
|
||||||
toml==0.10.1
|
|
||||||
wcwidth==0.2.5
|
|
||||||
wrapt==1.12.1
|
|
0
src/__init__.py
Normal file
0
src/__init__.py
Normal file
26
src/cli.py
26
src/cli.py
|
@ -6,8 +6,6 @@ from pyconfigstore import ConfigStore
|
||||||
from PyInquirer import (Token, ValidationError, Validator, print_json, prompt,
|
from PyInquirer import (Token, ValidationError, Validator, print_json, prompt,
|
||||||
style_from_dict)
|
style_from_dict)
|
||||||
|
|
||||||
# from main import SteamGifts
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import colorama
|
import colorama
|
||||||
colorama.init()
|
colorama.init()
|
||||||
|
@ -56,7 +54,8 @@ def ask(type, name, message, validator=None, choices=[]):
|
||||||
return answers
|
return answers
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def run():
|
||||||
|
from main import SteamGifts as SG
|
||||||
|
|
||||||
def askCookie():
|
def askCookie():
|
||||||
cookie = ask(type='input',
|
cookie = ask(type='input',
|
||||||
|
@ -75,12 +74,14 @@ def main():
|
||||||
config.read('config.ini')
|
config.read('config.ini')
|
||||||
if not config['DEFAULT'].get('cookie'):
|
if not config['DEFAULT'].get('cookie'):
|
||||||
cookie = askCookie()
|
cookie = askCookie()
|
||||||
|
else:
|
||||||
re_enter_cookie = ask(type='confirm',
|
re_enter_cookie = ask(type='confirm',
|
||||||
name='reenter',
|
name='reenter',
|
||||||
message='Do you want to enter new cookie?')['reenter']
|
message='Do you want to enter new cookie?')['reenter']
|
||||||
if re_enter_cookie:
|
if re_enter_cookie:
|
||||||
cookie = askCookie()
|
cookie = askCookie()
|
||||||
|
else:
|
||||||
|
cookie = config['DEFAULT'].get('cookie')
|
||||||
|
|
||||||
gift_type = ask(type='list',
|
gift_type = ask(type='list',
|
||||||
name='gift_type',
|
name='gift_type',
|
||||||
|
@ -94,8 +95,9 @@ def main():
|
||||||
'New'
|
'New'
|
||||||
])['gift_type']
|
])['gift_type']
|
||||||
|
|
||||||
# s = SteamGifts(cookie, gift_type)
|
s = SG(cookie, gift_type)
|
||||||
# s.start()
|
s.start()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
run()
|
194
src/main.py
194
src/main.py
|
@ -4,129 +4,129 @@ import requests
|
||||||
import json
|
import json
|
||||||
import threading
|
import threading
|
||||||
|
|
||||||
|
from requests.adapters import HTTPAdapter
|
||||||
|
from urllib3.util import Retry
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from random import randint
|
from random import randint
|
||||||
from requests import RequestException
|
from requests import RequestException
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
CONFIG_DEFAULT = {
|
from cli import log
|
||||||
'cookie': 'Paste you cookie here',
|
|
||||||
'sleeptime': 900
|
|
||||||
}
|
|
||||||
|
|
||||||
def exitMessage(msg):
|
|
||||||
print(msg)
|
|
||||||
input()
|
|
||||||
sys.exit()
|
|
||||||
|
|
||||||
def readConfig():
|
|
||||||
config = configparser.ConfigParser()
|
|
||||||
|
|
||||||
def initConfig():
|
|
||||||
config['STEAMGIFTS'] = CONFIG_DEFAULT
|
|
||||||
with open('config.ini', 'w') as configfile:
|
|
||||||
config.write(configfile)
|
|
||||||
|
|
||||||
if not len(config.read('config.ini')):
|
|
||||||
initConfig()
|
|
||||||
exitMessage('Init file was created. Please, look into it and set up your cookie.')
|
|
||||||
elif list(CONFIG_DEFAULT.keys()) != list(config['STEAMGIFTS'].keys()):
|
|
||||||
initConfig()
|
|
||||||
exitMessage('Init file was reinitialised due to incorrect format. Please, look into it and set up your cookie.')
|
|
||||||
|
|
||||||
global timeout, cookies
|
|
||||||
timeout = config['STEAMGIFTS']['sleeptime']
|
|
||||||
cookies = {'PHPSESSID': config['STEAMGIFTS']['cookie']}
|
|
||||||
|
|
||||||
|
|
||||||
pages = 1
|
class SteamGifts:
|
||||||
|
def __init__(self, cookie, gifts_type):
|
||||||
|
self.cookie = {
|
||||||
|
'PHPSESSID': cookie
|
||||||
|
}
|
||||||
|
self.gifts_type = gifts_type
|
||||||
|
|
||||||
def get_soup_from_page(url):
|
self.base = "https://www.steamgifts.com"
|
||||||
r = requests.get(url, cookies=cookies)
|
self.session = requests.Session()
|
||||||
soup = BeautifulSoup(r.text, 'html.parser')
|
|
||||||
return soup
|
|
||||||
|
|
||||||
def get_page():
|
self.filter_url = {
|
||||||
global xsrf_token, points
|
'All': "search?page=%d",
|
||||||
|
'Wishlist': "search?page=%d&type=wishlist",
|
||||||
|
'Recommended': "search?page=%d&type=recommended",
|
||||||
|
'Copies': "search?page=%d©_min=2",
|
||||||
|
'DLC': "search?page=%d&dlc=true",
|
||||||
|
'New': "search?page=%d&type=new"
|
||||||
|
}
|
||||||
|
|
||||||
try:
|
def requests_retry_session(
|
||||||
soup = get_soup_from_page('https://www.steamgifts.com')
|
self,
|
||||||
|
retries=5,
|
||||||
|
backoff_factor=0.3
|
||||||
|
):
|
||||||
|
session = self.session or requests.Session()
|
||||||
|
retry = Retry(
|
||||||
|
total=retries,
|
||||||
|
read=retries,
|
||||||
|
connect=retries,
|
||||||
|
backoff_factor=backoff_factor,
|
||||||
|
status_forcelist=(500, 502, 504),
|
||||||
|
)
|
||||||
|
adapter = HTTPAdapter(max_retries=retry)
|
||||||
|
session.mount('http://', adapter)
|
||||||
|
session.mount('https://', adapter)
|
||||||
|
return session
|
||||||
|
|
||||||
xsrf_token = soup.find('input', {'name': 'xsrf_token'})['value']
|
def get_soup_from_page(self, url):
|
||||||
points = soup.find('span', {'class': 'nav__points'}).text # storage points
|
r = self.requests_retry_session().get(url)
|
||||||
except RequestException:
|
r = requests.get(url, cookies=self.cookie)
|
||||||
print('Cant connect to the site')
|
soup = BeautifulSoup(r.text, 'html.parser')
|
||||||
print('Waiting 2 minutes and reconnect...')
|
return soup
|
||||||
sleep(120)
|
|
||||||
get_page()
|
|
||||||
except TypeError:
|
|
||||||
print('Cant recognize your cookie value.')
|
|
||||||
sleep(30)
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
|
def update_info(self):
|
||||||
|
soup = self.get_soup_from_page(self.base)
|
||||||
|
|
||||||
# get codes of the games
|
self.xsrf_token = soup.find('input', {'name': 'xsrf_token'})['value']
|
||||||
def get_games():
|
self.points = int(soup.find('span', {'class': 'nav__points'}).text) # storage points
|
||||||
global game_name
|
|
||||||
global pages
|
|
||||||
|
|
||||||
n = 1
|
def get_game_content(self, page=1):
|
||||||
while n <= pages:
|
n = page
|
||||||
print('Proccessing games from %d page.' % n)
|
while True:
|
||||||
|
txt = "⚙️ Retrieving games from %d page." % n
|
||||||
|
log(txt, "magenta")
|
||||||
|
|
||||||
soup = get_soup_from_page('https://www.steamgifts.com/giveaways/search?page=' + str(n))
|
filtered_url = self.filter_url[self.gifts_type] % n
|
||||||
|
paginated_url = f"{self.base}/giveaways/{filtered_url}"
|
||||||
|
|
||||||
try:
|
soup = self.get_soup_from_page(paginated_url)
|
||||||
gifts_list = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['giveaway__row-inner-wrap'])
|
|
||||||
|
|
||||||
for item in gifts_list:
|
game_list = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['giveaway__row-inner-wrap'])
|
||||||
if int(points) == 0:
|
|
||||||
print('> Sleeping to get 6 points')
|
for item in game_list:
|
||||||
sleep(timeout)
|
if self.points == 0:
|
||||||
get_games()
|
log("🛋️ Sleeping to get 6 points", "yellow")
|
||||||
|
sleep(900)
|
||||||
|
self.get_game_content(page=n)
|
||||||
break
|
break
|
||||||
|
|
||||||
game_cost = item.find_all('span', {'class': 'giveaway__heading__thin'})
|
game_cost = item.find_all('span', {'class': 'giveaway__heading__thin'})[-1]
|
||||||
|
|
||||||
last_div = None
|
if game_cost:
|
||||||
for last_div in game_cost:
|
game_cost = game_cost.getText().replace('(', '').replace(')', '').replace('P', '')
|
||||||
pass
|
else:
|
||||||
if last_div:
|
|
||||||
game_cost = last_div.getText().replace('(', '').replace(')', '').replace('P', '')
|
|
||||||
|
|
||||||
game_name = item.find('a', {'class': 'giveaway__heading__name'}).text.encode('utf-8')
|
|
||||||
|
|
||||||
if int(points) - int(game_cost) < 0:
|
|
||||||
print('Not enough points to enter: ' + game_name)
|
|
||||||
continue
|
continue
|
||||||
elif int(points) - int(game_cost) > 0:
|
|
||||||
entry_gift(item.find('a', {'class': 'giveaway__heading__name'})['href'].split('/')[2])
|
game_name = item.find('a', {'class': 'giveaway__heading__name'}).text
|
||||||
|
|
||||||
|
if self.points - int(game_cost) < 0:
|
||||||
|
txt = f"⛔ Not enough points to enter: {game_name}"
|
||||||
|
log(txt, "red")
|
||||||
|
continue
|
||||||
|
|
||||||
|
elif self.points - int(game_cost) >= 0:
|
||||||
|
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}"
|
||||||
|
log(txt, "green")
|
||||||
|
# sleep(randint(10, 30))
|
||||||
|
|
||||||
n = n+1
|
n = n+1
|
||||||
except AttributeError:
|
|
||||||
break
|
|
||||||
|
|
||||||
print('List of games is ended. Waiting 2 min to update...')
|
|
||||||
sleep(120)
|
|
||||||
get_page()
|
|
||||||
get_games()
|
|
||||||
|
|
||||||
|
|
||||||
def entry_gift(code):
|
log("🛋️ List of games is ended. Waiting 2 min to update...", "yellow")
|
||||||
payload = {'xsrf_token': xsrf_token, 'do': 'entry_insert', 'code': code}
|
sleep(120)
|
||||||
entry = requests.post('https://www.steamgifts.com/ajax.php', data=payload, cookies=cookies)
|
self.start()
|
||||||
json_data = json.loads(entry.text)
|
|
||||||
|
|
||||||
get_page()
|
def entry_gift(self, game_id):
|
||||||
# print(json_data)
|
payload = {'xsrf_token': self.xsrf_token, 'do': 'entry_insert', 'code': game_id}
|
||||||
|
entry = requests.post('https://www.steamgifts.com/ajax.php', data=payload, cookies=self.cookie)
|
||||||
|
json_data = json.loads(entry.text)
|
||||||
|
|
||||||
# updating points after entered a giveaway
|
self.update_info()
|
||||||
if json_data['type'] == 'success':
|
|
||||||
print('> Bot has entered giveaway: ' + game_name.decode("utf-8"))
|
|
||||||
sleep(randint(10, 30))
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if json_data['type'] == 'success':
|
||||||
readConfig()
|
return True
|
||||||
|
|
||||||
get_page()
|
def start(self):
|
||||||
get_games()
|
self.update_info()
|
||||||
|
|
||||||
|
if self.points > 0:
|
||||||
|
txt = "🤖 Hoho! I am back! You have %d points. Lets hack." % self.points
|
||||||
|
log(txt, "blue")
|
||||||
|
|
||||||
|
self.get_game_content()
|
||||||
|
|
Loading…
Reference in a new issue