Skip to content

Instantly share code, notes, and snippets.

@ahashem
Created October 3, 2018 10:21
Show Gist options
  • Save ahashem/6bda9a755ecbe8da4ec3eb9eef2ab5d4 to your computer and use it in GitHub Desktop.
Save ahashem/6bda9a755ecbe8da4ec3eb9eef2ab5d4 to your computer and use it in GitHub Desktop.
scrab list of all countries and cities from RTX platform using NodeJS
/* eslint-disable */
/**
* Get All Cities from all RTX platform countries
* To run:
* 1. Try the url in your browser and Get Cookie from your browser request.
* 2. install `request-promise`
* 2. run: `node cities.js`
*/
var request = require('request');
var rp = require('request-promise');
const AllCities = {}
var options = (geotype, parentId) => ({
method: 'GET',
url: 'https://app.rtxplatform.com/core/json/geosearch/pops',
qs: {geotype: geotype, parent_id: parentId},
headers:
{
Cookie: '//Session Cookie from RTX//',
},
});
getAllRegions = (countryId) => {
return rp(options('region', countryId))
.then(res => {
return JSON.parse(res).results;
})
.catch(error => {
throw new Error(error);
})
};
const getCitiesInRegion = (regoinId) => {
return rp(options('city', regoinId))
.then(res => {
return JSON.parse(res).results;
})
.catch(error => {
throw new Error(error);
})
};
const getCities = async () => {
// All 250 cities in RTX
for (let i = 1; i <= 250; i++) {
let countryCode;
const regionsInCountry = await getAllRegions(i);
if (regionsInCountry && regionsInCountry) {
const regions = regionsInCountry
countryCode = regions[0] && regions[0].parent_string;
AllCities[countryCode] = []
for (let j = 0; j < regions.length; j++) {
const cityParentId = regions[j].id;
const citiesInRegion = await getCitiesInRegion(cityParentId);
if (citiesInRegion) {
AllCities[countryCode].push(...citiesInRegion);
}
}
}
}
return AllCities;
}
module.exports = getCities();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment