Ruff
This commit is contained in:
parent
4668372036
commit
cf6ef584a0
|
@ -5,9 +5,9 @@ Python library for parsing and processing URLs of Social Media Sites.
|
|||
Used by one-page-internet.
|
||||
"""
|
||||
|
||||
import dataclasses
|
||||
import re
|
||||
import urllib.parse
|
||||
import dataclasses
|
||||
|
||||
import aenum
|
||||
import enforce_typing
|
||||
|
@ -205,7 +205,6 @@ WIKIDATA_PROPERTIES: dict[SocialSiteId | int, WikidataInfo] = {
|
|||
}
|
||||
|
||||
|
||||
|
||||
RE_ID = r'@?([^\s/]+)'
|
||||
RE_DUAL_ID = r'@?([^\s/]+/[^\s/]+)'
|
||||
RE_ANY_SUBPATH = r'(|\/|\/\S*)$'
|
||||
|
@ -214,12 +213,14 @@ SPECIAL_REGEX_LITERALS = frozenset({RE_ID, RE_DUAL_ID, RE_ANY_SUBPATH})
|
|||
REGEX_LITERALS_TO_FORMATTER = {RE_ID: '{id}', RE_DUAL_ID: '{id}', RE_ANY_SUBPATH: ''}
|
||||
DOES_NOT_NEED_AUTO_SLASH = frozenset({RE_ANY_SUBPATH})
|
||||
|
||||
|
||||
@enforce_typing.enforce_types
|
||||
@dataclasses.dataclass(frozen=True)
|
||||
class SocialPathFormat:
|
||||
regex: str
|
||||
formatter: str
|
||||
|
||||
|
||||
def social_path_format_adv(main_domain: str, *path: str) -> SocialPathFormat:
|
||||
if main_domain.startswith('www.'):
|
||||
msg = f'Redundant www: {main_domain}'
|
||||
|
@ -248,19 +249,24 @@ def social_path_format_adv(main_domain: str, *path: str) -> SocialPathFormat:
|
|||
formatter_builder.append('/')
|
||||
|
||||
return SocialPathFormat(
|
||||
regex = ''.join(regex_builder),
|
||||
formatter = ''.join(formatter_builder),
|
||||
regex=''.join(regex_builder),
|
||||
formatter=''.join(formatter_builder),
|
||||
)
|
||||
|
||||
|
||||
def social_path_format(main_domain: str) -> SocialPathFormat:
|
||||
return social_path_format_adv(main_domain, RE_ID)
|
||||
|
||||
|
||||
def re_social_subdomain(main_domain: str) -> SocialPathFormat:
|
||||
return SocialPathFormat(
|
||||
regex = r'^(?:https?:\/\/)?(?:www\.)?([\w_-]+)\.' + re.escape(main_domain) + r'(\/.*)?$',
|
||||
formatter = 'https://{id}.' + main_domain,
|
||||
regex=r'^(?:https?:\/\/)?(?:www\.)?([\w_-]+)\.'
|
||||
+ re.escape(main_domain)
|
||||
+ r'(\/.*)?$',
|
||||
formatter='https://{id}.' + main_domain,
|
||||
)
|
||||
|
||||
|
||||
def re_social_path(main_domain: str) -> str:
|
||||
return social_path_format(main_domain).regex
|
||||
|
||||
|
@ -286,108 +292,145 @@ PIXIV_USER_ID_URL_2 = (
|
|||
)
|
||||
|
||||
URL_FORMATS: list[tuple[object, SocialPathFormat]] = [
|
||||
|
||||
# Twitter
|
||||
(SocialSiteId.TWITTER, social_path_format_adv('x.com', RE_ID, RE_ANY_SUBPATH)),
|
||||
(SocialSiteId.TWITTER, social_path_format_adv('twitter.com', RE_ID, RE_ANY_SUBPATH)),
|
||||
|
||||
(
|
||||
SocialSiteId.TWITTER,
|
||||
social_path_format_adv('twitter.com', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
# Linktr.ee
|
||||
(SocialSiteId.LINKTREE_PAGE, social_path_format('linktr.ee')),
|
||||
|
||||
# Twitch.tv
|
||||
(SocialSiteId.TWITCH, social_path_format('twitch.tv')),
|
||||
|
||||
# Wikidata
|
||||
(SocialSiteId.WIKIDATA, social_path_format_adv('wikidata.org', 'wiki', RE_ID)),
|
||||
|
||||
# Tumblr
|
||||
(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')),
|
||||
|
||||
# Instagram
|
||||
(SocialSiteId.INSTAGRAM_PAGE, social_path_format('instagram.com')),
|
||||
|
||||
# Patreon
|
||||
(SocialSiteId.PATREON_PAGE, social_path_format_adv('patreon.com', RE_ID, RE_ANY_SUBPATH)),
|
||||
|
||||
(
|
||||
SocialSiteId.PATREON_PAGE,
|
||||
social_path_format_adv('patreon.com', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
# Artstation
|
||||
(SocialSiteId.ARTSTATION_PAGE, social_path_format_adv('artstation.com', RE_ID, RE_ANY_SUBPATH)),
|
||||
(
|
||||
SocialSiteId.ARTSTATION_PAGE,
|
||||
social_path_format_adv('artstation.com', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
(SocialSiteId.ARTSTATION_PAGE, re_social_subdomain('artstation.com')),
|
||||
|
||||
# Inprnt
|
||||
(SocialSiteId.INPRNT_PAGE, social_path_format_adv('inprnt.com', 'gallery', RE_ID)),
|
||||
|
||||
# Facebook
|
||||
(SocialSiteId.FACEBOOK_PAGE, social_path_format('facebook.com')),
|
||||
|
||||
# Substack
|
||||
(SocialSiteId.SUBSTACK, re_social_subdomain('substack.com')),
|
||||
|
||||
# Etsy shop
|
||||
(SocialSiteId.ETSY_SHOP, social_path_format_adv('etsy.com', 'shop', RE_ID)),
|
||||
|
||||
# Behance
|
||||
(SocialSiteId.BEHANCE_PAGE, social_path_format('behance.net')),
|
||||
|
||||
# Tiktok
|
||||
(SocialSiteId.TIKTOK_USER, social_path_format('tiktok.com')),
|
||||
|
||||
# Pixiv
|
||||
(SocialSiteId.PIXIV_USER_NICKNAME, social_path_format_adv('pixiv.net', 'stacc', RE_ID)),
|
||||
(
|
||||
SocialSiteId.PIXIV_USER_NICKNAME,
|
||||
social_path_format_adv('pixiv.net', 'stacc', RE_ID),
|
||||
),
|
||||
(SocialSiteId.PIXIV_USER_NICKNAME, re_social_subdomain('fanbox.cc')),
|
||||
(SocialSiteId.PIXIV_USER_NICKNAME, social_path_format_adv('sketch.pixiv.net', RE_ID)),
|
||||
|
||||
(
|
||||
SocialSiteId.PIXIV_USER_NICKNAME,
|
||||
social_path_format_adv('sketch.pixiv.net', RE_ID),
|
||||
),
|
||||
# Carrd
|
||||
(SocialSiteId.CARRD_PAGE, re_social_subdomain('carrd.co')),
|
||||
|
||||
# Youtube
|
||||
(SocialSiteId.YOUTUBE_CHANNEL_HANDLE, social_path_format_adv('youtube.com', RE_ID, RE_ANY_SUBPATH)),
|
||||
(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)),
|
||||
|
||||
(
|
||||
SocialSiteId.YOUTUBE_CHANNEL_HANDLE,
|
||||
social_path_format_adv('youtube.com', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
(
|
||||
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),
|
||||
),
|
||||
# Vimeo
|
||||
(SocialSiteId.VIMEO_CHANNEL, social_path_format_adv('vimeo.com', RE_ID)),
|
||||
|
||||
# Newgrounds
|
||||
(SocialSiteId.NEWGROUNDS_PAGE, re_social_subdomain('newgrounds.com')),
|
||||
|
||||
# Artsy
|
||||
(SocialSiteId.ARTSY_ARTIST, social_path_format_adv( 'artsy.net', 'artist', RE_ID, RE_ANY_SUBPATH)),
|
||||
|
||||
(
|
||||
SocialSiteId.ARTSY_ARTIST,
|
||||
social_path_format_adv('artsy.net', 'artist', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
# Artnet
|
||||
(SocialSiteId.ARTNET_ARTIST, social_path_format_adv( 'artnet.com', 'artists', RE_ID, RE_ANY_SUBPATH)),
|
||||
|
||||
(
|
||||
SocialSiteId.ARTNET_ARTIST,
|
||||
social_path_format_adv('artnet.com', 'artists', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
# Deviant art
|
||||
(SocialSiteId.DEVIANT_ART_ACCOUNT, social_path_format_adv('deviantart.com', RE_ID)),
|
||||
(SocialSiteId.DEVIANT_ART_ACCOUNT, re_social_subdomain('deviantart.com')),
|
||||
|
||||
# Danbooru
|
||||
(SocialSiteId.DANBOORU_ARTIST, social_path_format_adv('danbooru.donmai.us', 'artists', RE_ID)),
|
||||
|
||||
(
|
||||
SocialSiteId.DANBOORU_ARTIST,
|
||||
social_path_format_adv('danbooru.donmai.us', 'artists', RE_ID),
|
||||
),
|
||||
# Bandcamp
|
||||
(SocialSiteId.BANDCAMP_PROFILE, re_social_subdomain('bandcamp.com')),
|
||||
|
||||
# Bluesky
|
||||
(SocialSiteId.BLUESKY_PROFILE, social_path_format_adv('bsky.app', 'profile', RE_ID)),
|
||||
|
||||
(
|
||||
SocialSiteId.BLUESKY_PROFILE,
|
||||
social_path_format_adv('bsky.app', 'profile', RE_ID),
|
||||
),
|
||||
# Medium
|
||||
(SocialSiteId.MEDIUM_BLOG, social_path_format_adv('medium.com', RE_ID), ),
|
||||
(SocialSiteId.MEDIUM_BLOG, re_social_subdomain('medium.com'), ),
|
||||
(
|
||||
SocialSiteId.MEDIUM_BLOG,
|
||||
social_path_format_adv('medium.com', RE_ID),
|
||||
),
|
||||
(
|
||||
SocialSiteId.MEDIUM_BLOG,
|
||||
re_social_subdomain('medium.com'),
|
||||
),
|
||||
# Ko-fi
|
||||
(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'), ),
|
||||
(
|
||||
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'),
|
||||
),
|
||||
# Threads
|
||||
(SocialSiteId.THREADS_USERNAME, social_path_format_adv('threads.net', RE_ID), ),
|
||||
(
|
||||
SocialSiteId.THREADS_USERNAME,
|
||||
social_path_format_adv('threads.net', RE_ID),
|
||||
),
|
||||
# Itch.io
|
||||
(SocialSiteId.ITCH_IO_DEVELOPER, re_social_subdomain('itch.io'), ),
|
||||
(
|
||||
SocialSiteId.ITCH_IO_DEVELOPER,
|
||||
re_social_subdomain('itch.io'),
|
||||
),
|
||||
# Cohost
|
||||
(SocialSiteId.COHOST_PROFILE, social_path_format_adv('cohost.org', RE_ID), ),
|
||||
(
|
||||
SocialSiteId.COHOST_PROFILE,
|
||||
social_path_format_adv('cohost.org', RE_ID),
|
||||
),
|
||||
# Soundcloud
|
||||
(SocialSiteId.SOUNDCLOUD_ARTIST, social_path_format_adv('soundcloud.com', RE_ID), ),
|
||||
(
|
||||
SocialSiteId.SOUNDCLOUD_ARTIST,
|
||||
social_path_format_adv('soundcloud.com', RE_ID),
|
||||
),
|
||||
# IGDB
|
||||
(SocialSiteId.IGDB_GAME_ID, social_path_format_adv('igdb.com', 'games', RE_ID), ),
|
||||
(
|
||||
SocialSiteId.IGDB_GAME_ID,
|
||||
social_path_format_adv('igdb.com', 'games', RE_ID),
|
||||
),
|
||||
# Steam game
|
||||
(
|
||||
SocialSiteId.STEAM_APPLICATION_ID,
|
||||
|
@ -399,16 +442,25 @@ URL_FORMATS: list[tuple[object, SocialPathFormat]] = [
|
|||
social_path_format_adv('github.com', RE_DUAL_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
# Plurk
|
||||
(SocialSiteId.PLURK, social_path_format_adv('plurk.com', RE_ID), ),
|
||||
(
|
||||
SocialSiteId.PLURK,
|
||||
social_path_format_adv('plurk.com', RE_ID),
|
||||
),
|
||||
# Linked in
|
||||
(
|
||||
SocialSiteId.LINKEDIN_PERSONAL_PROFILE,
|
||||
social_path_format_adv('linkedin.com', 'in', RE_ID),
|
||||
),
|
||||
# Google Blogger
|
||||
(SocialSiteId.GOOGLE_BLOGGER_PAGE, re_social_subdomain('blogspot.com'), ),
|
||||
(
|
||||
SocialSiteId.GOOGLE_BLOGGER_PAGE,
|
||||
re_social_subdomain('blogspot.com'),
|
||||
),
|
||||
# Cara
|
||||
(SocialSiteId.CARA_PROFILE, social_path_format_adv('cara.app', RE_ID, RE_ANY_SUBPATH), ),
|
||||
(
|
||||
SocialSiteId.CARA_PROFILE,
|
||||
social_path_format_adv('cara.app', RE_ID, RE_ANY_SUBPATH),
|
||||
),
|
||||
]
|
||||
|
||||
REGEXES: list[tuple[str, object]] = [
|
||||
|
@ -491,12 +543,17 @@ def to_parse_result(url: str | urllib.parse.ParseResult) -> urllib.parse.ParseRe
|
|||
msg = f'Expected {urllib.parse.ParseResult} or {str}'
|
||||
raise TypeError(msg)
|
||||
|
||||
def to_url(social_site_id: SocialSiteId, social_id: str) -> urllib.parse.ParseResult | None:
|
||||
for (ssi, fmt) in URL_FORMATS:
|
||||
|
||||
def to_url(
|
||||
social_site_id: SocialSiteId,
|
||||
social_id: str,
|
||||
) -> urllib.parse.ParseResult | None:
|
||||
for ssi, fmt in URL_FORMATS:
|
||||
if ssi == social_site_id:
|
||||
return to_parse_result(fmt.formatter.format(id=social_id))
|
||||
return None
|
||||
|
||||
|
||||
def determine_social_from_url(
|
||||
url_not_normalized: str | urllib.parse.ParseResult,
|
||||
) -> SocialLink | None:
|
||||
|
|
|
@ -212,7 +212,9 @@ NOT_PARSABLE = [
|
|||
PARSABLE_SOCIAL_IDS_COMBINED,
|
||||
)
|
||||
def test_parse_social_ids(
|
||||
url: str, expected_social_site_id: SocialSiteId, expected_social_id: str,
|
||||
url: str,
|
||||
expected_social_site_id: SocialSiteId,
|
||||
expected_social_id: str,
|
||||
) -> None:
|
||||
social_link: SocialLink | None = determine_social_from_url(url)
|
||||
assert social_link is not None, url
|
||||
|
@ -243,6 +245,7 @@ def test_determine_social_from_url_internally() -> None:
|
|||
with pytest.raises(TypeError):
|
||||
assert socials_util.determine_social_from_url_internally(None)
|
||||
|
||||
|
||||
def test_normalize_url():
|
||||
social_link = determine_social_from_url('http://twitter.com/dril')
|
||||
assert social_link is not None
|
||||
|
|
Loading…
Reference in New Issue
Block a user