steamgifts-bot/main.py

133 lines
3.9 KiB
Python
Raw Normal View History

2019-02-28 13:04:41 +01:00
import sys
2020-03-09 19:32:32 +01:00
import configparser
2019-02-28 13:04:41 +01:00
import requests
import json
import threading
2020-03-09 19:32:32 +01:00
from time import sleep
from random import randint
2019-02-28 13:04:41 +01:00
from requests import RequestException
2020-03-09 19:32:32 +01:00
from bs4 import BeautifulSoup
2019-02-28 13:04:41 +01:00
2020-03-09 19:32:32 +01:00
CONFIG_DEFAULT = {
'cookie': 'Paste you cookie here',
'sleeptime': 900
}
2019-02-28 13:04:41 +01:00
2020-03-09 19:32:32 +01:00
def exitMessage(msg):
print(msg)
input()
sys.exit()
2019-02-28 13:04:41 +01:00
2020-03-09 19:32:32 +01:00
def readConfig():
config = configparser.ConfigParser()
2019-02-28 13:04:41 +01:00
2020-03-09 19:32:32 +01:00
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']}
2019-02-28 13:04:41 +01:00
2020-03-09 19:32:32 +01:00
pages = 1
def get_soup_from_page(url):
2019-02-28 13:04:41 +01:00
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...')
2020-03-09 19:32:32 +01:00
sleep(120)
2019-02-28 13:04:41 +01:00
get_page()
except TypeError:
print('Cant recognize your cookie value.')
2020-03-09 19:32:32 +01:00
sleep(30)
2019-02-28 13:04:41 +01:00
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')
2020-03-09 19:32:32 +01:00
sleep(timeout)
2019-02-28 13:04:41 +01:00
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')
2019-02-28 13:04:41 +01:00
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
2020-03-09 19:32:32 +01:00
except AttributeError:
2019-02-28 13:04:41 +01:00
break
print('List of games is ended. Waiting 2 min to update...')
2020-03-09 19:32:32 +01:00
sleep(120)
2019-02-28 13:04:41 +01:00
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':
2020-03-09 19:32:32 +01:00
print('> Bot has entered giveaway: ' + game_name.decode("utf-8"))
sleep(randint(10, 30))
2019-02-28 13:04:41 +01:00
if __name__ == '__main__':
2020-03-09 19:32:32 +01:00
readConfig()
2019-02-28 13:04:41 +01:00
get_page()
get_games()