mirror of
https://gitlab.com/chicken-riders/RcGcDb.git
synced 2025-02-23 00:54:09 +00:00
Created a skeleton for rewrite
This commit is contained in:
parent
b1ee9171aa
commit
88ff0a3552
|
@ -4,4 +4,5 @@ lxml >= 4.2.1
|
|||
nest-asyncio >= 1.4.0
|
||||
irc >= 19.0.1
|
||||
beautifulsoup4>=4.9.3
|
||||
asyncpg>=0.22.0
|
||||
asyncpg>=0.22.0
|
||||
aioredis >= 1.3.1
|
941
src/bot.py
941
src/bot.py
File diff suppressed because it is too large
Load diff
|
@ -10,9 +10,25 @@ if TYPE_CHECKING:
|
|||
|
||||
|
||||
class Domain:
|
||||
def __init__(self, task: asyncio.Task, irc_client: Optional[irc.client_aio.AioSimpleIRCClient] = None):
|
||||
self.task = task
|
||||
def __init__(self, name: str, irc_client: Optional[irc.client_aio.AioSimpleIRCClient] = None):
|
||||
self.name = name # This should be always in format of topname.extension for example fandom.com
|
||||
self.task: asyncio.Task = self.create_task()
|
||||
self.wikis: list[src.wiki.Wiki] = list()
|
||||
self.rate_limiter: src.wiki_ratelimiter = src.wiki_ratelimiter.RateLimiter()
|
||||
self.irc = irc_client
|
||||
|
||||
def add_wiki(self, wiki: src.wiki.Wiki, index: int = None):
|
||||
"""Adds a wiki to domain list.
|
||||
|
||||
:parameter wiki - Wiki object
|
||||
:parameter index (optional) - index at which the wiki should be added, if not specified it's the end of the list"""
|
||||
if index:
|
||||
self.wikis.insert(index, wiki)
|
||||
else:
|
||||
self.wikis.append(wiki)
|
||||
|
||||
def create_task(self) -> asyncio.Task:
|
||||
return asyncio.create_task(self.run_wiki_check())
|
||||
|
||||
async def run_wiki_check(self):
|
||||
raise NotImplementedError
|
||||
|
|
38
src/domain_manager.py
Normal file
38
src/domain_manager.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
from __future__ import annotations
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from urllib.parse import urlparse, urlunparse
|
||||
from src.config import settings
|
||||
from src.domain import Domain
|
||||
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.wiki import Wiki
|
||||
|
||||
|
||||
class DomainManager:
|
||||
def __init__(self):
|
||||
self.domains: dict[str, Domain] = {}
|
||||
|
||||
def new_wiki(self, wiki: Wiki):
|
||||
"""Finds a domain for the wiki and adds a wiki to the domain object.
|
||||
|
||||
:parameter wiki - Wiki object to be added"""
|
||||
wiki_domain = self.get_domain(wiki.script_url)
|
||||
try:
|
||||
self.domains[wiki_domain].add_wiki(wiki)
|
||||
except KeyError:
|
||||
self.new_domain(wiki_domain).add_wiki(wiki)
|
||||
|
||||
@staticmethod
|
||||
def get_domain(url: str) -> str:
|
||||
"""Returns a domain for given URL (for example fandom.com, wikipedia.org)"""
|
||||
parsed_url = urlparse(url)
|
||||
return ".".join(urlunparse((*parsed_url[0:2], "", "", "", "")).split(".")[-2:])
|
||||
|
||||
def new_domain(self, name: str) -> Domain:
|
||||
# TODO IRC Part
|
||||
self.domains[name] = Domain(name, irc)
|
||||
return self.domains[name]
|
||||
|
||||
|
||||
domains = DomainManager()
|
11
src/wiki.py
11
src/wiki.py
|
@ -17,9 +17,18 @@ from bs4 import BeautifulSoup
|
|||
|
||||
logger = logging.getLogger("rcgcdb.wiki")
|
||||
|
||||
class Wiki:
|
||||
def __init__(self, script_url: str, rc_id: int, discussion_id: int):
|
||||
self.script_url = script_url
|
||||
self.session = aiohttp.ClientSession(headers=settings["header"], timeout=aiohttp.ClientTimeout(6.0))
|
||||
self.statistics = Statistics()
|
||||
|
||||
@property
|
||||
def rc_id(self):
|
||||
return self.statistics.rc_id
|
||||
|
||||
@dataclass
|
||||
class Wiki:
|
||||
class Wiki_old:
|
||||
mw_messages: int = None
|
||||
fail_times: int = 0 # corresponding to amount of times connection with wiki failed for client reasons (400-499)
|
||||
session: aiohttp.ClientSession = None
|
||||
|
|
Loading…
Reference in a new issue