Instagram API: How to get all user media?

后端 未结 10 2101
灰色年华
灰色年华 2020-11-28 02:36

In general I need to get all user media.

User has more than 250 photos.

I do /users/1/media/recent/?access_token=...&count=250

But i

10条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 03:13

    In June 2016 Instagram made most of the functionality of their API available only to applications that have passed a review process. They still however provide JSON data through the web interface, and you can add the parameter __a=1 to a URL to only include the JSON data.

    max=
    while :;do
      c=$(curl -s "https://www.instagram.com/username/?__a=1&max_id=$max")
      jq -r '.user.media.nodes[]?|.display_src'<<<"$c"
      max=$(jq -r .user.media.page_info.end_cursor<<<"$c")
      jq -e .user.media.page_info.has_next_page<<<"$c">/dev/null||break
    done
    

    Edit: As mentioned in the comment by alnorth29, the max_id parameter is now ignored. Instagram also changed the format of the response, and you need to perform additional requests to get the full-size URLs of images in the new-style posts with multiple images per post. You can now do something like this to list the full-size URLs of images on the first page of results:

    c=$(curl -s "https://www.instagram.com/username/?__a=1")
    jq -r '.graphql.user.edge_owner_to_timeline_media.edges[]?|.node|select(.__typename!="GraphSidecar").display_url'<<<"$c"
    jq -r '.graphql.user.edge_owner_to_timeline_media.edges[]?|.node|select(.__typename=="GraphSidecar")|.shortcode'<<<"$c"|while read l;do
      curl -s "https://www.instagram.com/p/$l?__a=1"|jq -r '.graphql.shortcode_media|.edge_sidecar_to_children.edges[]?.node|.display_url'
    done
    

    To make a list of the shortcodes of each post made by the user whose profile is opened in the frontmost tab in Safari, I use a script like this:

    sjs(){ osascript -e'{on run{a}','tell app"safari"to do javascript a in document 1',end} -- "$1";}
    
    while :;do
      sjs 'o="";a=document.querySelectorAll(".v1Nh3 a");for(i=0;e=a[i];i++){o+=e.href+"\n"};o'>>/tmp/a
      sjs 'window.scrollBy(0,window.innerHeight)'
      sleep 1
    done
    

提交回复
热议问题