From a51366e6048426203bd4777ce352e1af5ff24a98 Mon Sep 17 00:00:00 2001 From: Florrie Date: Mon, 19 Nov 2018 23:37:27 -0400 Subject: Explore projects, not users --- index.js | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/index.js b/index.js index 27552c9..001ce39 100755 --- a/index.js +++ b/index.js @@ -308,26 +308,21 @@ const getData = function(request) { }) } -const explore = { - users: async () => { - const getUrl = mode => { - const limit = Math.ceil(Math.random() * 40) - const offset = Math.floor(Math.random() * 400) - return `https://api.scratch.mit.edu/explore/projects?mode=${mode}&limit=${limit}&offset=${offset}` - } +const getExploreProjects = async () => { + const getUrl = mode => { + const limit = Math.ceil(Math.random() * 40) + const offset = Math.floor(Math.random() * 400) + return `https://api.scratch.mit.edu/explore/projects?mode=${mode}&limit=${limit}&offset=${offset}` + } - const fetchResults = await Promise.all(['recent', 'trending', 'popular'].map(m => fetch(getUrl(m)).then(res => res.json()))) - const projects = fetchResults.reduce((acc, arr) => acc.concat(arr), []) + const fetchResults = await Promise.all(['recent', 'trending', 'popular'].map(m => fetch(getUrl(m)).then(res => res.json()))) + let projects = fetchResults.reduce((acc, arr) => acc.concat(arr), []) - // Dumb "remove duplicates" code incoming! - let authors = projects.map(project => project.author) - const seenUsernames = [] - authors = authors.filter( - ({ username: un }) => seenUsernames.includes(un) ? false : (seenUsernames.push(un), true)) + // Dumb "remove duplicates" code incoming! + const seenIDs = [] + projects = projects.filter(({ id }) => seenIDs.includes(id) ? false : (seenIDs.push(id), true)) - const selected = shuffle(projects).slice(0, 15) - return selected.map(project => project.author) - } + return shuffle(projects).slice(0, 12) } const handleRequest = async (request, response) => { @@ -593,17 +588,17 @@ const handleRequest = async (request, response) => { } } - if (urlParts[0] === 'explore') { - if (compareArr(urlParts.slice(1), ['users'])) { - const users = await explore.users() + if (compareArr(urlParts, ['explore'])) { + const projects = await getExploreProjects() - return page(request, response, fixWS` -

Explore Users

-

Here are some randomly picked users to check out:

-

${users.map(u => templates.user(u.username, u.id)).join(', ')}

-

(Users are randomly selected from the authors of a variety of recent and trending projects.)

- `) - } + return page(request, response, fixWS` +

Explore

+

Here are some randomly picked projects to check out:

+ +

(Results are randomly selected from a variety of recent and trending projects.)

+ `) } if (compareArr(urlParts, ['style.css'])) { -- cgit 1.3.0-6-gf8a5