2020-07-09 23:58:25 +00:00
|
|
|
from dataclasses import dataclass
|
2020-07-10 13:38:36 +00:00
|
|
|
from src.session import session
|
2020-07-10 14:11:45 +00:00
|
|
|
import logging, aiohttp
|
|
|
|
from src.exceptions import *
|
2020-07-10 20:07:33 +00:00
|
|
|
from src.database import db_cursor
|
|
|
|
import src.discord
|
2020-07-10 13:38:36 +00:00
|
|
|
|
2020-07-10 14:11:45 +00:00
|
|
|
logger = logging.getLogger("rcgcdb.wiki")
|
2020-07-09 23:58:25 +00:00
|
|
|
|
|
|
|
@dataclass
|
2020-07-09 22:24:23 +00:00
|
|
|
class Wiki:
|
2020-07-10 13:38:36 +00:00
|
|
|
mw_messages: int = None
|
2020-07-09 23:58:25 +00:00
|
|
|
fail_times: int = 0 # corresponding to amount of times connection with wiki failed for client reasons (400-499)
|
2020-07-10 13:38:36 +00:00
|
|
|
|
2020-07-10 20:07:33 +00:00
|
|
|
async def fetch_wiki(self, extended, script_path, api_path) -> aiohttp.ClientResponse:
|
|
|
|
url_path = script_path + api_path
|
2020-07-10 13:38:36 +00:00
|
|
|
amount = 20
|
|
|
|
if extended:
|
|
|
|
params = {"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges",
|
|
|
|
"meta": "allmessages|siteinfo",
|
|
|
|
"utf8": 1, "tglimit": "max", "tgprop": "displayname",
|
|
|
|
"rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user",
|
|
|
|
"rclimit": amount, "rctype": "edit|new|log|external",
|
|
|
|
"ammessages": "recentchanges-page-added-to-category|recentchanges-page-removed-from-category|recentchanges-page-added-to-category-bundled|recentchanges-page-removed-from-category-bundled",
|
|
|
|
"amenableparser": 1, "amincludelocal": 1, "siprop": "namespaces"}
|
|
|
|
else:
|
|
|
|
params = {"action": "query", "format": "json", "uselang": "content", "list": "tags|recentchanges",
|
|
|
|
"utf8": 1,
|
|
|
|
"tglimit": "max", "tgprop": "displayname",
|
|
|
|
"rcprop": "title|redirect|timestamp|ids|loginfo|parsedcomment|sizes|flags|tags|user",
|
|
|
|
"rclimit": amount, "rctype": "edit|new|log|external", "siprop": "namespaces"}
|
|
|
|
try:
|
2020-07-10 14:11:45 +00:00
|
|
|
response = await session.get(url_path, params=params)
|
2020-07-10 20:07:33 +00:00
|
|
|
except (aiohttp.ClientConnectionError, aiohttp.ServerTimeoutError):
|
|
|
|
logger.exception("A connection error occurred while requesting {}".format(url_path))
|
|
|
|
raise WikiServerError
|
2020-07-10 14:11:45 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
async def check_status(self, wiki_id, status, name):
|
|
|
|
if 199 < status < 300:
|
|
|
|
self.fail_times = 0
|
|
|
|
pass
|
|
|
|
elif 400 < status < 500: # ignore 400 error since this might be our fault
|
|
|
|
self.fail_times += 1
|
|
|
|
logger.warning("Wiki {} responded with HTTP code {}, increased fail_times to {}, skipping...".format(name, status, self.fail_times))
|
|
|
|
if self.fail_times > 3:
|
2020-07-10 20:07:33 +00:00
|
|
|
await self.remove(wiki_id, status)
|
2020-07-10 14:11:45 +00:00
|
|
|
raise WikiError
|
|
|
|
elif 499 < status < 600:
|
|
|
|
logger.warning("Wiki {} responded with HTTP code {}, skipping...".format(name, status, self.fail_times))
|
|
|
|
raise WikiServerError
|
|
|
|
|
2020-07-10 20:07:33 +00:00
|
|
|
async def remove(self, wiki_id, reason):
|
|
|
|
src.discord.wiki_removal()
|