Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions data/flatten.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// eslint-disable-next-line import/no-extraneous-dependencies
const got = require('got');
const flatten = require('lodash/flatten');

const client = got.extend({
json: true,
baseUrl: 'https://4kmaps.now.sh',
hooks: {
afterResponse: [
(response, retryWithMergedOptions) => {
if (response.statusCode >= 400 && response.statusCode !== 404) { // Unauthorized
return retryWithMergedOptions({});
}

if (response.statusCode === 404) {
return false;
}

// No changes otherwise
return response;
},
],
},
});

async function query(regionPath) {
const data = await client(`${regionPath}`);

if (!data) {
return [];
}

if (data.body.children.length === 0) {
return [data.body.regionKey];
}

const results = await Promise.all(data.body.children.map(c => query(c.href)));
return flatten(results);
}

// eslint-disable-next-line no-console
query(`/regions/${process.argv[2]}`).then(console.log).catch(console.error);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"eslint-config-airbnb-base": "^13.0.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^7.0.1",
"got": "^9.3.2",
"mocha": "^5.2.0",
"nodemon": "^1.18.3",
"rewire": "^4.0.1",
Expand Down
Loading