From 9488e30a53858348231311e9a5bc667ce034b6f1 Mon Sep 17 00:00:00 2001 From: Dalton Date: Tue, 11 Dec 2018 23:15:37 -0800 Subject: [PATCH] async edits --- bot.js | 89 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 41 deletions(-) diff --git a/bot.js b/bot.js index 60d118d..8438c1e 100644 --- a/bot.js +++ b/bot.js @@ -190,67 +190,74 @@ function imgurRequest(subreddit, page_max) { return returnText; } -// Abstracted Reddit Json media grab. -// todo I think since it's randomly picking indices but children.length * 2 times, it's missing rare items. -function handleRedditJson(json) { - //let json = data.json(); - if (json.data) { - 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); - } +// Abstracted Reddit Json media grab. Will return a list of URLs! +function getUrlListFromReddit(json) { + + 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); } - // What if we don't find anything i.e. r/dogs but its valid subreddit. - return ""; + return url_list; } // Abstracted Imgur Json media grab. -function handleImgurJson(data) { - let json = data.json(); - let json_data = json.data[getRandomInt(0, json.data.length - 1)]; - if (json_data) { - return json_data.link; +function getUrlListFromImgur(json) { + + let url_list = []; + for (let i = 0; i < json.data.length; i++) { + if (json.data[i]) { + url_list.push(json.data.link); + } } + return url_list; } // Abstracted Reddit Booru Json media grab. -function handleRedditBooruJson(data) { +function getUrlListFromRedditBooru(json) { - let json = data.json(); - return json[getRandomInt(0, json.length)].cdnUrl; + let url_list = []; + for (let i = 0; i < json.length; i++) { + if (json[i]) { + url_list.push(json[i].cdnUrl); + } + } + return url_list; } async function loadJson(url) { let response = await fetch(url); - if (response.status == 200) { + if (response.status === 200) { return response.json(); } else { - throw new HttpError(response); + console.log(response); } } -// Makes an async url request with the provided function: 'process'. -async function asyncReddit(channelMessage) { - - // test command for async site request - +// Makes an async url request parsing a user message. +async function asyncImageRequest(channelMessage) { + // Split the user message let messageSplit = channelMessage.content.substring(2).split(' '); - if (messageSplit.length === 2) { - let reddit_url = "https://www.reddit.com/r/" - + messageSplit[1].replace(/[^a-zA-Z0-9_\-]+/g, '') - + "/.json?show=all&count=25&limit=100"; - let reddit_json = await loadJson(reddit_url); - let reddit_response = handleRedditJson(reddit_json); - sendToBotTestingChannel("reddit_url:" + reddit_url + "\nreddit_json:" + reddit_json + "\nreddit_response:" + reddit_response); - } else { - sendToBotTestingChannel("message split length:" + messageSplit.length); + // todo locked down to one search term. + if (messageSplit.length === 2) { + //regex clean the requested item to help with silly typos. + let requested_item = messageSplit[1].replace(/[^a-zA-Z0-9_\-]+/g, ''); + + //collect all the json + let image_url_list = []; + let reddit_json = await loadJson("https://www.reddit.com/r/" + requested_item + "/.json?show=all&count=25&limit=100"); + let imgur_json = await loadJson('https://api.imgur.com/3/gallery/r/' + requested_item + '/time/' + getRandomInt(1, 5)); + let redditbooru_json = await loadJson("https://" + requested_item + ".redditbooru.com/images/?limit=1000"); + + image_url_list.concat(getUrlListFromReddit(reddit_json), getUrlListFromImgur(imgur_json), getUrlListFromRedditBooru(redditbooru_json)); + + if (image_url_list) { + return image_url_list[getRandomInt(0, image_url_list - 1)]; + } else { + return "I couldn't find that, sauce?"; + } } } @@ -658,7 +665,7 @@ client.on('message', message => { break; case 'async': - asyncReddit(message); + asyncImageRequest(message); break; // Voice commands