2021-08-02 09:25:47 +02:00
|
|
|
const { generateStreamUrl } = require('../../ui/util/web');
|
2021-10-17 10:36:14 +02:00
|
|
|
const { lbryProxy: Lbry } = require('../lbry');
|
2021-12-01 16:36:52 +01:00
|
|
|
const { URL, SITE_NAME, PROXY_URL } = require('../../config.js');
|
2021-08-06 22:53:01 +02:00
|
|
|
const Mime = require('mime-types');
|
2021-11-30 03:27:56 +01:00
|
|
|
const Rss = require('rss');
|
2021-06-30 08:02:09 +02:00
|
|
|
|
2021-12-01 16:36:52 +01:00
|
|
|
Lbry.setDaemonConnectionString(PROXY_URL);
|
2021-06-30 08:02:09 +02:00
|
|
|
|
2021-07-09 10:06:28 +02:00
|
|
|
const NUM_ENTRIES = 500;
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
// ****************************************************************************
|
|
|
|
// Fetch claim info
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2021-07-01 03:24:02 +02:00
|
|
|
async function doClaimSearch(options) {
|
|
|
|
let results;
|
|
|
|
try {
|
|
|
|
results = await Lbry.claim_search(options);
|
|
|
|
} catch {}
|
|
|
|
return results ? results.items : undefined;
|
|
|
|
}
|
|
|
|
|
2021-07-09 11:33:34 +02:00
|
|
|
async function getChannelClaim(name, claimId) {
|
|
|
|
let claim;
|
2021-08-02 05:42:31 +02:00
|
|
|
let error;
|
|
|
|
|
2021-07-09 11:33:34 +02:00
|
|
|
try {
|
|
|
|
const url = `lbry://${name}#${claimId}`;
|
|
|
|
const response = await Lbry.resolve({ urls: [url] });
|
|
|
|
if (response && response[url] && !response[url].error) {
|
|
|
|
claim = response && response[url];
|
|
|
|
}
|
|
|
|
} catch {}
|
2021-08-02 05:42:31 +02:00
|
|
|
|
|
|
|
if (!claim) {
|
|
|
|
error = 'The RSS URL is invalid or is not associated with any channel.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return { claim, error };
|
2021-07-01 03:24:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getClaimsFromChannel(claimId, count) {
|
|
|
|
const options = {
|
|
|
|
channel_ids: [claimId],
|
|
|
|
page_size: count,
|
|
|
|
has_source: true,
|
|
|
|
claim_type: 'stream',
|
2021-08-06 22:53:01 +02:00
|
|
|
order_by: ['release_time'],
|
2021-07-01 03:24:02 +02:00
|
|
|
no_totals: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
return await doClaimSearch(options);
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
// ****************************************************************************
|
|
|
|
// Helpers
|
|
|
|
// ****************************************************************************
|
|
|
|
|
2021-08-06 22:53:01 +02:00
|
|
|
function encodeWithSpecialCharEncode(string) {
|
|
|
|
// encodeURIComponent doesn't encode `'` and others
|
|
|
|
// which other services may not like
|
|
|
|
return encodeURIComponent(string).replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29');
|
|
|
|
}
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const generateEnclosureForClaimContent = (claim) => {
|
|
|
|
const value = claim.value;
|
|
|
|
if (!value || !value.stream_type) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2021-08-06 22:53:01 +02:00
|
|
|
const fileExt = value.source && value.source.media_type && '.' + Mime.extension(value.source.media_type);
|
2021-08-02 05:42:31 +02:00
|
|
|
|
|
|
|
switch (value.stream_type) {
|
|
|
|
case 'video':
|
2021-08-06 22:53:01 +02:00
|
|
|
return {
|
|
|
|
url: generateStreamUrl(claim.name, claim.claim_id) + (fileExt || '.mp4'),
|
|
|
|
type: (value.source && value.source.media_type) || 'video/mp4',
|
|
|
|
size: (value.source && value.source.size) || 0, // Per spec, 0 is a valid fallback.
|
|
|
|
};
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
case 'audio':
|
2021-08-06 22:53:01 +02:00
|
|
|
return {
|
2021-09-27 18:40:06 +02:00
|
|
|
url: generateStreamUrl(claim.name, claim.claim_id) + ((fileExt === '.mpga' ? '.mp3' : fileExt) || '.mp3'),
|
2021-08-06 22:53:01 +02:00
|
|
|
type: (value.source && value.source.media_type) || 'audio/mpeg',
|
|
|
|
size: (value.source && value.source.size) || 0, // Per spec, 0 is a valid fallback.
|
|
|
|
};
|
2021-08-02 05:42:31 +02:00
|
|
|
case 'image':
|
2021-08-06 22:53:01 +02:00
|
|
|
return {
|
|
|
|
url: generateStreamUrl(claim.name, claim.claim_id) + (fileExt || '.jpeg'),
|
|
|
|
type: (value.source && value.source.media_type) || 'image/jpeg',
|
|
|
|
size: (value.source && value.source.size) || 0, // Per spec, 0 is a valid fallback.
|
|
|
|
};
|
2021-08-02 05:42:31 +02:00
|
|
|
case 'document':
|
|
|
|
case 'software':
|
|
|
|
return {
|
2021-08-02 09:25:47 +02:00
|
|
|
url: generateStreamUrl(claim.name, claim.claim_id),
|
2021-08-02 05:42:31 +02:00
|
|
|
type: (value.source && value.source.media_type) || undefined,
|
|
|
|
size: (value.source && value.source.size) || 0, // Per spec, 0 is a valid fallback.
|
|
|
|
};
|
2021-07-16 14:24:42 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
default:
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const getLanguageValue = (claim) => {
|
2021-11-30 03:27:56 +01:00
|
|
|
const {
|
|
|
|
value: { languages },
|
|
|
|
} = claim;
|
|
|
|
|
|
|
|
return languages && languages.length > 0 ? languages[0] : 'en';
|
2021-08-02 05:42:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const replaceLineFeeds = (str) => str.replace(/(?:\r\n|\r|\n)/g, '<br />');
|
2021-07-16 14:24:42 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const isEmailRoughlyValid = (email) => /^\S+@\S+$/.test(email);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 'itunes:owner' is required by castfeedvalidator (w3c allows omission), and
|
|
|
|
* both name and email must be defined. The email must also be a "valid" one.
|
|
|
|
*
|
|
|
|
* Use a fallback email when the creator did not specify one. The email will not
|
|
|
|
* be shown to the user; it is just used for administrative purposes.
|
|
|
|
*
|
|
|
|
* @param claim
|
|
|
|
* @returns any
|
|
|
|
*/
|
|
|
|
const generateItunesOwnerElement = (claim) => {
|
|
|
|
let email = 'no-reply@odysee.com';
|
2021-11-30 03:27:56 +01:00
|
|
|
const { value } = claim;
|
|
|
|
const name = (value && value.title) || claim.name;
|
2021-08-02 05:42:31 +02:00
|
|
|
|
2021-11-30 03:27:56 +01:00
|
|
|
if (isEmailRoughlyValid(value.email)) {
|
|
|
|
email = value.email;
|
2021-08-02 05:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
'itunes:owner': [{ 'itunes:name': name }, { 'itunes:email': email }],
|
2021-07-09 11:57:59 +02:00
|
|
|
};
|
2021-08-02 05:42:31 +02:00
|
|
|
};
|
2021-06-30 08:02:09 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const generateItunesExplicitElement = (claim) => {
|
2021-08-06 22:53:01 +02:00
|
|
|
const tags = (claim && claim.value && claim.value.tags) || [];
|
2021-08-02 05:42:31 +02:00
|
|
|
return { 'itunes:explicit': tags.includes('mature') ? 'yes' : 'no' };
|
|
|
|
};
|
|
|
|
|
|
|
|
const getItunesCategory = (claim) => {
|
|
|
|
const itunesCategories = [
|
|
|
|
'Arts',
|
|
|
|
'Business',
|
|
|
|
'Comedy',
|
|
|
|
'Education',
|
|
|
|
'Fiction',
|
|
|
|
'Government',
|
|
|
|
'History',
|
|
|
|
'Health & Fitness',
|
|
|
|
'Kids & Family',
|
|
|
|
'Leisure',
|
|
|
|
'Music',
|
|
|
|
'News',
|
|
|
|
'Religion & Spirituality',
|
|
|
|
'Science',
|
|
|
|
'Society & Culture',
|
|
|
|
'Sports',
|
|
|
|
'Technology',
|
|
|
|
'True Crime',
|
|
|
|
'TV & Film',
|
|
|
|
];
|
|
|
|
|
2021-08-06 22:53:01 +02:00
|
|
|
const tags = (claim && claim.value && claim.value.tags) || [];
|
|
|
|
|
|
|
|
for (let i = 0; i < itunesCategories.length; ++i) {
|
|
|
|
const itunesCategory = itunesCategories[i];
|
|
|
|
if (tags.includes(itunesCategory.toLowerCase())) {
|
2021-08-02 05:42:31 +02:00
|
|
|
// "Note: Although you can specify more than one category and subcategory
|
|
|
|
// in your RSS feed, Apple Podcasts only recognizes the first category and
|
|
|
|
// subcategory."
|
|
|
|
// --> The only parse the first found tag.
|
2021-08-06 22:53:01 +02:00
|
|
|
return itunesCategory.replace('&', '&');
|
2021-07-16 14:24:42 +02:00
|
|
|
}
|
2021-08-02 05:42:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// itunes will not accept any other categories, and the element is required
|
|
|
|
// to pass castfeedvalidator. So, fallback to 'Leisure' (closes to "General")
|
|
|
|
// if the creator did not specify a tag.
|
|
|
|
return 'Leisure';
|
|
|
|
};
|
2021-07-16 14:24:42 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const generateItunesDurationElement = (claim) => {
|
|
|
|
let duration;
|
|
|
|
if (claim && claim.value) {
|
|
|
|
if (claim.value.video) {
|
|
|
|
duration = claim.value.video.duration;
|
|
|
|
} else if (claim.value.audio) {
|
|
|
|
duration = claim.value.audio.duration;
|
2021-07-16 14:24:42 +02:00
|
|
|
}
|
2021-08-02 05:42:31 +02:00
|
|
|
}
|
2021-07-16 14:24:42 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
if (duration) {
|
|
|
|
return { 'itunes:duration': `${duration}` };
|
|
|
|
}
|
|
|
|
};
|
2021-07-01 03:24:02 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const generateItunesImageElement = (claim) => {
|
|
|
|
const thumbnailUrl = (claim && claim.value && claim.value.thumbnail && claim.value.thumbnail.url) || '';
|
|
|
|
if (thumbnailUrl) {
|
|
|
|
return {
|
|
|
|
'itunes:image': { _attr: { href: thumbnailUrl } },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-11-30 03:27:56 +01:00
|
|
|
const getFormattedDescription = (claim) => replaceLineFeeds(claim.value.description || '');
|
2021-06-30 08:02:09 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
// ****************************************************************************
|
|
|
|
// Generate
|
|
|
|
// ****************************************************************************
|
|
|
|
|
|
|
|
function generateFeed(feedLink, channelClaim, claimsInChannel) {
|
|
|
|
// --- Channel ---
|
2021-08-06 22:53:01 +02:00
|
|
|
let channelTitle = (channelClaim.value && channelClaim.value.title) || channelClaim.name;
|
2022-02-13 21:25:18 +01:00
|
|
|
let channelURL = URL + '/' + channelClaim.canonical_url.replace('lbry://', '').replace(/#/g, ':');
|
2021-08-02 05:42:31 +02:00
|
|
|
const feed = new Rss({
|
2021-08-06 22:53:01 +02:00
|
|
|
title: channelTitle + ' on ' + SITE_NAME,
|
2021-08-02 05:42:31 +02:00
|
|
|
description: getFormattedDescription(channelClaim),
|
|
|
|
feed_url: feedLink,
|
2022-02-13 21:25:18 +01:00
|
|
|
site_url: (channelClaim.value && channelClaim.value.website_url) || channelURL,
|
2021-08-02 05:42:31 +02:00
|
|
|
image_url: (channelClaim.value && channelClaim.value.thumbnail && channelClaim.value.thumbnail.url) || undefined,
|
|
|
|
language: getLanguageValue(channelClaim),
|
|
|
|
custom_namespaces: { itunes: 'http://www.itunes.com/dtds/podcast-1.0.dtd' },
|
|
|
|
custom_elements: [
|
2021-08-06 22:53:01 +02:00
|
|
|
{ 'itunes:author': channelTitle },
|
2021-08-02 05:42:31 +02:00
|
|
|
{
|
|
|
|
'itunes:category': [
|
|
|
|
{
|
|
|
|
_attr: {
|
|
|
|
text: getItunesCategory(channelClaim),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
generateItunesImageElement(channelClaim),
|
|
|
|
generateItunesOwnerElement(channelClaim),
|
|
|
|
generateItunesExplicitElement(channelClaim),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
// --- Content ---
|
|
|
|
claimsInChannel.forEach((c) => {
|
|
|
|
const title = (c.value && c.value.title) || c.name;
|
|
|
|
const thumbnailUrl = (c.value && c.value.thumbnail && c.value.thumbnail.url) || '';
|
|
|
|
const thumbnailHtml = thumbnailUrl
|
|
|
|
? `<p><img src="${thumbnailUrl}" width="480" alt="thumbnail" title="${title}" /></p>`
|
|
|
|
: '';
|
|
|
|
const description = thumbnailHtml + getFormattedDescription(c);
|
|
|
|
|
2021-08-06 22:53:01 +02:00
|
|
|
const url = `${URL}/${encodeWithSpecialCharEncode(c.name)}:${c.claim_id}`;
|
2022-02-13 21:25:18 +01:00
|
|
|
const date =
|
|
|
|
c.value && c.value.release_time ? c.value.release_time * 1000 : c.meta && c.meta.creation_timestamp * 1000;
|
2021-08-06 22:53:01 +02:00
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
feed.item({
|
|
|
|
title: title,
|
|
|
|
description: description,
|
2021-08-06 22:53:01 +02:00
|
|
|
url: url,
|
2021-08-02 05:42:31 +02:00
|
|
|
guid: undefined, // defaults to 'url'
|
|
|
|
author: undefined, // defaults feed author property
|
2021-08-06 22:53:01 +02:00
|
|
|
date: new Date(date),
|
2021-08-02 05:42:31 +02:00
|
|
|
enclosure: generateEnclosureForClaimContent(c),
|
|
|
|
custom_elements: [
|
|
|
|
{ 'itunes:title': title },
|
2021-08-06 22:53:01 +02:00
|
|
|
{ 'itunes:author': channelTitle },
|
2021-08-02 05:42:31 +02:00
|
|
|
generateItunesImageElement(c),
|
|
|
|
generateItunesDurationElement(c),
|
|
|
|
generateItunesExplicitElement(c),
|
|
|
|
],
|
2021-06-30 08:02:09 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return feed;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getRss(ctx) {
|
|
|
|
if (!ctx.params.claimName || !ctx.params.claimId) {
|
|
|
|
return 'Invalid URL';
|
|
|
|
}
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const { claim: channelClaim, error } = await getChannelClaim(ctx.params.claimName, ctx.params.claimId);
|
|
|
|
if (error) {
|
|
|
|
return error;
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-02 05:42:31 +02:00
|
|
|
const latestClaimsInChannel = await getClaimsFromChannel(channelClaim.claim_id, NUM_ENTRIES);
|
|
|
|
const feed = generateFeed(`${URL}${ctx.request.url}`, channelClaim, latestClaimsInChannel);
|
|
|
|
return feed.xml();
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getRss };
|