Files
redux-scraper/app/helpers/domain/domains_helper.rb
2025-02-24 23:45:35 +00:00

102 lines
2.6 KiB
Ruby

# typed: strict
module Domain::DomainsHelper
extend T::Sig
extend T::Helpers
include HelpersInterface
abstract!
ALLOWED_EXTERNAL_LINK_DOMAINS = %w[
youtube.com
x.com
weasyl.com
vimeo.com
twitter.com
twitch.tv
tumblr.com
t.me
spreadshirt.de
spreadshirt.com
redbubble.com
pixiv.net
pinterest.com
patreon.com
mstdn.social
livejournal.com
ko-fi.com
instagram.com
facebook.com
dribbble.com
discord.gg
deviantart.com
bsky.app
behance.net
gumroad.com
bigcartel.com
furaffinity.net
].freeze
DOMAIN_TO_ICON_PATH =
T.let(
{
"x.com" => "x-twitter.png",
"wixmp.com" => "deviantart.png",
"weasyl.com" => "weasyl.png",
"twitter.com" => "x-twitter.png",
"t.me" => "telegram.png",
"pixiv.net" => "pixiv.png",
"patreon.com" => "patreon.png",
"newgrounds.com" => "newgrounds.png",
"itaku.ee" => "itaku.png",
"inkbunny.net" => "inkbunny.png",
"ib.metapix.net" => "inkbunny.png",
"furaffinity.net" => "fa.png",
"e621.net" => "e621.png",
"deviantart.com" => "deviantart.png",
"bsky.app" => "bsky.png",
"redbubble.com" => "redbubble.png",
"spreadshirt.de" => "spreadshirt.png",
"spreadshirt.com" => "spreadshirt.png",
}.freeze,
T::Hash[String, String],
)
DOMAIN_TITLE_MAPPERS =
T.let(
[
[%r{://t.me/([^/]+)}, ->(match) { match[1] }],
[%r{://bsky.app/profile/([^/]+)}, ->(match) { match[1] }],
[%r{://(.*\.)?x.com/([^/]+)}, ->(match) { match[2] }],
[%r{://(.*\.)?twitter.com/([^/]+)}, ->(match) { match[2] }],
[%r{://(.*\.)?patreon.com/([^/]+)}, ->(match) { match[2] }],
[%r{://(.*\.)?furaffinity.net/user/([^/]+)}, ->(match) { match[2] }],
],
T::Array[[Regexp, T.proc.params(match: MatchData).returns(String)]],
)
sig { params(domain: String, host: String).returns(T::Boolean) }
def url_matches_domain?(domain, host)
host == domain || host.end_with?(".#{domain}")
end
sig { params(domain: String).returns(T.nilable(String)) }
def icon_path_for_domain(domain)
for test_domain, icon in DOMAIN_TO_ICON_PATH
if url_matches_domain?(test_domain, domain)
return asset_path("domain-icons/#{icon}")
end
end
nil
end
sig { params(url: String).returns(String) }
def title_for_url(url)
url = url.to_s
for mapper in DOMAIN_TITLE_MAPPERS
if (match = mapper[0].match(url)) && (group = mapper[1].call(match))
return group
end
end
url
end
end