diff --git a/README.md b/README.md index da82073..152935b 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ RcGcDb is a backend for handling webhooks to which recent changes of MediaWiki w #### Installation ``` $ git clone git@gitlab.com:chicken-riders/rcgcdb.git #clone repo -$ cd RcGcDw +$ cd RcGcDb $ python3 -m venv . #(optional, if you want to contain everything (you should!)) $ source bin/activate #(optional, see above) $ pip3 install -r requirements.txt #install requirements (lxml may require additional distro packages, more on that here https://lxml.de/build.html) diff --git a/settings.json.example b/settings.json.example index 2ff6e72..b982714 100644 --- a/settings.json.example +++ b/settings.json.example @@ -4,6 +4,7 @@ }, "max_requests_per_minute": 30, "minimal_cooldown_per_wiki_in_sec": 60, + "database_path": "rcgcdb.db", "monitoring_webhook": "111111111111111111/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", "logging": { "version": 1, diff --git a/src/bot.py b/src/bot.py index a73aeaf..5db453d 100644 --- a/src/bot.py +++ b/src/bot.py @@ -112,9 +112,9 @@ def global_exception_handler(loop, context): """Global exception handler for asyncio, lets us know when something crashes""" msg = context.get("exception", context["message"]) logger.error(msg) - requests.post("https://discord.com/api/webhooks/" + settings["monitoring_webhook"], - data=DiscordMessage("embed", "exception", None, content= - "[RcGcDb] Exception detected, function might have shut down! Exception: {}".format(msg), wiki=None)) + #requests.post("https://discord.com/api/webhooks/" + settings["monitoring_webhook"], + # data=DiscordMessage("embed", "exception", None, content= + # "[RcGcDb] Exception detected, function might have shut down! Exception: {}".format(msg), wiki=None)) async def main_loop(): diff --git a/src/database.py b/src/database.py index a0517b4..5673d66 100644 --- a/src/database.py +++ b/src/database.py @@ -1,4 +1,5 @@ import sqlite3 +from src.config import settings -db_connection = sqlite3.connect('rcgcdb.db') +db_connection = sqlite3.connect(settings.get("database_path", 'rcgcdb.db')) db_cursor = db_connection.cursor() diff --git a/src/wiki.py b/src/wiki.py index c86852f..0f16ca6 100644 --- a/src/wiki.py +++ b/src/wiki.py @@ -21,12 +21,8 @@ class Wiki: fail_times: int = 0 # corresponding to amount of times connection with wiki failed for client reasons (400-499) session: aiohttp.ClientSession = None - async def create_session(self): - self.session = aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(5.0)) async def fetch_wiki(self, extended, script_path) -> aiohttp.ClientResponse: - if self.session is None: - await self.create_session() url_path = script_path + "api.php" amount = 20 if extended: @@ -44,7 +40,8 @@ class Wiki: "rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user", "rclimit": amount, "rctype": "edit|new|log|external", "siprop": "namespaces|general"} try: - response = await self.session.get(url_path, params=params) + async with aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(5.0)) as session: + response = await session.get(url_path, params=params) except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError): logger.exception("A connection error occurred while requesting {}".format(url_path)) raise WikiServerError @@ -52,7 +49,8 @@ class Wiki: async def safe_request(self, url): try: - request = await self.session.get(url, timeout=5, allow_redirects=False) + async with aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(5.0)) as session: + request = await session.get(url, timeout=5, allow_redirects=False) request.raise_for_status() except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError): logger.exception("Reached connection error for request on link {url}".format(url=url))