1
0
ScrapTheChan/scrapthechan/parsers/__init__.py

39 lines
1.4 KiB
Python
Raw Normal View History

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