From a3ad795ac217e1596c71cadf180fe803a21ebef8 Mon Sep 17 00:00:00 2001 From: Dalton Date: Tue, 11 Dec 2018 21:16:02 -0800 Subject: [PATCH] better handle reddit json logic --- bot.js | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/bot.js b/bot.js index 82f028e..a8c7204 100644 --- a/bot.js +++ b/bot.js @@ -188,13 +188,14 @@ function imgurRequest(subreddit, page_max) { function handleRedditJson(data) { let json = data.json(); if (json.data) { - for (let i = 0; i < json.data.children.length * 2; i++) { - let rand = getRandomInt(0, json.data.children.length - 1); - - if (json.data.children[rand].data.post_hint === "image" || json.data.children[rand].data.post_hint === "link" || json.data.children.data.post_hint === "rich:video") { - return json.data.children[rand].data.url; + sendToBotTestingChannel("json data pressseeeeent"); + let url_list = []; + for (let i = 0; i < json.data.children.length; i++) { + if (json.data.children[i].data.post_hint === "image" || json.data.children[i].data.post_hint === "link" || json.data.children[i].data.post_hint === "rich:video") { + url_list.push(json.data.children[i].data.url); } } + return url_list[getRandomInt(0, url_list.length - 1)]; } else { sendToBotTestingChannel("json data:" + json.data); } @@ -223,8 +224,6 @@ function asyncSiteRequest(url, process) { fetch(url) .then(data => { - sendToBotTestingChannel("data.data.children::" + data.data.children); - return process(data); }) .catch(error => {