Skip to content

Instantly share code, notes, and snippets.

@christineywang
Last active December 20, 2018 17:44
Show Gist options
  • Save christineywang/cfa3e57e6e945f0828cb3d40d4f54f9d to your computer and use it in GitHub Desktop.
Save christineywang/cfa3e57e6e945f0828cb3d40d4f54f9d to your computer and use it in GitHub Desktop.
const fs = require('fs');
const https = require('https');
const fetch = require('node-fetch');
fs.readFile('./output2.json', 'utf8', function(err, data) {
if (err) throw err;
const venues = JSON.parse(data);
const fetches = venues.map(venue => {
const name = encodeURIComponent(venue.name);
const ll = encodeURIComponent(venue.ll);
//const address = encodeURIComponent(venue.address);
const city = encodeURIComponent(venue.city);
//const state = encodeURIComponent(venue.state);
const zip = encodeURIComponent(venue.zip);
//const phone = venue.phone;
//const url = `https://api.foursquare.com/v2/venues/add?name=${name}&ll=${ll}&address=${address}&city=${city}&state=${state}&zip=${zip}&phone=${phone}&primaryCategoryId=4bf58dd8d48988d16e941735&oauth_token=<token>&v=20181201`;
const url = `https://api.foursquare.com/v2/venues/add?name=${name}&ll=${ll}&city=${city}&zip=${zip}&primaryCategoryId=4bf58dd8d48988d16e941735&oauth_token=<token>&v=20181201`;
return fetch(url, {
method: 'post'
}).then(res => {
return res.json()
}).then(data => {
const statusCode = data.meta.code;
if (statusCode === 409) {
const duplicateVenueId = JSON.stringify(data.response.candidateDuplicateVenues[0].id);
const duplicateVenueName = JSON.stringify(data.response.candidateDuplicateVenues[0].name);
return {
id: duplicateVenueId,
duplicateVenueName,
status: 'duplicated'
}
}
if (statusCode === 200) {
const newVenueId = data.response.id;
const newVenueName = data.response.name;
return {
id: newVenueId,
newVenueName,
status: 'new'
}
}
if (statusCode === 400) {
const requestId = JSON.stringify(data.meta.requestId);
const errorMessage = JSON.stringify(data.meta.errorDetail);
return {
requestId,
errorMessage
}
}
}).catch(error => {
console.log(error)
});
})
// The venues will have the following shape:
// { id: number, status: 'duplicated' or 'new' } or
// { requestId: number, errorMessage: string }
Promise.all(fetches).then((venues) => {
console.log(venues);
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment