From 6ae0984edff94c2546ef760608cddb3c38614e52 Mon Sep 17 00:00:00 2001 From: Niko Storni Date: Mon, 23 Sep 2019 19:42:21 +0200 Subject: [PATCH] add trending call use new resolve and trending remove old trending func --- app/helpers/lbrytv-sdk.js | 61 +++++++++++++++++++++++- app/sockets.js | 99 +++++++++++++++------------------------ 2 files changed, 98 insertions(+), 62 deletions(-) diff --git a/app/helpers/lbrytv-sdk.js b/app/helpers/lbrytv-sdk.js index d7c0ab8..5fdfca1 100644 --- a/app/helpers/lbrytv-sdk.js +++ b/app/helpers/lbrytv-sdk.js @@ -37,7 +37,7 @@ let resolve = function(urls) { return reject("DAEMON ERROR: resolve"); } - if (Object.prototype.hasOwnProperty.call(daemonResponse,"error")) { + if (Object.prototype.hasOwnProperty.call(daemonResponse, "error")) { messageSlack({ message: "```" + daemonResponse.error + "```", title: "DAEMON ERROR: resolve" @@ -51,7 +51,64 @@ let resolve = function(urls) { }; let getTrending = function() { - return; + return new Promise(function(resolve, reject) { + let options = { + method: "POST", + url: "https://api.lbry.tv/api/proxy", + headers: + { + "Content-Type": "application/json" + }, + body: + { + method: "claim_search", + params: + { + page_size: 20, + page: 1, + no_totals: true, + any_tags: + ["art", + "automotive", + "blockchain", + "comedy", + "economics", + "education", + "gaming", + "music", + "news", + "science", + "sports", + "technology"], + channel_ids: [], + not_channel_ids: [], + not_tags: ["porn", "nsfw", "mature", "xxx"], + order_by: ["trending_global", "trending_mixed"] + } + }, + json: true + }; + + request(options, function(error, response, daemonResponse) { + if (error) { + messageSlack({ + message: "```" + error + "```", + title: "DAEMON ERROR: trending" + }); + return reject("DAEMON ERROR: trending"); + } + + if (Object.prototype.hasOwnProperty.call(daemonResponse, "error")) { + messageSlack({ + message: "```" + daemonResponse.error + "```", + title: "DAEMON ERROR: trending" + }); + return reject("DAEMON ERROR: trending"); + } else + return resolve(daemonResponse.result.items); + + }); + }); }; export default { diff --git a/app/sockets.js b/app/sockets.js index 1b4cb54..847e70c 100644 --- a/app/sockets.js +++ b/app/sockets.js @@ -118,66 +118,54 @@ export default async(socket, action) => { function generateContent(exampleNumber, displayTrendingContent) { if (exampleNumber === 1) { - return getTrendingContent() + return lbrytvAPI.getTrending() .then(response => { - if (!response || !response.success || response.success !== true || !response.data) - return ""; - - const rawContentCollection = []; const renderedContentCollection = []; - const trendingContentData = response.data; + const urlsToResolve = []; - for (const data of trendingContentData) { - rawContentCollection.push(fetchMetadata({ - claim: data.url, - example: exampleNumber, - method: "resolve" - })); - } + response.forEach(r =>{ + urlsToResolve.push(r.canonical_url); + }); + lbrytvAPI.resolve(urlsToResolve) + .then(resolveResponse => { + if (resolveResponse !== null) { + let responses = Object.values(resolveResponse); - Promise.all(rawContentCollection) - .then(collection => { - for (const part of collection) { - if (part && part.value.tags && part.value.tags.includes("mature")) - continue; - if (part === undefined) - continue; - try { - renderedContentCollection.push(` -
-
+ for (let r in responses) { + let part = responses[r]; -
- ${part.value.title} -
- -
- ${part.signing_channel ? part.signing_channel.name : "Anon"} -
-
- `); - } catch(err) { - console.error(err); - return; // TODO: Return nice error message + if (part.value && part.value.thumbnail.url) { + renderedContentCollection.push(` +
+
+
+ +
+ ${part.value.title || "Untitled"} +
+ +
+ ${part.signing_channel ? part.signing_channel.name : "Anon"} +
+
+ `); + } } } - renderedContentCollection.push(` - - `); - + + `); displayTrendingContent(renderedContentCollection.join("")); }) .catch(error => { console.error(error); - return null; }); }); } @@ -199,7 +187,7 @@ function generateContent(exampleNumber, displayTrendingContent) { lbrytvAPI.resolve(approvedUrls) .then(resolveResponse => { - if (resolveResponse != null) { + if (resolveResponse !== null) { let responses = Object.values(resolveResponse); for (let r in responses) { @@ -217,11 +205,11 @@ function generateContent(exampleNumber, displayTrendingContent) {
- ${part.value.title} + ${part.value.title || "Untitled"}
- ${part.signing_channel.name || "Anon"} + ${part.signing_channel ? part.signing_channel.name : "Anon"}
`); @@ -378,15 +366,6 @@ function getGitHubUserToken(socket) { }); } -async function getTrendingContent() { - try { - const response = await got("https://api.lbry.com/file/list_trending"); - return JSON.parse(response.body); // eslint-disable-line padding-line-between-statements - } catch(error) { - return error; - } -} - function makeImageSourceSecure(url) { if (!url || !url.length) return url;