Skip to content

Instantly share code, notes, and snippets.

@johnholdun
Created December 15, 2021 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnholdun/242e8e3ffb83c82daf860c338bbccc19 to your computer and use it in GitHub Desktop.
Save johnholdun/242e8e3ffb83c82daf860c338bbccc19 to your computer and use it in GitHub Desktop.
log in to instagram in your browser, then run this in the console. use at your own risk! works as of 2021-12-15 but is likely to break!
;(async () => {
const PATH = 'https://www.instagram.com/graphql/query/'
const QUERY_HASHES = {
list: '8c2a529969ee035a5063f2fc8602a0fd',
post: '7d4d42b121a214d23bd43206e5142c8c'
}
const get = (query, args) => {
const queryParams = { query_hash: QUERY_HASHES[query], variables: JSON.stringify(args) }
const queryString = new URLSearchParams(queryParams).toString()
const url = `${PATH}?${queryString}`
return fetch(url).then(r => r.json())
}
const getPhotos = async (userId) => {
let result = []
let after = ''
while (true) {
const response = await get('list', { id: userId, first: 50, after })
if (response.status !== 'ok') {
console.log('uh oh', response)
break
}
result = result.concat(
response
.data
.user
.edge_owner_to_timeline_media
.edges
.map(e => e.node)
.filter(n => n.__typename === 'GraphImage')
.map(node => ({
shortcode: node.shortcode,
url: node.display_url,
dimensions: node.dimensions,
taken_at_timestamp: node.taken_at_timestamp,
location: node.location,
caption: node.edge_media_to_caption && node.edge_media_to_caption.edges.length ? node.edge_media_to_caption.edges[0].node.text : null
})
)
)
if (!response.data.user.edge_owner_to_timeline_media.page_info.has_next_page) {
break
}
after = response.data.user.edge_owner_to_timeline_media.page_info.end_cursor
await new Promise((res) => setTimeout(res, 1000))
}
return result
}
const getPhoto = async (shortcode) => {
const response = await get('post', { shortcode, child_comment_count: 3, fetch_comment_count: 40, parent_comment_count: 24, has_threaded_comments: true })
if (response.status !== 'ok') {
console.log('uh oh', response)
return
}
const node = data.shortcode_media
const result = {
shortcode: node.shortcode,
url: node.display_url,
dimensions: node.dimensions,
taken_at_timestamp: node.taken_at_timestamp,
location: node.location,
caption: node.edge_media_to_caption && node.edge_media_to_caption.edges.length ? node.edge_media_to_caption.edges[0].node.text : null
}
return result
}
// I don't have a recommendation for an easy way to get your user ID, sorry!
console.log(JSON.stringify(await getPhotos('631832')))
// console.log(JSON.stringify(await getPhoto('CWr6fSareab'), null, 2))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment