Created
March 22, 2024 15:19
-
-
Save RReverser/4d7d53285d71217922bb3099bec958eb to your computer and use it in GitHub Desktop.
Resolve locations from all checkins from your Foursquare GDPR data export
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { readFile, writeFile } from 'fs/promises'; | |
// Get a Places API key at https://foursquare.com/developers/home. | |
const FOURSQUARE_PLACES_API_KEY = 'XXX'; | |
// First, collect all deduplicated venue IDs. | |
let venueIds = new Set(); | |
for (let i = 1; ; i++) { | |
try { | |
var json = require(`./checkins${i}.json`); | |
} catch (e) { | |
break; | |
} | |
for (let item of json.items) { | |
try { | |
venueIds.add(item.venue.id); | |
} catch (e) { | |
console.warn('Could not process', item); | |
console.warn(e); | |
} | |
} | |
} | |
let venueInfo = Object.create(null); | |
// Try to load an earlier snapshot in case there was a fatal error. | |
try { | |
venueInfo = JSON.parse(await readFile('venues-data.json', 'utf8')); | |
} catch {} | |
try { | |
for (let [i, id] of venueIds.entries()) { | |
// Ignore already resolved venue IDs from previous snapshot. | |
if (id in venueInfo) { | |
continue; | |
} | |
console.log(`Processing venue ${i + 1}/${venueIds.size}`); | |
// Resolve GPS coordinates and Foursquare's own geocoding. | |
const res = await fetch(`https://api.foursquare.com/v3/places/${id}?fields=geocodes,location`, { | |
headers: { | |
'Accept': 'application/json', | |
'Authorization': FOURSQUARE_PLACES_API_KEY | |
} | |
}); | |
let json; | |
try { | |
if (!res.ok) { | |
throw new Error(`HTTP ${res.status}: ${res.statusText}`); | |
} | |
// Store resolved data. | |
json = await res.json(); | |
} catch (e) { | |
// If there's non-recoverable error (e.g. venue is 404 now), just store it as-is, not much we can do. | |
json = { error: e.message }; | |
} | |
venueInfo[id] = json; | |
} | |
} finally { | |
// If we finished for whatever reason - either success or some fatal error - save the snapshot to a JSON file. | |
await writeFile('venues-data.json', JSON.stringify(venueInfo, null, 2)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment