2022-05-13 19:29:40 +02:00
|
|
|
import threading
|
2022-04-27 19:49:55 +02:00
|
|
|
from random import randint
|
2022-05-04 17:14:38 +02:00
|
|
|
from time import sleep
|
2022-04-23 18:34:04 +02:00
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
from flask_basicauth import BasicAuth
|
|
|
|
|
2022-04-23 21:00:22 +02:00
|
|
|
import log
|
2022-05-04 17:28:33 +02:00
|
|
|
from ConfigReader import ConfigReader, ConfigException
|
2022-05-05 15:01:11 +02:00
|
|
|
from SteamGifts import SteamGifts, SteamGiftsException
|
|
|
|
from notification import Notification
|
2022-05-13 19:29:40 +02:00
|
|
|
from threading import Thread
|
2022-04-23 21:00:22 +02:00
|
|
|
|
2022-05-05 22:50:01 +02:00
|
|
|
|
2022-04-23 21:00:22 +02:00
|
|
|
logger = log.get_logger(__name__)
|
2022-04-23 18:34:04 +02:00
|
|
|
|
|
|
|
|
2022-05-13 19:29:40 +02:00
|
|
|
class WebServerThread(threading.Thread):
|
2022-04-23 18:34:04 +02:00
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
def __init__(self, config):
|
2022-05-14 04:33:10 +02:00
|
|
|
Thread.__init__(self)
|
|
|
|
self.exc = None
|
2022-05-14 06:47:15 +02:00
|
|
|
self.config = config
|
|
|
|
self.port = config['WEB'].getint('web.port')
|
|
|
|
self.ssl = config['WEB'].getboolean('web.ssl')
|
|
|
|
self.enabled = config['WEB'].getboolean('web.enabled')
|
|
|
|
self.app_root = config['WEB'].get('web.app_root')
|
|
|
|
self.basic_auth = config['WEB'].getboolean('web.basic_auth')
|
|
|
|
self.basic_auth_username = config['WEB'].get('web.basic_auth.username')
|
|
|
|
self.basic_auth_password = config['WEB'].get('web.basic_auth.password')
|
2022-05-14 04:33:10 +02:00
|
|
|
|
2022-05-13 19:29:40 +02:00
|
|
|
def run_webserver(self):
|
2022-05-13 22:55:52 +02:00
|
|
|
from flask import Flask
|
|
|
|
from flask import render_template
|
|
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
if self.basic_auth:
|
|
|
|
app.config['BASIC_AUTH_USERNAME'] = self.basic_auth_username
|
|
|
|
app.config['BASIC_AUTH_PASSWORD'] = self.basic_auth_password
|
|
|
|
|
|
|
|
app.config['BASIC_AUTH_FORCE'] = self.basic_auth
|
|
|
|
basic_auth = BasicAuth(app)
|
|
|
|
|
|
|
|
@app.route(f"{self.app_root}")
|
2022-05-13 22:55:52 +02:00
|
|
|
def config():
|
|
|
|
with open('../config/config.ini', 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
return render_template('configuration.html', config=content)
|
2022-05-04 17:14:38 +02:00
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
@app.route(f"{self.app_root}log")
|
2022-05-13 22:55:52 +02:00
|
|
|
def logs():
|
|
|
|
return render_template('log.html')
|
2022-05-05 15:01:11 +02:00
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
@app.route(f"{self.app_root}stream")
|
2022-05-13 22:55:52 +02:00
|
|
|
def stream():
|
|
|
|
def generate():
|
2022-05-14 04:33:10 +02:00
|
|
|
with open('../config/info.log') as f:
|
2022-05-13 22:55:52 +02:00
|
|
|
while True:
|
|
|
|
yield f.read()
|
|
|
|
sleep(10)
|
2022-05-13 19:29:40 +02:00
|
|
|
|
2022-05-13 22:55:52 +02:00
|
|
|
return app.response_class(generate(), mimetype='text/plain')
|
2022-05-13 19:29:40 +02:00
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
if self.enabled:
|
|
|
|
logger.info("Webserver Enabled. Running")
|
|
|
|
if self.ssl:
|
|
|
|
app.run(port=self.port, host="0.0.0.0", ssl_context='adhoc')
|
|
|
|
else:
|
|
|
|
app.run(port=self.port, host="0.0.0.0")
|
|
|
|
else:
|
|
|
|
logger.info("Webserver NOT Enabled.")
|
2022-05-13 19:29:40 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# Variable that stores the exception, if raised by someFunction
|
|
|
|
self.exc = None
|
|
|
|
try:
|
|
|
|
self.run_webserver()
|
|
|
|
except BaseException as e:
|
|
|
|
self.exc = e
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
threading.Thread.join(self)
|
|
|
|
# Since join() returns in caller thread
|
|
|
|
# we re-raise the caught exception
|
|
|
|
# if any was caught
|
|
|
|
if self.exc:
|
|
|
|
raise self.exc
|
|
|
|
|
|
|
|
|
|
|
|
class GiveawayEntererThread(threading.Thread):
|
|
|
|
|
|
|
|
def __init__(self, config, notification):
|
|
|
|
Thread.__init__(self)
|
|
|
|
self.exc = None
|
|
|
|
self.config = config
|
|
|
|
self.notification = notification
|
|
|
|
|
|
|
|
def run_steam_gifts(self, config, notification):
|
2022-05-05 15:01:11 +02:00
|
|
|
cookie = config['DEFAULT'].get('cookie')
|
2022-05-13 17:43:59 +02:00
|
|
|
user_agent = config['DEFAULT'].get('user_agent')
|
2022-05-12 17:54:58 +02:00
|
|
|
main_page_enabled = config['DEFAULT'].getboolean('enabled')
|
2022-05-05 15:01:11 +02:00
|
|
|
minimum_points = config['DEFAULT'].getint('minimum_points')
|
|
|
|
max_entries = config['DEFAULT'].getint('max_entries')
|
|
|
|
max_time_left = config['DEFAULT'].getint('max_time_left')
|
|
|
|
minimum_game_points = config['DEFAULT'].getint('minimum_game_points')
|
|
|
|
blacklist = config['DEFAULT'].get('blacklist_keywords')
|
|
|
|
|
2022-05-13 17:43:59 +02:00
|
|
|
all_page = SteamGifts(cookie, user_agent, 'All', False, minimum_points, max_entries,
|
2022-05-05 15:01:11 +02:00
|
|
|
max_time_left, minimum_game_points, blacklist, notification)
|
|
|
|
|
2022-05-12 17:54:58 +02:00
|
|
|
wishlist_page_enabled = config['WISHLIST'].getboolean('wishlist.enabled')
|
2022-05-05 15:01:11 +02:00
|
|
|
wishlist_minimum_points = config['WISHLIST'].getint('wishlist.minimum_points')
|
|
|
|
wishlist_max_entries = config['WISHLIST'].getint('wishlist.max_entries')
|
|
|
|
wishlist_max_time_left = config['WISHLIST'].getint('wishlist.max_time_left')
|
2022-05-04 17:14:38 +02:00
|
|
|
|
2022-05-13 17:43:59 +02:00
|
|
|
wishlist_page = SteamGifts(cookie, user_agent, 'Wishlist', False, wishlist_minimum_points,
|
2022-05-05 15:01:11 +02:00
|
|
|
wishlist_max_entries, wishlist_max_time_left, 0, '', notification)
|
|
|
|
|
2022-05-12 17:54:58 +02:00
|
|
|
if not main_page_enabled and not wishlist_page_enabled:
|
2022-05-14 17:13:29 +02:00
|
|
|
logger.error("⁉️ Both 'Default' and 'Wishlist' configurations are disabled. Nothing will run. Exiting...")
|
2022-05-05 15:01:11 +02:00
|
|
|
sleep(10)
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
while True:
|
2022-05-14 17:13:29 +02:00
|
|
|
logger.info("🟢 Evaluating giveaways.")
|
2022-05-12 17:54:58 +02:00
|
|
|
if wishlist_page_enabled:
|
2022-05-05 15:01:11 +02:00
|
|
|
wishlist_page.start()
|
2022-05-12 17:54:58 +02:00
|
|
|
if main_page_enabled:
|
2022-05-05 15:01:11 +02:00
|
|
|
all_page.start()
|
|
|
|
|
2022-05-14 17:13:29 +02:00
|
|
|
logger.info("🔴 All giveaways evaluated.")
|
2022-05-05 15:01:11 +02:00
|
|
|
random_seconds = randint(1740, 3540) # sometime between 29-59 minutes
|
2022-05-14 17:13:29 +02:00
|
|
|
logger.info(f"🛋 Going to sleep for {random_seconds / 60} minutes.")
|
2022-05-05 15:01:11 +02:00
|
|
|
sleep(random_seconds)
|
2022-05-13 19:29:40 +02:00
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# Variable that stores the exception, if raised by someFunction
|
|
|
|
self.exc = None
|
|
|
|
try:
|
|
|
|
self.run_steam_gifts(self.config, self.notification)
|
|
|
|
except BaseException as e:
|
|
|
|
self.exc = e
|
|
|
|
|
|
|
|
def join(self):
|
|
|
|
threading.Thread.join(self)
|
|
|
|
# Since join() returns in caller thread
|
|
|
|
# we re-raise the caught exception
|
|
|
|
# if any was caught
|
|
|
|
if self.exc:
|
|
|
|
raise self.exc
|
|
|
|
|
|
|
|
|
|
|
|
def run():
|
|
|
|
logger.info("Starting Steamgifts bot.")
|
|
|
|
file_name = '../config/config.ini'
|
|
|
|
config = None
|
|
|
|
try:
|
|
|
|
config = ConfigReader(file_name)
|
|
|
|
except IOError:
|
|
|
|
txt = f"{file_name} doesn't exist. Rename {file_name}.example to {file_name} and fill out."
|
|
|
|
logger.warning(txt)
|
|
|
|
exit(-1)
|
|
|
|
except ConfigException as e:
|
|
|
|
logger.error(e)
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
config.read(file_name)
|
|
|
|
|
|
|
|
notification = Notification(config['NOTIFICATIONS'].get('notification.prefix'))
|
|
|
|
pushover_enabled = config['NOTIFICATIONS'].getboolean('pushover.enabled')
|
|
|
|
pushover_token = config['NOTIFICATIONS'].get('pushover.token')
|
|
|
|
pushover_user_key = config['NOTIFICATIONS'].get('pushover.user_key')
|
|
|
|
if pushover_enabled:
|
|
|
|
notification.enable_pushover(pushover_token, pushover_user_key)
|
|
|
|
try:
|
|
|
|
g = GiveawayEntererThread(config, notification)
|
|
|
|
g.setName("Giveaway Enterer")
|
|
|
|
g.start()
|
|
|
|
|
2022-05-14 06:47:15 +02:00
|
|
|
w = WebServerThread(config)
|
2022-05-13 19:29:40 +02:00
|
|
|
w.setName("WebServer")
|
2022-05-13 22:55:52 +02:00
|
|
|
# if the giveaway thread dies then this daemon thread will die by definition
|
2022-05-13 19:29:40 +02:00
|
|
|
w.setDaemon(True)
|
|
|
|
w.start()
|
|
|
|
|
|
|
|
g.join()
|
2022-05-05 15:01:11 +02:00
|
|
|
except SteamGiftsException as e:
|
2022-05-09 03:55:09 +02:00
|
|
|
notification.send_error(e)
|
2022-05-05 15:01:11 +02:00
|
|
|
sleep(5)
|
|
|
|
exit(-1)
|
|
|
|
except Exception as e:
|
|
|
|
logger.error(e)
|
2022-05-09 03:55:09 +02:00
|
|
|
notification.send_error("Something happened and the bot had to quit!")
|
2022-05-05 15:01:11 +02:00
|
|
|
sleep(5)
|
|
|
|
exit(-1)
|
2022-04-23 18:34:04 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2022-05-14 04:33:10 +02:00
|
|
|
logger.info("""
|
|
|
|
-------------------------------------------------------------------------------------
|
|
|
|
_____ _ _ __ _ ____ _
|
|
|
|
/ ____|| | (_) / _|| | | _ \ | |
|
|
|
|
| (___ | |_ ___ __ _ _ __ ___ __ _ _ | |_ | |_ ___ | |_) | ___ | |_
|
|
|
|
\___ \ | __|/ _ \ / _` || '_ ` _ \ / _` || || _|| __|/ __| | _ < / _ \ | __|
|
|
|
|
____) || |_| __/| (_| || | | | | || (_| || || | | |_ \__ \ | |_) || (_) || |_
|
|
|
|
|_____/ \__|\___| \__,_||_| |_| |_| \__, ||_||_| \__||___/ |____/ \___/ \__|
|
|
|
|
__/ |
|
|
|
|
|___/
|
|
|
|
-------------------------------------------------------------------------------------
|
|
|
|
""")
|
2022-05-09 03:55:09 +02:00
|
|
|
run()
|