148 lines
4.4 KiB
Python
148 lines
4.4 KiB
Python
from dataclasses import dataclass
|
|
from enforce_typing import enforce_types
|
|
from typing import List, Set, Optional, Union
|
|
from enum import Enum
|
|
import datetime
|
|
import re
|
|
import urllib.parse
|
|
|
|
class SocialSiteId(Enum):
|
|
REDDIT = 1
|
|
TWITTER = 2
|
|
RSS_FEED = 3
|
|
PAGE_WATCH = 4
|
|
LINKTREE_PAGE = 5
|
|
TWITCH = 6
|
|
WIKIDATA = 7
|
|
SONGKICK_ARTIST = 8
|
|
TUMBLR = 9
|
|
MASTODON_PAGE = 10
|
|
INSTAGRAM_PAGE = 11
|
|
PATREON_PAGE = 12
|
|
ARTSTATION_PAGE = 13
|
|
INPRNT_PAGE = 14
|
|
FACEBOOK_PAGE = 15
|
|
EMAIL = 16
|
|
JSON_LD = 17 # Similar to PAGE_WATCH, but focused on embedded microdata
|
|
SUBSTACK = 18
|
|
|
|
def wikidata_property(self, client):
|
|
return client.get(WIKIDATA_PROPERTIES[self])
|
|
|
|
@enforce_types
|
|
@dataclass(frozen = True)
|
|
class SocialLink(object):
|
|
url: urllib.parse.ParseResult
|
|
social_site_id: SocialSiteId
|
|
social_id: Optional[str]
|
|
|
|
WIKIDATA_PROPERTIES = {
|
|
SocialSiteId.EMAIL: 968,
|
|
SocialSiteId.FACEBOOK_PAGE: 2013,
|
|
SocialSiteId.INSTAGRAM_PAGE: 2003,
|
|
SocialSiteId.LINKTREE_PAGE: 11079,
|
|
SocialSiteId.REDDIT: 3984,
|
|
SocialSiteId.RSS_FEED: 1019,
|
|
SocialSiteId.SONGKICK_ARTIST: 3478,
|
|
SocialSiteId.TWITCH: 5797,
|
|
SocialSiteId.TWITTER: 2002,
|
|
SocialSiteId.WIKIDATA: 43649390,
|
|
|
|
#SocialSiteId.TUMBLR: 2000 + 9,
|
|
#SocialSiteId.MASTODON_PAGE: 2000 + 10,
|
|
#SocialSiteId.PATREON_PAGE: 2000 + 12,
|
|
#SocialSiteId.ARTSTATION_PAGE: 2000 + 13,
|
|
#SocialSiteId.INPRNT_PAGE: 2000 + 14,
|
|
}
|
|
|
|
REDDIT_SUBSCRIPTION_URL = r'^(?:https?:\/\/)?(?:old\.)?reddit\.com\/r\/(\w+)\/?$'
|
|
TWITTER_HANDLE_URL = r'^(?:https?:\/\/)?(?:www\.)?twitter\.com\/(\w+)\/?$'
|
|
LINKTREE_PAGE_URL = r'^(?:https?:\/\/)?(?:www\.)?linktr\.ee\/(\w+)\/?$'
|
|
TWITCH_STREAM_URL = r'^(?:https?:\/\/)?(?:www\.)?twitch\.tv\/(\w+)\/?$'
|
|
WIKIDATA_ITEM_URL = r'^(?:https?:\/\/)?(?:www\.)?wikidata\.org\/wiki\/(\w+)\/?$'
|
|
SONGKICK_ARTIST_URL = r'^(?:https?:\/\/)?(?:www\.)?songkick\.com\/artists\/(\d+)([\w-]*)\/?$'
|
|
TUMBLR_PAGE_URL = r'^(?:https?:\/\/)?(?:www\.)?tumblr\.com\/([\w-]+)(?:\/|\/rss)?\/?$'
|
|
TUMBLR_PAGE_URL_2 = r'^(?:https?:\/\/)?(\w+)\.tumblr\.com\/?$'
|
|
INSTAGRAM_URL = r'^(?:https?:\/\/)?(?:www\.)?instagram\.com\/([\w_.-]+)\/?$'
|
|
PATREON_URL = r'^(?:https?:\/\/)?(?:www\.)?patreon\.com\/([\w-]+)\/?$'
|
|
ARTSTATION_URL = r'^(?:https?:\/\/)?(?:www\.)?artstation\.com\/([\w-]+)\/?$'
|
|
INPRNT_URL = r'^(?:https?:\/\/)?(?:www\.)?inprnt\.com\/gallery\/([\w-]+)\/?$'
|
|
MAILTO_URL = r'^mailto:(?:[\w._.]+@[\w._.]+)$'
|
|
FACEBOOK_PAGE_URL = r'^(?:https?:\/\/)?(?:www\.)?facebook\.com\/([\w-]+)\/?$'
|
|
SUBSTACK_PREFIX_URL = r'^(?:https?:\/\/)?(\w+)\.substack\.com\/?$'
|
|
|
|
REGEXES = [
|
|
# Subreddits
|
|
(REDDIT_SUBSCRIPTION_URL, SocialSiteId.REDDIT),
|
|
|
|
# Twitter
|
|
(TWITTER_HANDLE_URL, SocialSiteId.TWITTER),
|
|
|
|
# Facebook
|
|
(FACEBOOK_PAGE_URL, SocialSiteId.FACEBOOK_PAGE),
|
|
|
|
# Linktr.ee
|
|
(LINKTREE_PAGE_URL, SocialSiteId.LINKTREE_PAGE),
|
|
|
|
# Twitch.tv
|
|
(TWITCH_STREAM_URL, SocialSiteId.TWITCH),
|
|
|
|
# Wikidata
|
|
(WIKIDATA_ITEM_URL, SocialSiteId.WIKIDATA),
|
|
|
|
# Songkick
|
|
(SONGKICK_ARTIST_URL, SocialSiteId.SONGKICK_ARTIST),
|
|
|
|
# Tumblr
|
|
(TUMBLR_PAGE_URL, SocialSiteId.TUMBLR),
|
|
(TUMBLR_PAGE_URL_2, SocialSiteId.TUMBLR),
|
|
|
|
# Instagram
|
|
(INSTAGRAM_URL, SocialSiteId.INSTAGRAM_PAGE),
|
|
|
|
# Patreon
|
|
(PATREON_URL, SocialSiteId.PATREON_PAGE),
|
|
|
|
# Artstation
|
|
(ARTSTATION_URL, SocialSiteId.ARTSTATION_PAGE),
|
|
|
|
# Inprnt
|
|
(INPRNT_URL, SocialSiteId.INPRNT_PAGE),
|
|
|
|
# Email
|
|
(MAILTO_URL, SocialSiteId.EMAIL),
|
|
|
|
# Substack
|
|
(SUBSTACK_PREFIX_URL, SocialSiteId.SUBSTACK),
|
|
]
|
|
|
|
def determine_social_from_url_internally(url):
|
|
|
|
# Regexes
|
|
for (social_site_url_regex, social_site_id) in REGEXES:
|
|
if m := re.match(social_site_url_regex, url):
|
|
groups = m.groups()
|
|
return (social_site_id, groups[0] if len(groups) > 0 else None)
|
|
|
|
# Mastodon
|
|
if 'mastodon' in url:
|
|
return (SocialSiteId.MASTODON_PAGE, None)
|
|
|
|
# Feed (?)
|
|
elif 'feed' in url or 'xml' in url or 'rss' in url or 'atom' in url:
|
|
return (SocialSiteId.RSS_FEED, None)
|
|
|
|
return (None, None)
|
|
|
|
def determine_social_from_url(url):
|
|
if isinstance(url, str):
|
|
url = urllib.parse.urlparse(url)
|
|
(social_site_id, social_id) = determine_social_from_url_internally(url.geturl())
|
|
|
|
if not social_site_id:
|
|
return None
|
|
return SocialLink(url, social_site_id, social_id)
|
|
|
|
assert determine_social_from_url('http://www.twitter.com/dril').social_id == 'dril'
|
|
assert determine_social_from_url('http://worstdril.tumblr.com/')
|