Sanitize URLs to compare just apex domain
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import {type I18n} from '@lingui/core'
|
||||
import {plural} from '@lingui/macro'
|
||||
import psl from 'psl'
|
||||
|
||||
export function displayDuration(i18n: I18n, durationInMinutes: number) {
|
||||
const roundedDurationInMinutes = Math.round(durationInMinutes)
|
||||
@@ -25,27 +26,48 @@ export function displayDuration(i18n: I18n, durationInMinutes: number) {
|
||||
|
||||
const serviceUrlToNameMap: Record<string, string> = {
|
||||
'twitch.tv': 'Twitch',
|
||||
'www.twitch.tv': 'Twitch',
|
||||
'youtube.com': 'YouTube',
|
||||
'www.youtube.com': 'YouTube',
|
||||
'youtu.be': 'YouTube',
|
||||
'nba.com': 'NBA',
|
||||
'www.nba.com': 'NBA',
|
||||
'nba.smart.link': 'nba.smart.link',
|
||||
'espn.com': 'ESPN',
|
||||
'www.espn.com': 'ESPN',
|
||||
'stream.place': 'Streamplace',
|
||||
'skylight.social': 'Skylight',
|
||||
'bluecast.app': 'Bluecast',
|
||||
'www.bluecast.app': 'Bluecast',
|
||||
}
|
||||
|
||||
export function getLiveServiceNames(domains: Set<string>) {
|
||||
console.log(domains)
|
||||
const names = Array.from(
|
||||
new Set(Array.from(domains.values()).map(d => serviceUrlToNameMap[d] || d)),
|
||||
new Set(
|
||||
Array.from(domains.values())
|
||||
.map(d => sanitizeLiveNowHost(d))
|
||||
.map(d => serviceUrlToNameMap[d] || d),
|
||||
),
|
||||
)
|
||||
return {
|
||||
names,
|
||||
formatted: names.join(', '),
|
||||
}
|
||||
}
|
||||
|
||||
export function sanitizeLiveNowHost(hostname: string) {
|
||||
// special case this one
|
||||
if (hostname === 'nba.smart.link') {
|
||||
return hostname
|
||||
}
|
||||
const parsed = psl.parse(hostname)
|
||||
if (parsed.error || !parsed.listed || !parsed.domain) {
|
||||
// fall back to dumb version
|
||||
return hostname.replace(/^www\./, '')
|
||||
}
|
||||
return parsed.domain
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts the apex domain from a given URL, for use when matching allowed
|
||||
* Live Now hosts.
|
||||
*/
|
||||
export function getLiveNowHost(url: string) {
|
||||
const {hostname} = new URL(url)
|
||||
return sanitizeLiveNowHost(hostname)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user