Reorganise project dir

This commit is contained in:
stilmaniac 2020-06-26 22:24:52 +03:00
parent 927f2d568c
commit 5b82582ca1
4 changed files with 3 additions and 1 deletions

101
src/cli.py Normal file
View file

@ -0,0 +1,101 @@
import six
import configparser
from pyfiglet import figlet_format
from pyconfigstore import ConfigStore
from PyInquirer import (Token, ValidationError, Validator, print_json, prompt,
style_from_dict)
# from main import SteamGifts
try:
import colorama
colorama.init()
except ImportError:
colorama = None
try:
from termcolor import colored
except ImportError:
colored = None
config = configparser.ConfigParser()
style = style_from_dict({
Token.QuestionMark: '#fac731 bold',
Token.Answer: '#4688f1 bold',
Token.Selected: '#0abf5b', # default
Token.Pointer: '#673ab7 bold',
})
def log(string, color, font="slant", figlet=False):
if colored:
if not figlet:
six.print_(colored(string, color))
else:
six.print_(colored(figlet_format(
string, font=font), color))
else:
six.print_(string)
def ask(type, name, message, validator=None, choices=[]):
questions = [
{
'type': type,
'name': name,
'message': message,
'validator': validator,
},
]
if choices:
questions[0].update({
'choices': choices,
})
answers = prompt(questions, style=style)
return answers
def main():
def askCookie():
cookie = ask(type='input',
name='cookie',
message='Enter PHPSESSID cookie (Only needed to provide once):')
config['DEFAULT']['cookie'] = cookie['cookie']
with open('config.ini', 'w') as configfile:
config.write(configfile)
return cookie['cookie']
log("SteamGifts Bot", color="blue", figlet=True)
log("Welcome to SteamGifts Bot!", "green")
log("Created by: github.com/stilManiac", "white")
config.read('config.ini')
if not config['DEFAULT'].get('cookie'):
cookie = askCookie()
re_enter_cookie = ask(type='confirm',
name='reenter',
message='Do you want to enter new cookie?')['reenter']
if re_enter_cookie:
cookie = askCookie()
gift_type = ask(type='list',
name='gift_type',
message='Select type:',
choices=[
'All',
'Wishlist',
'Recommended',
'Copies',
'DLC',
'New'
])['gift_type']
# s = SteamGifts(cookie, gift_type)
# s.start()
if __name__ == '__main__':
main()

132
src/main.py Normal file
View file

@ -0,0 +1,132 @@
import sys
import configparser
import requests
import json
import threading
from time import sleep
from random import randint
from requests import RequestException
from bs4 import BeautifulSoup
CONFIG_DEFAULT = {
'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
def get_soup_from_page(url):
r = requests.get(url, cookies=cookies)
soup = BeautifulSoup(r.text, 'html.parser')
return soup
def get_page():
global xsrf_token, points
try:
soup = get_soup_from_page('https://www.steamgifts.com')
xsrf_token = soup.find('input', {'name': 'xsrf_token'})['value']
points = soup.find('span', {'class': 'nav__points'}).text # storage points
except RequestException:
print('Cant connect to the site')
print('Waiting 2 minutes and reconnect...')
sleep(120)
get_page()
except TypeError:
print('Cant recognize your cookie value.')
sleep(30)
sys.exit(0)
# get codes of the games
def get_games():
global game_name
global pages
n = 1
while n <= pages:
print('Proccessing games from %d page.' % n)
soup = get_soup_from_page('https://www.steamgifts.com/giveaways/search?page=' + str(n))
try:
gifts_list = soup.find_all(lambda tag: tag.name == 'div' and tag.get('class') == ['giveaway__row-inner-wrap'])
for item in gifts_list:
if int(points) == 0:
print('> Sleeping to get 6 points')
sleep(timeout)
get_games()
break
game_cost = item.find_all('span', {'class': 'giveaway__heading__thin'})
last_div = None
for last_div in game_cost:
pass
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
elif int(points) - int(game_cost) > 0:
entry_gift(item.find('a', {'class': 'giveaway__heading__name'})['href'].split('/')[2])
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):
payload = {'xsrf_token': xsrf_token, 'do': 'entry_insert', 'code': code}
entry = requests.post('https://www.steamgifts.com/ajax.php', data=payload, cookies=cookies)
json_data = json.loads(entry.text)
get_page()
# print(json_data)
# updating points after entered a giveaway
if json_data['type'] == 'success':
print('> Bot has entered giveaway: ' + game_name.decode("utf-8"))
sleep(randint(10, 30))
if __name__ == '__main__':
readConfig()
get_page()
get_games()