1
0
ScrapTheChan/scrapthechan/parsers/__init__.py

38 lines
1.3 KiB
Python

"""Here are defined the JSON parsers for imageboards."""
from re import search
from typing import List
from scrapthechan.parser import Parser
__all__ = ["SUPPORTED_IMAGEBOARDS", "get_parser_by_url", "get_parser_by_site"]
SUPPORTED_IMAGEBOARDS: List[str] = ["4chan.org", "lainchan.org", "2ch.hk", \
"8kun.top"]
def get_parser_by_url(url: str) -> Parser:
"""Parses URL and extracts from it site name, board and thread.
And then returns initialised Parser object for detected imageboard."""
URLRX = r"https?:\/\/(?P<s>[\w\.]+)\/(?P<b>\w+)\/(?:\w+)?\/(?P<t>\w+)"
site, board, thread = search(URLRX, url).groups()
return get_parser_by_site(site, board, thread)
def get_parser_by_site(site: str, board: str, thread: str) -> Parser:
"""Returns an initialised parser for `site` with `board` and `thread`."""
if '4chan' in site:
from .fourchan import FourChanParser
return FourChanParser(board, thread)
elif 'lainchan' in site:
from .lainchan import LainchanParser
return LainchanParser(board, thread)
elif '2ch' in site:
from .dvach import DvachParser
return DvachParser(board, thread)
elif '8kun' in site:
from .eightkun import EightKunParser
return EightKunParser(board, thread)
else:
raise NotImplementedError(f"Parser for {site} is not implemented")