Skip to content

Instantly share code, notes, and snippets.

@fcavalcantirj
Created October 27, 2020 20:22
Show Gist options
  • Save fcavalcantirj/9903faa7dcb4002ece19522071860bc6 to your computer and use it in GitHub Desktop.
Save fcavalcantirj/9903faa7dcb4002ece19522071860bc6 to your computer and use it in GitHub Desktop.
POC to download instagram images without the API
const express = require('express')
const axios = require('axios');
const app = express()
const port = 3000
async function instagramPhotos (theProfile) {
// It will contain our photos' links
const res = []
try {
const userInfoSource = await axios.get(`https://www.instagram.com/${theProfile}/`)
// userInfoSource.data contains the HTML from Axios
const jsonObject = userInfoSource.data.match(/<script type="text\/javascript">window\._sharedData = (.*)<\/script>/)[1].slice(0, -1)
const userInfo = JSON.parse(jsonObject)
// Retrieve only the first 10 results
const mediaArray = userInfo.entry_data.ProfilePage[0].graphql.user.edge_owner_to_timeline_media.edges.splice(0, 10)
for (let media of mediaArray) {
const node = media.node
// Process only if is an image
if ((node.__typename && node.__typename !== 'GraphImage')) {
continue
}
// Push the thumbnail src in the array
res.push(node.thumbnail_src)
}
} catch (e) {
console.error('Unable to retrieve photos. Reason: ' + e.toString())
}
return res
}
app.get('/insta/:profile', async function(req, res) {
console.log(`fecthing profile [${req.params.profile}]`);
let images = await instagramPhotos(req.params.profile);
console.log(`found [${new Array(images).length}] images`);
res.send(JSON.stringify(images));
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment