Skip to content

Commit

Permalink
updating scrape.js for gatsbygram
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaikant committed Mar 30, 2018
1 parent 67246f1 commit b7a2fae
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions examples/gatsbygram/scrape.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,27 @@ const bar = new ProgressBar(
mkdirp.sync(`./data/images`)

let posts = []
let userId

// Write json
const saveJSON = _ =>
fs.writeFileSync(`./data/posts.json`, JSON.stringify(posts, ``, 2))

const getPosts = maxId => {
let url = `https://www.instagram.com/${username}/?__a=1`
if (maxId) url += `&max_id=${maxId}`
let url2 = `https://www.instagram.com/graphql/query/?query_hash=472f257a40c653c64c666ce877d59d2b`

if (maxId) url = url2 + `&variables={"id":"${userId}","first":12,"after":"${maxId}"}`

request(url, { encoding: `utf8` }, (err, res, body) => {
if (err) console.log(`error: ${err}`)
body = JSON.parse(body).graphql
if (maxId) {
body = JSON.parse(body).data
} else {
//This is the first request, lets get the userId
body = JSON.parse(body).graphql
userId = body.user.id
}
body.user.edge_owner_to_timeline_media.edges
.filter(({ node: item }) => item[`__typename`] === `GraphImage`)
.map(({ node: item }) => {
Expand Down Expand Up @@ -76,7 +85,7 @@ const getPosts = maxId => {
posts.push(item)
})

const lastId = get(body, `user.media.page_info.end_cursor`)
const lastId = get(body, `user.edge_owner_to_timeline_media.page_info.end_cursor`)
if (posts.length < 100 && lastId) getPosts(lastId)
else saveJSON()
})
Expand Down

0 comments on commit b7a2fae

Please sign in to comment.