Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updating scrape.js for gatsbygram #4777

Merged
merged 2 commits into from
Mar 30, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
updating scrape.js for gatsbygram
  • Loading branch information
Jaikant committed Mar 30, 2018
commit b7a2fae7a7fee31d50118516157047b42d38a092
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