threading

- added a simple webserver thread that runs alongside the steamgift enterer
This commit is contained in:
mcinj 2022-05-13 13:29:40 -04:00
parent d250ed48b5
commit 4f4709776b
2 changed files with 109 additions and 21 deletions

14
src/index.html Normal file
View file

@ -0,0 +1,14 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is a simple paragraph.</p>
</body>
</html>

View file

@ -1,3 +1,4 @@
import threading
from random import randint from random import randint
from time import sleep from time import sleep
@ -5,35 +6,57 @@ import log
from ConfigReader import ConfigReader, ConfigException from ConfigReader import ConfigReader, ConfigException
from SteamGifts import SteamGifts, SteamGiftsException from SteamGifts import SteamGifts, SteamGiftsException
from notification import Notification from notification import Notification
from threading import Thread
logger = log.get_logger(__name__) logger = log.get_logger(__name__)
def run(): class WebServerThread(threading.Thread):
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) def run_webserver(self):
import http.server
import socketserver
notification = Notification(config['NOTIFICATIONS'].get('notification.prefix')) PORT = 8000
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: class MyHttpRequestHandler(http.server.SimpleHTTPRequestHandler):
def do_GET(self):
self.path = 'index.html'
return http.server.SimpleHTTPRequestHandler.do_GET(self)
Handler = MyHttpRequestHandler
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print("Http Server Serving at port", PORT)
httpd.serve_forever()
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):
cookie = config['DEFAULT'].get('cookie') cookie = config['DEFAULT'].get('cookie')
user_agent = config['DEFAULT'].get('user_agent') user_agent = config['DEFAULT'].get('user_agent')
main_page_enabled = config['DEFAULT'].getboolean('enabled') main_page_enabled = config['DEFAULT'].getboolean('enabled')
@ -68,6 +91,57 @@ def run():
random_seconds = randint(1740, 3540) # sometime between 29-59 minutes random_seconds = randint(1740, 3540) # sometime between 29-59 minutes
logger.info(f"Going to sleep for {random_seconds / 60} minutes.") logger.info(f"Going to sleep for {random_seconds / 60} minutes.")
sleep(random_seconds) sleep(random_seconds)
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()
w = WebServerThread()
w.setName("WebServer")
w.setDaemon(True)
w.start()
g.join()
except SteamGiftsException as e: except SteamGiftsException as e:
notification.send_error(e) notification.send_error(e)
sleep(5) sleep(5)