Skip to content

Instantly share code, notes, and snippets.

@sairion
Created August 28, 2016 10:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sairion/cc1017e7e3d85eb7fd059dde36842355 to your computer and use it in GitHub Desktop.
Save sairion/cc1017e7e3d85eb7fd059dde36842355 to your computer and use it in GitHub Desktop.
Get Instagram shared data via fetching HTML (i.e. location data)
let __DEBUG_HTML = '';
function fetchLoc(locID){
return fetch(`https://www.instagram.com/explore/locations/${locID}/`)
.then(res => res.text())
.then(txt => {
__DEBUG_HTML = txt;
const doc = parseInstgramExplorePage(txt);
const sharedData = extractSharedData(doc);
const locData = extractLocationData(sharedData);
return locData;
})
.catch(e => {
alert('Was unable to send request in current location, or there was another kind of error. Please see console.')
console.debug(e);
});
}
function parseInstgramExplorePage(txt) {
const dp = new DOMParser();
const doc = dp.parseFromString(txt, "text/html");
return doc;
}
function extractSharedData(doc) {
const scs = Array.from(doc.querySelectorAll('script'));
const sharedDataRawText = scs.filter(sc =>
sc.textContent.indexOf('_sharedData') > -1
)[0];
if (sharedDataRawText) {
const sharedDataJSONText = sharedDataRawText.textContent.trim().match(/\=\ (.*);/)[1];
let sharedData;
try {
sharedData = JSON.parse(sharedDataJSONText);
} catch(e) {
alert('Failed to parse data. See `window.__FAIL_TXT`');
window.__FAIL_TXT = sharedDataJSONText;
}
return sharedData;
}
}
function extractLocationData(sharedData) {
if (sharedData &&
sharedData.entry_data &&
sharedData.entry_data.LocationsPage &&
sharedData.entry_data.LocationsPage[0]
) {
return sharedData.entry_data.LocationsPage[0];
}
}
fetchLoc('8049032')
.then(locData => {
debugger;
});
@eteamin
Copy link

eteamin commented Dec 29, 2018

you saved my day sir. thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment