1
0
socials-util/__init__.py

175 lines
5.6 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 # Should have been named REDDIT_SUBREDDIT
REDDIT_USER = 22
REDDIT_SUBREDDIT = REDDIT
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
ETSY_SHOP = 19
KO_FI = 20
BEHANCE_PAGE = 21
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_SUBREDDIT: 3984,
SocialSiteId.REDDIT_USER: 4265,
SocialSiteId.RSS_FEED: 1019,
SocialSiteId.SONGKICK_ARTIST: 3478,
SocialSiteId.TWITCH: 5797,
SocialSiteId.TWITTER: 2002,
SocialSiteId.WIKIDATA: 43649390,
SocialSiteId.TUMBLR: 3943,
#SocialSiteId.MASTODON_PAGE: 2000 + 10,
#SocialSiteId.PATREON_PAGE: 2000 + 12,
#SocialSiteId.ARTSTATION_PAGE: 2000 + 13,
#SocialSiteId.INPRNT_PAGE: 2000 + 14,
}
REDDIT_SUBREDDIT_URL = r'^(?:https?:\/\/)?(?:old\.)?reddit\.com\/r\/(\w+)\/?$'
REDDIT_USER_URL = r'^(?:https?:\/\/)?(?:old\.)?reddit\.com\/user\/(\w+)(?:|\/submitted)\/?$'
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\/?$'
ETSY_SHOP_URL = r'^(?:https?:\/\/)?(?:www\.)?etsy\.com\/shop\/([\w-]+)\/?$'
KO_FI_URL = r'^(?:https?:\/\/)?(?:www\.)?ko\-fi\.com\/([\w-]+)(?:|\/shop)\/?$'
BEHANCE_PAGE_URL = r'^(?:https?:\/\/)?(?:www\.)?behance\.net\/([\w-]+)\/?$'
REGEXES = [
# Reddit
(REDDIT_SUBREDDIT_URL, SocialSiteId.REDDIT_SUBREDDIT),
(REDDIT_USER_URL, SocialSiteId.REDDIT_USER),
# 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),
# Etsy shop
(ETSY_SHOP_URL, SocialSiteId.ETSY_SHOP),
# Ko-fi
(KO_FI_URL, SocialSiteId.KO_FI),
# Behance
(BEHANCE_PAGE_URL, SocialSiteId.BEHANCE_PAGE),
]
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, re.I):
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)
TEST = True
if TEST:
assert determine_social_from_url('http://www.twitter.com/dril').social_id == 'dril'
assert determine_social_from_url('http://worstdril.tumblr.com/')
assert determine_social_from_url('https://deep-dark-fears.tumblr.com').social_id == 'deep-dark-fears'
assert determine_social_from_url('https://www.etsy.com/shop/aleksiremesart').social_id == 'aleksiremesart'
assert determine_social_from_url('https://ko-fi.com/A627LI1/shop').social_id == 'A627LI1'
assert determine_social_from_url('https://ko-fi.com/A627LI1/').social_id == 'A627LI1'