diff --git a/src/configloader.py b/src/configloader.py index 50fa9c6..5bd7467 100644 --- a/src/configloader.py +++ b/src/configloader.py @@ -1,22 +1,33 @@ -import json, sys, logging +import json +import logging +import sys + +global settings + + +def load_settings(): + global settings + try: # load settings + with open("settings.json", encoding="utf8") 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.13.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) + # Set the cooldown to 15 seconds if it's a wiki farm like Fandom or Gamepedia and the cooldown is even lower than that. + # Look, it's unreasonable to have even higher refresh rate than that, seriously. Setting it even lower can cause issues + # for all users of the script for high usage of farm's servers. So please, do not remove this code unless you absolutely + # know what you are doing <3 + if any(("fandom.com" in settings["wiki_url"], "gamepedia.com" in settings["wiki_url"])): + if settings["cooldown"] < 15: + settings["cooldown"] = 15 + if settings["fandom_discussions"]["cooldown"] < 15: + settings["fandom_discussions"]["cooldown"] = 15 + + +load_settings() -try: # load settings - with open("settings.json", encoding="utf8") 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.13.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) -# Set the cooldown to 15 seconds if it's a wiki farm like Fandom or Gamepedia and the cooldown is even lower than that. -# Look, it's unreasonable to have even higher refresh rate than that, seriously. Setting it even lower can cause issues -# for all users of the script for high usage of farm's servers. So please, do not remove this code unless you absolutely -# know what you are doing <3 -if any(("fandom.com" in settings["wiki_url"], "gamepedia.com" in settings["wiki_url"])): - if settings["cooldown"] < 15: - settings["cooldown"] = 15 -if settings["fandom_discussions"]["cooldown"] < 15: - settings["fandom_discussions"]["cooldown"] = 15 diff --git a/src/migrations/1.13.1.1.py b/src/migrations/1.13.1.1.py deleted file mode 100644 index 83d8600..0000000 --- a/src/migrations/1.13.1.1.py +++ /dev/null @@ -1,23 +0,0 @@ -from src.configloader import settings -import logging - -from src.migrations.utils import return_example_file - -logger = logging.getLogger("rcgcdw.migrations.1.13.1.1") -base_file = return_example_file() -new_settings = settings.copy() - - -def run(): - if "event_appearance" not in settings: - try: - settings["event_appearance"] = {} - struct = settings['appearance']['embed'] - for key, value in struct.items(): - settings["event_appearance"][key] = value - settings["event_appearance"][key]["emoji"] = base_file["event_appearance"] - except KeyError: - logger.error("Failed to migrate appearance embed.") - else: # Don't do migrations - return - diff --git a/src/migrations/11311.py b/src/migrations/11311.py new file mode 100644 index 0000000..ec966b2 --- /dev/null +++ b/src/migrations/11311.py @@ -0,0 +1,46 @@ +from src.configloader import settings, load_settings +import logging +import shutil +import time +import json +import sys + +from src.migrations.utils import return_example_file + +logger = logging.getLogger("rcgcdw.migrations.1.13.1.1") +base_file = return_example_file() +new_settings = settings.copy() + +def run(): + global base_file + if "event_appearance" not in settings: + logger.info("Running migration 1.13.1.1") + if "event_appearance" not in base_file: # if local base file is outdated, download from repo + base_file = return_example_file(force=True) + try: + struct = settings['appearance']['embed'] + new_settings["event_appearance"] = {} + keys = [] + for key, value in struct.items(): + if key not in ("show_edit_changes", "show_footer", "embed_images"): + new_settings["event_appearance"][key] = value + try: + new_settings["event_appearance"][key]["emoji"] = base_file["event_appearance"][key]["emoji"] + except KeyError: + new_settings["event_appearance"][key]["emoji"] = "" + keys.append(key) + for item in keys: + del new_settings['appearance']['embed'][item] + except KeyError: + logger.exception("Failed to migrate appearance embed.") + sys.exit(1) + shutil.copy("settings.json", "settings.json.{}.bak".format(int(time.time()))) + with open("settings.json", "w") as new_write: + new_write.write(json.dumps(new_settings, indent=4)) + load_settings() + logger.info("Migration 1.13.1.1 has been successful.") + else: + logger.debug("Ignoring migration 1.13.1.1") + + +run() diff --git a/src/migrations/__init__.py b/src/migrations/__init__.py index e69de29..8e786d0 100644 --- a/src/migrations/__init__.py +++ b/src/migrations/__init__.py @@ -0,0 +1 @@ +__all__ = ["11311"] diff --git a/src/migrations/utils.py b/src/migrations/utils.py index 55adfb5..fc448f1 100644 --- a/src/migrations/utils.py +++ b/src/migrations/utils.py @@ -5,8 +5,10 @@ import json discussion_logger = logging.getLogger("rcgcdw.migrations.utils") -def return_example_file() -> dict: +def return_example_file(force=False) -> dict: try: + if force: + raise FileNotFoundError with open('settings.json.example', 'r') as example_file: return json.loads(example_file.read()) except FileNotFoundError: diff --git a/src/rcgcdw.py b/src/rcgcdw.py index 1970e18..2bd33e5 100644 --- a/src/rcgcdw.py +++ b/src/rcgcdw.py @@ -46,7 +46,7 @@ TESTING = True if "--test" in sys.argv else False # debug mode, pipeline testin logging.config.dictConfig(settings["logging"]) logger = logging.getLogger("rcgcdw") logger.debug("Current settings: {settings}".format(settings=settings)) - +from src.migrations import * # migrations after logging storage = datafile # Remove previous data holding file if exists and limitfetch allows