diff options
author | alyx <alyx@aleteoryx.me> | 2023-11-25 13:25:26 -0500 |
---|---|---|
committer | alyx <alyx@aleteoryx.me> | 2023-11-25 13:25:26 -0500 |
commit | 646d743dce4a3704febfa28be3abce405823baef (patch) | |
tree | 3cdc1de58e91fe7c9dafd53e26b5d95d703e5fb9 /src/bbss/friends.py | |
parent | 4d93c08e447f8d1d8d2a3206267856014adf4de7 (diff) | |
download | bbss.py-646d743dce4a3704febfa28be3abce405823baef.tar.gz bbss.py-646d743dce4a3704febfa28be3abce405823baef.tar.bz2 bbss.py-646d743dce4a3704febfa28be3abce405823baef.zip |
Modelling, guts basically done.
Diffstat (limited to 'src/bbss/friends.py')
-rw-r--r-- | src/bbss/friends.py | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/src/bbss/friends.py b/src/bbss/friends.py new file mode 100644 index 0000000..cd5e7cf --- /dev/null +++ b/src/bbss/friends.py @@ -0,0 +1,44 @@ +from .lists import BaseListFile, ListFileEntry, parse_listfile +from dataclasses import dataclass +from typing import cast, Optional +from collections.abc import Sequence +import requests +import re + +FRIEND_REGEX = re.compile(r"""^ + (?:(?P<scheme>[a-z]+)://)? # match the URL scheme, e.x. `https://` + (?P<domain>(?:[a-z0-9\-]+\.)*(?:[a-z])+) # match the actual domain + (?: + | # match no path + (?P<path>(?:/[^/\s#?]+)*/?) # match a path + ) + $""", re.X) + +@dataclass(frozen=True) +class FriendListFileEntry(ListFileEntry): + url: str + domain: str + scheme: str + path: str + + def __init__(self, entry: str, comment: Optional[str]): + super().__init__(entry, comment) + m = FRIEND_REGEX.match(entry) + if m is None: return + self.__setattr__('scheme', m.group('scheme') or None) + self.__setattr__('domain', m.group('domain')) + self.__setattr__('path', m.group('path').removesuffix('/')) + self.__setattr__('url', (self.scheme if self.scheme else 'https') + '://' + self.domain + (self.path if self.path else '/BBSS') + '/') + + def exists(self) -> bool: + return requests.head(self.url).ok + + def get(self) -> requests.Response: + return requests.get(self.url) + +class FriendListFile(BaseListFile[FriendListFileEntry]): + def __init__(self, contents: str): + super().__init__(contents) + self._entries = parse_listfile(contents, FriendListFileEntry) + +__all__ = ["FriendListFileEntry", "FriendListFile"] |