diff --git a/configloader.py b/configloader.py new file mode 100644 index 0000000..86cf036 --- /dev/null +++ b/configloader.py @@ -0,0 +1,12 @@ +import json, sys, logging + +try: # load settings + with open("settings.json") as sfile: + settings = json.load(sfile) + if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"] != -1: + settings["limitrefetch"] = settings["limit"] + if "user-agent" in settings["header"]: + settings["header"]["user-agent"] = settings["header"]["user-agent"].format(version="1.6.0.1") # set the version in the useragent +except FileNotFoundError: + logging.critical("No config file could be found. Please make sure settings.json is in the directory.") + sys.exit(1) diff --git a/misc.py b/misc.py index d9cca00..9dcca0f 100644 --- a/misc.py +++ b/misc.py @@ -16,6 +16,7 @@ import json, logging, sys, re from html.parser import HTMLParser +from configloader import settings # Create a custom logger diff --git a/rcgcdw.py b/rcgcdw.py index c6a48f2..7327628 100644 --- a/rcgcdw.py +++ b/rcgcdw.py @@ -25,6 +25,7 @@ from bs4 import BeautifulSoup from collections import defaultdict, Counter from urllib.parse import quote_plus from html.parser import HTMLParser +from configloader import settings if __name__ != "__main__": # return if called as a module logging.critical("The file is being executed as a module. Please execute the script using the console.") @@ -32,23 +33,11 @@ if __name__ != "__main__": # return if called as a module TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testing -try: # load settings - with open("settings.json") as sfile: - settings = json.load(sfile) - if settings["limitrefetch"] < settings["limit"] and settings["limitrefetch"] != -1: - settings["limitrefetch"] = settings["limit"] - if "user-agent" in settings["header"]: - settings["header"]["user-agent"] = settings["header"]["user-agent"].format(version="1.6.0.1") # set the version in the useragent -except FileNotFoundError: - logging.critical("No config file could be found. Please make sure settings.json is in the directory.") - sys.exit(1) - -logging.debug("Current settings: {settings}".format(settings=settings)) - # Prepare logging logging.config.dictConfig(settings["logging"]) logger = logging.getLogger("rcgcdw") +logger.debug("Current settings: {settings}".format(settings=settings)) # Setup translation