Ensuring affiliate URL is valid.

This commit is contained in:
DebaucheryLibrarian 2026-01-30 22:39:34 +01:00
parent bff3bc6a0b
commit 5ae3b5d91c
2 changed files with 22 additions and 10 deletions

View File

@ -1,16 +1,20 @@
import format from 'template-format'; import format from 'template-format';
function getWatchUrl(scene) { function getWatchUrl(scene) {
try {
if (scene.url) { if (scene.url) {
return scene.url; return new URL(scene.url).href;
} }
if (scene.channel && (scene.channel.isIndependent || scene.channel.type === 'network')) { if (scene.channel && (scene.channel.isIndependent || scene.channel.type === 'network')) {
return scene.channel.url; return new URL(scene.channel.url).href;
} }
if (scene.network) { if (scene.network) {
return scene.network.url; return new URL(scene.network.url).href;
}
} catch (_error) {
// invalid URL
} }
return null; return null;
@ -73,8 +77,16 @@ export function getAffiliateSceneUrl(scene) {
return watchUrl; return watchUrl;
} }
function getEntityUrl(entity) {
try {
return new URL(entity.url || entity.parent?.url).href;
} catch (_error) {
return null;
}
}
export function getAffiliateEntityUrl(entity) { export function getAffiliateEntityUrl(entity) {
const entityUrl = entity.url || entity.parent?.url; const entityUrl = getEntityUrl(entity);
if (!entityUrl) { if (!entityUrl) {
return null; return null;

View File

@ -39,7 +39,7 @@ export function curateEntity(entity, context) {
}, },
}; };
curatedEntity.affiliateUrl = getAffiliateEntityUrl(curatedEntity, curatedEntity); curatedEntity.affiliateUrl = getAffiliateEntityUrl(curatedEntity);
return curatedEntity; return curatedEntity;
} }