2021-07-01 03:24:02 +02:00
|
|
|
const { URL, SITE_NAME, LBRY_WEB_API } = require('../../config.js');
|
|
|
|
const { Lbry } = require('lbry-redux');
|
2021-06-30 08:02:09 +02:00
|
|
|
const Feed = require('feed').Feed;
|
|
|
|
|
2021-07-01 03:24:02 +02:00
|
|
|
const SDK_API_PATH = `${LBRY_WEB_API}/api/v1`;
|
|
|
|
const proxyURL = `${SDK_API_PATH}/proxy`;
|
|
|
|
Lbry.setDaemonConnectionString(proxyURL);
|
2021-06-30 08:02:09 +02:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getChannelClaim(claimId) {
|
|
|
|
const options = {
|
|
|
|
claim_ids: [claimId],
|
|
|
|
page_size: 1,
|
|
|
|
no_totals: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const claims = await doClaimSearch(options);
|
|
|
|
return claims ? claims[0] : undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getClaimsFromChannel(claimId, count) {
|
|
|
|
const options = {
|
|
|
|
channel_ids: [claimId],
|
|
|
|
page_size: count,
|
|
|
|
has_source: true,
|
|
|
|
claim_type: 'stream',
|
|
|
|
order_by: ['creation_timestamp'],
|
|
|
|
no_totals: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
return await doClaimSearch(options);
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getFeed(channelClaim) {
|
|
|
|
const replaceLineFeeds = (str) => str.replace(/(?:\r\n|\r|\n)/g, '<br>');
|
|
|
|
|
2021-07-01 03:24:02 +02:00
|
|
|
const value = channelClaim.value;
|
|
|
|
const title = value ? value.title : channelClaim.name;
|
|
|
|
|
2021-06-30 08:02:09 +02:00
|
|
|
const options = {
|
2021-07-01 03:24:02 +02:00
|
|
|
title: title + ' on ' + SITE_NAME,
|
|
|
|
description: value ? replaceLineFeeds(value.description) : '',
|
2021-06-30 08:02:09 +02:00
|
|
|
link: `${URL}/${channelClaim.name}:${channelClaim.claim_id}`,
|
|
|
|
favicon: URL + '/public/favicon.png',
|
|
|
|
generator: SITE_NAME + ' RSS Feed',
|
2021-07-01 03:24:02 +02:00
|
|
|
image: value ? value.thumbnail.url : '',
|
2021-06-30 08:02:09 +02:00
|
|
|
author: {
|
|
|
|
name: channelClaim.name,
|
|
|
|
link: URL + '/' + channelClaim.name + ':' + channelClaim.claim_id,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const feed = new Feed(options);
|
|
|
|
|
2021-07-01 03:24:02 +02:00
|
|
|
const latestClaims = await getClaimsFromChannel(channelClaim.claim_id, 50);
|
2021-06-30 08:02:09 +02:00
|
|
|
|
|
|
|
latestClaims.forEach((c) => {
|
2021-07-01 03:24:02 +02:00
|
|
|
const meta = c.meta;
|
|
|
|
const value = c.value;
|
|
|
|
|
2021-06-30 08:02:09 +02:00
|
|
|
feed.addItem({
|
|
|
|
guid: c.claim_id,
|
|
|
|
id: c.claim_id,
|
2021-07-01 03:24:02 +02:00
|
|
|
title: value ? value.title : c.name,
|
|
|
|
description: value ? replaceLineFeeds(value.description) : '',
|
|
|
|
image: value ? value.thumbnail.url : '',
|
2021-06-30 08:02:09 +02:00
|
|
|
link: URL + '/' + c.name + ':' + c.claim_id,
|
2021-07-01 03:24:02 +02:00
|
|
|
date: new Date(meta ? meta.creation_timestamp * 1000 : null),
|
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-07-01 03:24:02 +02:00
|
|
|
const channelClaim = await getChannelClaim(ctx.params.claimId);
|
|
|
|
if (typeof channelClaim === 'string' || !channelClaim) {
|
|
|
|
return channelClaim;
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:02 +02:00
|
|
|
const feed = await getFeed(channelClaim);
|
|
|
|
return feed.rss2();
|
2021-06-30 08:02:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = { getRss };
|