steamgifts-bot/src/run.py

112 lines
3.7 KiB
Python
Raw Normal View History

2022-04-23 18:34:04 +02:00
import configparser
2022-04-23 21:00:22 +02:00
from configparser import ConfigParser
from random import randint
2022-04-23 18:34:04 +02:00
2022-04-23 21:00:22 +02:00
import log
logger = log.get_logger(__name__)
2022-04-23 18:34:04 +02:00
2022-04-23 21:00:22 +02:00
class MyException(Exception):
pass
def value_range(min, max):
return [str(x) for x in [*range(min, max + 1)]]
class MyConfig(ConfigParser):
required_values = {
'DEFAULT': {
'gift_types': ('All', 'Wishlist', 'Recommended', 'Copies', 'DLC', 'New'),
'pinned': ('true', 'false'),
'minimum_points': '%s' % (value_range(0, 400)),
'max_entries': '%s' % (value_range(0, 100000)),
'max_time_left': '%s' % (value_range(0, 21600)),
'minimum_game_points': '%s' % (value_range(0, 50))
}
}
default_values = {
'DEFAULT': {
'cookie': '',
'gift_types': 'All',
'pinned': 'true',
'minimum_points': f"{randint(20, 100)}",
'max_entries': f"{randint(1000, 2500)}",
'max_time_left': f"{randint(180,500)}",
'minimum_game_points': '1',
'blacklist_keywords': 'hentai,adult'
}
}
2022-04-23 21:00:22 +02:00
def __init__(self, config_file):
super(MyConfig, self).__init__()
self.read(config_file)
modified = self.create_defaults()
if modified:
with open(config_file, 'w+') as file:
self.write(file)
2022-04-23 21:00:22 +02:00
self.validate_config()
def create_defaults(self):
modified = False
for section, keys in self.default_values.items():
if section not in self:
self.add_section(section)
modified = True
for key, value in keys.items():
if key not in self[section]:
self.set(section, key, value)
modified = True
return modified
2022-04-23 21:00:22 +02:00
def validate_config(self):
for section, keys in self.required_values.items():
2022-04-23 21:00:22 +02:00
if section not in self:
raise MyException(
'Missing section %s in the config file' % section)
for key, values in keys.items():
if key not in self[section] or self[section][key] == '':
raise MyException((
'Missing value for %s under section %s in ' +
'the config file') % (key, section))
if values:
if self[section][key] not in values:
raise MyException((
'Invalid value for %s under section %s in ' +
'the config file') % (key, section))
2022-04-23 18:34:04 +02:00
def run():
from main import SteamGifts as SG
2022-04-23 21:00:22 +02:00
file_name = '../config/config.ini'
config = None
2022-04-23 21:00:22 +02:00
try:
config = MyConfig(file_name)
2022-04-23 21:00:22 +02:00
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 MyException as e:
logger.error(e)
exit(-1)
2022-04-23 18:34:04 +02:00
2022-04-23 21:00:22 +02:00
config.read(file_name)
cookie = config['DEFAULT'].get('cookie')
2022-04-23 18:34:04 +02:00
pinned_games = config['DEFAULT'].getboolean('pinned')
gift_types = config['DEFAULT'].get('gift_types')
2022-04-23 21:00:22 +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-04-23 18:34:04 +02:00
s = SG(cookie, gift_types, pinned_games, minimum_points, max_entries, max_time_left, minimum_game_points, blacklist)
2022-04-23 18:34:04 +02:00
s.start()
if __name__ == '__main__':
run()