1
0
socials-util/socials_util/__init__.py

591 lines
18 KiB
Python
Raw Permalink Normal View History

2024-07-08 17:06:01 +00:00
"""# Socials-util.
Python library for parsing and processing URLs of Social Media Sites.
Used by one-page-internet.
"""
2023-12-03 21:19:40 +00:00
2024-11-09 16:54:15 +00:00
import dataclasses
2022-12-22 16:19:52 +00:00
import re
import urllib.parse
2024-05-12 14:35:17 +00:00
import aenum
2024-11-09 16:53:48 +00:00
import enforce_typing
2022-12-22 16:19:52 +00:00
2024-10-20 17:38:14 +00:00
from socials_util._version import __version__
2024-11-01 20:43:46 +00:00
__all__ = [
'__version__',
'SocialSiteId',
'SocialLink',
'WikidataInfo',
'AGGERAGOR_SOCIALS',
'determine_social_from_url',
]
2024-02-17 10:32:48 +00:00
2024-03-31 22:37:15 +00:00
class SocialSiteId(aenum.Enum):
2024-05-12 14:35:17 +00:00
"""The great social website enum."""
2024-02-20 21:07:59 +00:00
# Reddit-like
2024-03-31 22:37:15 +00:00
REDDIT = 1 # Should have been named REDDIT_SUBREDDIT
2023-03-06 22:34:30 +00:00
REDDIT_USER = 22
REDDIT_SUBREDDIT = REDDIT
2024-02-20 21:07:59 +00:00
# Microblogging
2022-12-22 16:19:52 +00:00
TWITTER = 2
2024-02-20 21:07:59 +00:00
MASTODON_PAGE = 10
INSTAGRAM_PAGE = 11
BLUESKY_PROFILE = 12361
BLUESKY_DID = 12409
THREADS_USERNAME = 11892
COHOST_PROFILE = 117203288
2024-06-08 14:59:38 +00:00
PLURK = 32111
2024-02-20 21:07:59 +00:00
# Blogs and feeds
2022-12-22 16:19:52 +00:00
RSS_FEED = 3
2024-02-20 21:07:59 +00:00
PATREON_PAGE = 12
TUMBLR = 9
SUBSTACK = 18
MEDIUM_BLOG = 3899
GOOGLE_BLOGGER_PAGE = 171186
# Video
TWITCH = 6
2024-06-08 14:59:38 +00:00
# Artists portfolio
ARTSTATION_PAGE = 13
PIXIV_USER_ID = 5435
PIXIV_USER_NICKNAME = 31
INPRNT_PAGE = 14
BEHANCE_PAGE = 21
NEWGROUNDS_PAGE = 28
ARTSY_ARTIST = 2042
ARTNET_ARTIST = 3782
DEVIANT_ART_ACCOUNT = 7737
CARA_PROFILE = 35
# Socials aggregators
WIKIDATA = 7
CARRD_PAGE = 24
LINK_COLLECTION_PAGE = 29
DANBOORU_ARTIST = 30
2024-02-20 21:07:59 +00:00
# Misc
2022-12-22 16:19:52 +00:00
PAGE_WATCH = 4
LINKTREE_PAGE = 5
SONGKICK_ARTIST = 8
FACEBOOK_PAGE = 15
EMAIL = 16
2024-03-31 22:37:15 +00:00
JSON_LD = 17 # Similar to PAGE_WATCH, but focused on embedded microdata
2023-03-06 22:34:30 +00:00
ETSY_SHOP = 19
KO_FI = 20
2023-07-22 19:56:31 +00:00
TIKTOK_USER = 7085
2023-05-21 20:07:17 +00:00
YOUTUBE_CHANNEL_HANDLE = 26
YOUTUBE_CHANNEL_ID = 2397
2023-05-21 20:07:17 +00:00
VIMEO_CHANNEL = 27
2023-09-02 16:29:56 +00:00
BANDCAMP_PROFILE = 3283
2023-12-03 20:33:50 +00:00
ITCH_IO_DEVELOPER = 8176
2023-12-26 23:53:11 +00:00
SOUNDCLOUD_ARTIST = 3040
IGDB_GAME_ID = 5794
2024-02-17 20:02:42 +00:00
STEAM_APPLICATION_ID = 1733
2024-01-02 15:27:24 +00:00
GITHUB_REPOSITORY = 364
2024-02-17 20:02:42 +00:00
LINKEDIN_PERSONAL_PROFILE = 6634
2024-02-06 17:18:21 +00:00
2024-02-17 10:32:48 +00:00
# Browser bookmarks
FIREFOX_PROFILE_BOOKMARKS = 33
FALKON_PROFILE_BOOKMARKS = 34
2023-01-02 22:47:03 +00:00
def wikidata_property(self, client):
return client.get(WIKIDATA_PROPERTIES[self])
2022-12-22 16:19:52 +00:00
2023-05-21 20:07:17 +00:00
def is_aggregator(self):
return self in AGGERAGOR_SOCIALS
2024-03-31 22:37:15 +00:00
2023-09-02 16:29:56 +00:00
AGGERAGOR_SOCIALS = {
2024-03-31 22:37:15 +00:00
SocialSiteId.LINKTREE_PAGE,
SocialSiteId.WIKIDATA,
SocialSiteId.CARRD_PAGE,
SocialSiteId.LINK_COLLECTION_PAGE,
SocialSiteId.DANBOORU_ARTIST,
SocialSiteId.IGDB_GAME_ID,
2023-09-02 16:29:56 +00:00
}
2023-05-21 20:07:17 +00:00
2024-03-31 22:37:15 +00:00
2024-11-09 16:53:48 +00:00
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
2024-05-12 14:35:17 +00:00
class SocialLink:
2022-12-22 16:19:52 +00:00
url: urllib.parse.ParseResult
social_site_id: SocialSiteId
2024-05-12 14:35:17 +00:00
social_id: str | None
2022-12-22 16:19:52 +00:00
2024-03-31 22:37:15 +00:00
2024-11-09 16:53:48 +00:00
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
2024-05-12 14:35:17 +00:00
class WikidataInfo:
property_id: int | None
issuer_id: int | None
id_version_of: SocialSiteId | None = None
nickname_version_of: SocialSiteId | None = None
2023-05-21 20:07:17 +00:00
2024-03-31 22:37:15 +00:00
WIKIDATA_PROPERTIES: dict[SocialSiteId | int, WikidataInfo] = {
2023-05-21 20:07:17 +00:00
SocialSiteId.EMAIL: WikidataInfo(968, None),
2023-07-22 19:56:31 +00:00
SocialSiteId.RSS_FEED: WikidataInfo(1079, None),
2023-05-21 20:07:17 +00:00
SocialSiteId.FACEBOOK_PAGE: WikidataInfo(2013, None),
SocialSiteId.INSTAGRAM_PAGE: WikidataInfo(2003, None),
SocialSiteId.LINKTREE_PAGE: WikidataInfo(11079, None),
SocialSiteId.REDDIT_SUBREDDIT: WikidataInfo(3984, None),
SocialSiteId.REDDIT_USER: WikidataInfo(4265, None),
SocialSiteId.RSS_FEED: WikidataInfo(1019, None),
SocialSiteId.SONGKICK_ARTIST: WikidataInfo(3478, None),
SocialSiteId.TWITCH: WikidataInfo(5797, None),
SocialSiteId.TWITTER: WikidataInfo(2002, None),
2023-07-22 19:56:31 +00:00
SocialSiteId.WIKIDATA: WikidataInfo(None, 2013),
2023-05-21 20:07:17 +00:00
SocialSiteId.TUMBLR: WikidataInfo(3943, None),
SocialSiteId.TIKTOK_USER: WikidataInfo(7085, None),
2024-03-31 22:37:15 +00:00
SocialSiteId.PIXIV_USER_ID: WikidataInfo(
2024-05-12 14:35:17 +00:00
5435,
306956,
id_version_of=SocialSiteId.PIXIV_USER_NICKNAME,
2024-03-31 22:37:15 +00:00
),
SocialSiteId.PIXIV_USER_NICKNAME: WikidataInfo(
2024-05-12 14:35:17 +00:00
None,
306956,
nickname_version_of=SocialSiteId.PIXIV_USER_ID,
2024-03-31 22:37:15 +00:00
),
SocialSiteId.MASTODON_PAGE: WikidataInfo(4033, None),
2023-05-21 20:07:17 +00:00
SocialSiteId.PATREON_PAGE: WikidataInfo(4175, 15861362),
SocialSiteId.ARTSTATION_PAGE: WikidataInfo(None, 65551500),
2024-03-31 22:37:15 +00:00
# SocialSiteId.INPRNT_PAGE: WikidataInfo(None, None),
2023-05-21 20:07:17 +00:00
SocialSiteId.CARRD_PAGE: WikidataInfo(None, 106036503),
2024-03-31 22:37:15 +00:00
SocialSiteId.YOUTUBE_CHANNEL_HANDLE: WikidataInfo(
2024-05-12 14:35:17 +00:00
11245,
866,
nickname_version_of=SocialSiteId.YOUTUBE_CHANNEL_ID,
2024-03-31 22:37:15 +00:00
),
SocialSiteId.YOUTUBE_CHANNEL_ID: WikidataInfo(
2024-05-12 14:35:17 +00:00
2397,
866,
id_version_of=SocialSiteId.YOUTUBE_CHANNEL_HANDLE,
2024-03-31 22:37:15 +00:00
),
2023-05-21 20:07:17 +00:00
SocialSiteId.VIMEO_CHANNEL: WikidataInfo(4015, 156376),
SocialSiteId.NEWGROUNDS_PAGE: WikidataInfo(None, 263655),
2023-07-22 19:56:31 +00:00
SocialSiteId.ARTSY_ARTIST: WikidataInfo(2042, 4796642),
2023-12-03 20:33:50 +00:00
SocialSiteId.ARTNET_ARTIST: WikidataInfo(3782, 266566),
SocialSiteId.DEVIANT_ART_ACCOUNT: WikidataInfo(7737, None),
2023-09-02 16:29:56 +00:00
SocialSiteId.DANBOORU_ARTIST: WikidataInfo(None, 64514853),
SocialSiteId.BANDCAMP_PROFILE: WikidataInfo(3283, 545966),
2024-02-06 17:18:21 +00:00
SocialSiteId.BLUESKY_PROFILE: WikidataInfo(12361, 78194383),
SocialSiteId.BLUESKY_DID: WikidataInfo(12409, 78194383),
2024-02-17 20:22:36 +00:00
SocialSiteId.THREADS_USERNAME: WikidataInfo(11892, 120281745),
2023-12-03 20:33:50 +00:00
SocialSiteId.ITCH_IO_DEVELOPER: WikidataInfo(8176, 22905933),
2023-12-16 22:09:27 +00:00
SocialSiteId.COHOST_PROFILE: WikidataInfo(None, 117203288),
2023-12-26 23:53:11 +00:00
SocialSiteId.SOUNDCLOUD_ARTIST: WikidataInfo(3040, None),
SocialSiteId.IGDB_GAME_ID: WikidataInfo(5794, None),
2024-02-17 20:02:42 +00:00
SocialSiteId.STEAM_APPLICATION_ID: WikidataInfo(1733, None),
2024-01-02 15:27:24 +00:00
SocialSiteId.GITHUB_REPOSITORY: WikidataInfo(None, 364),
2024-02-17 20:02:42 +00:00
SocialSiteId.LINKEDIN_PERSONAL_PROFILE: WikidataInfo(6634, None),
2024-03-31 22:37:15 +00:00
SocialSiteId.MEDIUM_BLOG: WikidataInfo(3899, None),
SocialSiteId.SUBSTACK: WikidataInfo(12007, None),
2024-02-17 20:18:53 +00:00
SocialSiteId.INPRNT_PAGE: WikidataInfo(None, None),
SocialSiteId.ETSY_SHOP: WikidataInfo(None, 1353939),
SocialSiteId.KO_FI: WikidataInfo(None, 77949925),
SocialSiteId.BEHANCE_PAGE: WikidataInfo(None, 4880667),
SocialSiteId.PLURK: WikidataInfo(None, 32111),
2024-02-20 21:07:59 +00:00
SocialSiteId.GOOGLE_BLOGGER_PAGE: WikidataInfo(None, 171186),
2024-06-08 14:59:38 +00:00
SocialSiteId.CARA_PROFILE: WikidataInfo(None, None),
2024-02-17 20:18:53 +00:00
# Weird internal
SocialSiteId.LINK_COLLECTION_PAGE: WikidataInfo(None, None),
SocialSiteId.PAGE_WATCH: WikidataInfo(None, None),
SocialSiteId.JSON_LD: WikidataInfo(None, None),
SocialSiteId.FIREFOX_PROFILE_BOOKMARKS: WikidataInfo(None, None),
SocialSiteId.FALKON_PROFILE_BOOKMARKS: WikidataInfo(None, None),
2023-01-02 22:47:03 +00:00
}
2024-03-31 22:37:15 +00:00
2024-11-21 18:39:38 +00:00
RE_ID_AT = r'@([^\s/]+)'
RE_ID_NOAT = r'([^\s/]+)'
RE_ID = r'@?([^\s/]+)'
RE_DUAL_ID = r'@?([^\s/]+/[^\s/]+)'
RE_ANY_SUBPATH = r'(|\/|\/\S*)$'
2024-11-21 18:39:55 +00:00
SPECIAL_REGEX_LITERALS = frozenset(
{RE_ID, RE_DUAL_ID, RE_ANY_SUBPATH, RE_ID_AT, RE_ID_NOAT},
)
2024-11-21 18:39:38 +00:00
REGEX_LITERALS_TO_FORMATTER = {
2024-11-21 18:39:55 +00:00
RE_ID: '{id}',
RE_ID_AT: '@{id}',
RE_ID_NOAT: '{id}',
RE_DUAL_ID: '{id}',
RE_ANY_SUBPATH: '',
2024-11-21 18:39:38 +00:00
}
DOES_NOT_NEED_AUTO_SLASH = frozenset({RE_ANY_SUBPATH})
2023-05-21 20:07:17 +00:00
2024-11-09 16:54:15 +00:00
2024-11-09 16:53:48 +00:00
@enforce_typing.enforce_types
@dataclasses.dataclass(frozen=True)
class SocialPathFormat:
regex: str
formatter: str
2024-03-31 22:37:15 +00:00
2024-11-09 16:54:15 +00:00
2024-11-09 16:53:48 +00:00
def social_path_format_adv(main_domain: str, *path: str) -> SocialPathFormat:
2024-05-12 14:34:47 +00:00
if main_domain.startswith('www.'):
msg = f'Redundant www: {main_domain}'
raise ValueError(msg)
2024-05-12 14:35:17 +00:00
regex_builder: list[str] = [
r'^',
r'(?:https?:\/\/)?',
r'(?:www\.|m\.|mobile\.)?',
2024-05-12 14:35:17 +00:00
re.escape(main_domain),
]
2024-11-09 16:53:48 +00:00
formatter_builder = ['https://', main_domain]
2023-05-21 20:07:17 +00:00
for p in path:
if p not in DOES_NOT_NEED_AUTO_SLASH:
2024-05-12 14:34:47 +00:00
regex_builder.append(r'\/')
2024-11-09 16:53:48 +00:00
formatter_builder.append('/')
2024-05-12 14:35:17 +00:00
regex_builder.append(
p if p in SPECIAL_REGEX_LITERALS else re.escape(p),
2024-05-12 14:35:17 +00:00
)
2024-11-09 16:53:48 +00:00
formatter_builder.append(
REGEX_LITERALS_TO_FORMATTER.get(p, p),
)
del p
if path[-1] not in DOES_NOT_NEED_AUTO_SLASH:
2024-05-12 14:34:47 +00:00
regex_builder.append(r'\/?$')
2024-11-09 16:53:48 +00:00
formatter_builder.append('/')
return SocialPathFormat(
2024-11-09 16:54:15 +00:00
regex=''.join(regex_builder),
formatter=''.join(formatter_builder),
2024-11-09 16:53:48 +00:00
)
2024-11-09 16:54:15 +00:00
2024-11-09 16:53:48 +00:00
def social_path_format(main_domain: str) -> SocialPathFormat:
return social_path_format_adv(main_domain, RE_ID)
2024-11-09 16:54:15 +00:00
2024-11-09 16:53:48 +00:00
def re_social_subdomain(main_domain: str) -> SocialPathFormat:
return SocialPathFormat(
2024-11-09 16:54:15 +00:00
regex=r'^(?:https?:\/\/)?(?:www\.)?([\w_-]+)\.'
+ re.escape(main_domain)
+ r'(\/.*)?$',
formatter='https://{id}.' + main_domain,
2024-11-09 16:53:48 +00:00
)
2024-11-09 16:54:15 +00:00
2024-11-09 16:53:48 +00:00
def re_social_path(main_domain: str) -> str:
2024-11-09 16:57:53 +00:00
return re_social_path_adv(main_domain, RE_ID)
2024-11-09 16:53:48 +00:00
def re_social_path_adv(main_domain: str, *path: str) -> str:
return social_path_format_adv(main_domain, *path).regex
2023-05-21 20:07:17 +00:00
2024-03-31 22:37:15 +00:00
2023-01-02 22:47:03 +00:00
MAILTO_URL = r'^mailto:(?:[\w._.]+@[\w._.]+)$'
2023-05-21 20:07:17 +00:00
REDDIT_SUBREDDIT_URL = r'^(?:https?:\/\/)?(?:old\.)?reddit\.com\/r\/([\w-]+)\/?$'
2024-03-31 22:37:15 +00:00
REDDIT_USER_URL = (
2024-08-25 17:13:11 +00:00
r'^(?:https?:\/\/)?(?:old\.|www\.)?reddit\.com\/user\/([\w-]+)(?:|\/submitted)\/?$'
2024-03-31 22:37:15 +00:00
)
2024-11-09 16:53:48 +00:00
2024-03-31 22:37:15 +00:00
SONGKICK_ARTIST_URL = (
r'^(?:https?:\/\/)?(?:www\.)?songkick\.com\/artists\/(\d+)([\w-]*)\/?$'
)
2024-11-09 16:53:48 +00:00
2023-09-02 16:29:56 +00:00
PIXIV_USER_ID_URL = r'^(?:https?:\/\/)?(?:www\.)?pixiv\.net(?:\/en)?\/users/(\d+)\/?$'
2024-03-31 22:37:15 +00:00
PIXIV_USER_ID_URL_2 = (
r'^(?:https?:\/\/)?(?:www\.)?pixiv\.net(?:\/en)?\/member\.php\/?[?]id=(\d+)$'
)
2022-12-22 16:19:52 +00:00
2024-11-09 16:53:48 +00:00
URL_FORMATS: list[tuple[object, SocialPathFormat]] = [
2022-12-22 16:19:52 +00:00
# Twitter
2024-11-09 16:53:48 +00:00
(SocialSiteId.TWITTER, social_path_format_adv('x.com', RE_ID, RE_ANY_SUBPATH)),
2024-11-09 16:54:15 +00:00
(
SocialSiteId.TWITTER,
social_path_format_adv('twitter.com', RE_ID, RE_ANY_SUBPATH),
),
2022-12-22 16:19:52 +00:00
# Linktr.ee
2024-11-09 16:53:48 +00:00
(SocialSiteId.LINKTREE_PAGE, social_path_format('linktr.ee')),
2022-12-22 16:19:52 +00:00
# Twitch.tv
2024-11-09 16:53:48 +00:00
(SocialSiteId.TWITCH, social_path_format('twitch.tv')),
2022-12-22 16:19:52 +00:00
# Wikidata
2024-11-09 16:53:48 +00:00
(SocialSiteId.WIKIDATA, social_path_format_adv('wikidata.org', 'wiki', RE_ID)),
2022-12-22 16:19:52 +00:00
# Tumblr
2024-11-09 16:53:48 +00:00
(SocialSiteId.TUMBLR, social_path_format('tumblr.com')),
(SocialSiteId.TUMBLR, re_social_subdomain('tumblr.com')),
(SocialSiteId.TUMBLR, social_path_format('tumblr.com/blog')),
(SocialSiteId.TUMBLR, social_path_format('tumblr.com/blog/view')),
2022-12-22 16:19:52 +00:00
# Instagram
2024-11-09 16:53:48 +00:00
(SocialSiteId.INSTAGRAM_PAGE, social_path_format('instagram.com')),
2022-12-22 16:19:52 +00:00
# Patreon
2024-11-09 16:54:15 +00:00
(
SocialSiteId.PATREON_PAGE,
social_path_format_adv('patreon.com', RE_ID, RE_ANY_SUBPATH),
),
2022-12-22 16:19:52 +00:00
# Artstation
2024-11-09 16:54:15 +00:00
(
SocialSiteId.ARTSTATION_PAGE,
social_path_format_adv('artstation.com', RE_ID, RE_ANY_SUBPATH),
),
2024-11-09 16:53:48 +00:00
(SocialSiteId.ARTSTATION_PAGE, re_social_subdomain('artstation.com')),
2022-12-22 16:19:52 +00:00
# Inprnt
2024-11-09 16:53:48 +00:00
(SocialSiteId.INPRNT_PAGE, social_path_format_adv('inprnt.com', 'gallery', RE_ID)),
# Facebook
(SocialSiteId.FACEBOOK_PAGE, social_path_format('facebook.com')),
2023-01-02 22:47:03 +00:00
# Substack
2024-11-09 16:53:48 +00:00
(SocialSiteId.SUBSTACK, re_social_subdomain('substack.com')),
2023-03-06 22:34:30 +00:00
# Etsy shop
2024-11-09 16:53:48 +00:00
(SocialSiteId.ETSY_SHOP, social_path_format_adv('etsy.com', 'shop', RE_ID)),
2023-03-06 22:34:30 +00:00
# Behance
2024-11-09 16:53:48 +00:00
(SocialSiteId.BEHANCE_PAGE, social_path_format('behance.net')),
# Tiktok
(SocialSiteId.TIKTOK_USER, social_path_format('tiktok.com')),
# Pixiv
2024-11-09 16:54:15 +00:00
(
SocialSiteId.PIXIV_USER_NICKNAME,
social_path_format_adv('pixiv.net', 'stacc', RE_ID),
),
2024-11-09 16:53:48 +00:00
(SocialSiteId.PIXIV_USER_NICKNAME, re_social_subdomain('fanbox.cc')),
2024-11-09 16:54:15 +00:00
(
SocialSiteId.PIXIV_USER_NICKNAME,
social_path_format_adv('sketch.pixiv.net', RE_ID),
),
2023-05-21 20:07:17 +00:00
# Carrd
2024-11-09 16:53:48 +00:00
(SocialSiteId.CARRD_PAGE, re_social_subdomain('carrd.co')),
2023-05-21 20:07:17 +00:00
# Youtube
2024-11-09 16:54:15 +00:00
(
SocialSiteId.YOUTUBE_CHANNEL_HANDLE,
2024-11-21 18:39:38 +00:00
social_path_format_adv('youtube.com', RE_ID_AT, RE_ANY_SUBPATH),
2024-11-09 16:54:15 +00:00
),
(
SocialSiteId.YOUTUBE_CHANNEL_HANDLE,
social_path_format_adv('youtube.com', 'c', RE_ID),
),
(
SocialSiteId.YOUTUBE_CHANNEL_ID,
social_path_format_adv('youtube.com', 'channel', RE_ID),
),
2024-11-21 18:39:38 +00:00
(
SocialSiteId.YOUTUBE_CHANNEL_HANDLE,
social_path_format_adv('youtube.com', RE_ID_NOAT, RE_ANY_SUBPATH),
),
2023-05-21 20:07:17 +00:00
# Vimeo
2024-11-09 16:53:48 +00:00
(SocialSiteId.VIMEO_CHANNEL, social_path_format_adv('vimeo.com', RE_ID)),
2023-05-21 20:07:17 +00:00
# Newgrounds
2024-11-09 16:53:48 +00:00
(SocialSiteId.NEWGROUNDS_PAGE, re_social_subdomain('newgrounds.com')),
2023-07-22 19:56:31 +00:00
# Artsy
2024-11-09 16:54:15 +00:00
(
SocialSiteId.ARTSY_ARTIST,
social_path_format_adv('artsy.net', 'artist', RE_ID, RE_ANY_SUBPATH),
),
2024-11-09 16:53:48 +00:00
# Artnet
2024-11-09 16:54:15 +00:00
(
SocialSiteId.ARTNET_ARTIST,
social_path_format_adv('artnet.com', 'artists', RE_ID, RE_ANY_SUBPATH),
),
# Deviant art
2024-11-09 16:53:48 +00:00
(SocialSiteId.DEVIANT_ART_ACCOUNT, social_path_format_adv('deviantart.com', RE_ID)),
(SocialSiteId.DEVIANT_ART_ACCOUNT, re_social_subdomain('deviantart.com')),
2023-09-02 16:29:56 +00:00
# Danbooru
2024-11-09 16:54:15 +00:00
(
SocialSiteId.DANBOORU_ARTIST,
social_path_format_adv('danbooru.donmai.us', 'artists', RE_ID),
),
2023-09-02 16:29:56 +00:00
# Bandcamp
2024-11-09 16:53:48 +00:00
(SocialSiteId.BANDCAMP_PROFILE, re_social_subdomain('bandcamp.com')),
2023-10-03 19:58:10 +00:00
# Bluesky
2024-11-09 16:54:15 +00:00
(
SocialSiteId.BLUESKY_PROFILE,
social_path_format_adv('bsky.app', 'profile', RE_ID),
),
2024-11-09 16:53:48 +00:00
# Medium
2024-11-09 16:54:15 +00:00
(
SocialSiteId.MEDIUM_BLOG,
social_path_format_adv('medium.com', RE_ID),
),
(
SocialSiteId.MEDIUM_BLOG,
re_social_subdomain('medium.com'),
),
2024-11-09 16:53:48 +00:00
# Ko-fi
2024-11-09 16:54:15 +00:00
(
SocialSiteId.KO_FI,
social_path_format_adv('ko-fi.com', RE_ID),
),
(
SocialSiteId.KO_FI,
social_path_format_adv('ko-fi.com', RE_ID, 'shop'),
),
2024-02-17 20:22:36 +00:00
# Threads
2024-11-09 16:54:15 +00:00
(
SocialSiteId.THREADS_USERNAME,
social_path_format_adv('threads.net', RE_ID),
),
2023-12-03 20:33:50 +00:00
# Itch.io
2024-11-09 16:54:15 +00:00
(
SocialSiteId.ITCH_IO_DEVELOPER,
re_social_subdomain('itch.io'),
),
2023-12-16 22:09:27 +00:00
# Cohost
2024-11-09 16:54:15 +00:00
(
SocialSiteId.COHOST_PROFILE,
social_path_format_adv('cohost.org', RE_ID),
),
2023-12-26 23:53:11 +00:00
# Soundcloud
2024-11-09 16:54:15 +00:00
(
SocialSiteId.SOUNDCLOUD_ARTIST,
social_path_format_adv('soundcloud.com', RE_ID),
),
2023-12-26 23:53:11 +00:00
# IGDB
2024-11-09 16:54:15 +00:00
(
SocialSiteId.IGDB_GAME_ID,
social_path_format_adv('igdb.com', 'games', RE_ID),
),
2024-02-17 20:02:42 +00:00
# Steam game
2024-03-31 22:37:15 +00:00
(
SocialSiteId.STEAM_APPLICATION_ID,
2024-11-09 16:53:48 +00:00
social_path_format_adv('store.steampowered.com', 'app', RE_ID, RE_ANY_SUBPATH),
2024-03-31 22:37:15 +00:00
),
2024-01-02 15:27:24 +00:00
# Github
(
SocialSiteId.GITHUB_REPOSITORY,
2024-11-09 16:53:48 +00:00
social_path_format_adv('github.com', RE_DUAL_ID, RE_ANY_SUBPATH),
),
2024-02-06 17:18:21 +00:00
# Plurk
2024-11-09 16:54:15 +00:00
(
SocialSiteId.PLURK,
social_path_format_adv('plurk.com', RE_ID),
),
2024-02-17 20:02:42 +00:00
# Linked in
2024-03-31 22:37:15 +00:00
(
SocialSiteId.LINKEDIN_PERSONAL_PROFILE,
2024-11-09 16:53:48 +00:00
social_path_format_adv('linkedin.com', 'in', RE_ID),
2024-03-31 22:37:15 +00:00
),
2024-02-20 21:07:59 +00:00
# Google Blogger
2024-11-09 16:54:15 +00:00
(
SocialSiteId.GOOGLE_BLOGGER_PAGE,
re_social_subdomain('blogspot.com'),
),
2024-06-08 14:59:38 +00:00
# Cara
2024-11-09 16:54:15 +00:00
(
SocialSiteId.CARA_PROFILE,
social_path_format_adv('cara.app', RE_ID, RE_ANY_SUBPATH),
),
2022-12-22 16:19:52 +00:00
]
2024-11-09 16:53:48 +00:00
REGEXES: list[tuple[str, object]] = [
# Reddit
(REDDIT_SUBREDDIT_URL, SocialSiteId.REDDIT_SUBREDDIT),
(REDDIT_USER_URL, SocialSiteId.REDDIT_USER),
# Songkick
(SONGKICK_ARTIST_URL, SocialSiteId.SONGKICK_ARTIST),
# Pixiv
(PIXIV_USER_ID_URL, SocialSiteId.PIXIV_USER_ID),
(PIXIV_USER_ID_URL_2, SocialSiteId.PIXIV_USER_ID),
# Email
(MAILTO_URL, SocialSiteId.EMAIL),
] + [(fmt.regex, social_site_id) for (social_site_id, fmt) in URL_FORMATS]
2024-05-12 14:34:47 +00:00
WELL_KNOWN_MASTODON_INSTANCES: frozenset[str] = frozenset(
2024-03-31 22:37:15 +00:00
{
# Includes all servers with 50 000+ users as of 6 july 2023.
# based on https://mastodonservers.net/servers/top
'mastodon.social',
#'pawoo.net',
'baraag.net',
'mstdn.jp',
'mastodon.cloud',
'mstdn.social',
'mastodon.online',
'mas.to',
'mastodon.world',
'mastodon.lol',
'mastodon.sdf.org',
'c.im',
'mastodon.uno',
'mastodonapp.uk',
'fosstodon.org',
'idlethumbs.social',
2024-05-12 14:35:17 +00:00
},
2024-03-31 22:37:15 +00:00
)
DISALLOWED_IDENTIFIERS: frozenset[str] = frozenset({'www', 'intent', 'user'})
2024-05-12 17:34:00 +00:00
2024-06-01 18:46:38 +00:00
2024-05-12 14:35:17 +00:00
def determine_social_from_url_internally(
url: str,
) -> tuple[SocialSiteId | None, str | None]:
2024-05-12 14:38:15 +00:00
if not isinstance(url, str):
msg = f'Url must be {str}'
raise TypeError(msg)
2022-12-22 16:19:52 +00:00
# Regexes
2024-03-31 22:37:15 +00:00
for social_site_url_regex, social_site_id in REGEXES:
2024-07-08 16:55:45 +00:00
if m := re.fullmatch(social_site_url_regex, url, re.IGNORECASE):
2023-01-02 22:47:03 +00:00
groups = m.groups()
2024-05-12 17:33:42 +00:00
username_or_id = groups[0] if len(groups) > 0 else None
if username_or_id in DISALLOWED_IDENTIFIERS:
continue
return (social_site_id, username_or_id)
del social_site_url_regex, social_site_id, m
2022-12-22 16:19:52 +00:00
# Mastodon
for mastodon_hostname in WELL_KNOWN_MASTODON_INSTANCES:
if url.startswith('https://' + mastodon_hostname):
return (SocialSiteId.MASTODON_PAGE, None)
2022-12-22 16:19:52 +00:00
if 'mastodon' in url:
return (SocialSiteId.MASTODON_PAGE, None)
# Feed (?)
2024-05-12 14:34:47 +00:00
if 'feed' in url or 'xml' in url or 'rss' in url or 'atom' in url:
2022-12-22 16:19:52 +00:00
return (SocialSiteId.RSS_FEED, None)
return (None, None)
2024-05-12 14:35:17 +00:00
2024-05-12 14:34:47 +00:00
def to_parse_result(url: str | urllib.parse.ParseResult) -> urllib.parse.ParseResult:
2023-01-02 22:47:03 +00:00
if isinstance(url, str):
2024-05-12 14:34:47 +00:00
return urllib.parse.urlparse(url)
if isinstance(url, urllib.parse.ParseResult):
return url
# Throw error
msg = f'Expected {urllib.parse.ParseResult} or {str}'
raise TypeError(msg)
2024-11-09 16:54:15 +00:00
def to_url(
social_site_id: SocialSiteId,
social_id: str,
) -> urllib.parse.ParseResult | None:
for ssi, fmt in URL_FORMATS:
2024-11-09 16:53:48 +00:00
if ssi == social_site_id:
return to_parse_result(fmt.formatter.format(id=social_id))
return None
2024-05-12 14:35:17 +00:00
2024-11-09 16:54:15 +00:00
2024-05-12 14:35:17 +00:00
def determine_social_from_url(
url_not_normalized: str | urllib.parse.ParseResult,
) -> SocialLink | None:
2024-05-12 14:34:47 +00:00
url = to_parse_result(url_not_normalized)
2024-03-31 22:37:15 +00:00
(social_site_id, social_id) = determine_social_from_url_internally(
2024-05-12 14:35:17 +00:00
url._replace(query='', fragment='').geturl(),
2024-03-31 22:37:15 +00:00
)
2023-05-21 20:07:17 +00:00
if not social_site_id:
2024-03-31 22:37:15 +00:00
(social_site_id, social_id) = determine_social_from_url_internally(
2024-05-12 14:35:17 +00:00
url._replace(fragment='').geturl(),
2024-03-31 22:37:15 +00:00
)
2022-12-22 16:19:52 +00:00
if not social_site_id:
return None
2024-11-09 16:53:48 +00:00
# Normalize url if possible
if social_id is not None:
url = to_url(social_site_id, social_id) or url
2023-01-02 22:47:03 +00:00
return SocialLink(url, social_site_id, social_id)