Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leotop/a1e8e75a9fe2523b80e6 to your computer and use it in GitHub Desktop.
Save leotop/a1e8e75a9fe2523b80e6 to your computer and use it in GitHub Desktop.
Grab instagram user's photos through client API
# -*- coding: utf-8 -*-
#
# Copyright(c) 2015 http://feilong.me
#
# @author: Felinx Lee <felinx.lee@gmail.com>
#
from tornado.httpclient import HTTPClient, HTTPRequest
from tornado import escape
import hashlib
cookie = "csrftoken=..." # Your current cookie
cookie = escape.squeeze(cookie)
agent = "Instagram 6.14.0 (iPhone7,1; iPhone OS 8_3; zh_CN;zh-Hans) AppleWebKit/420+"
url = "https://i.instagram.com/api/v1/feed/user/USER_ID" # USER_ID should be replaced by real ID like 12345678
def main():
client = HTTPClient()
request = HTTPRequest(url, headers={"Cookie": cookie, "User-Agent": agent})
response = client.fetch(request)
resp = escape.json_decode(response.body)
items = resp.get("items", [])
for item in items:
imgurl = item["image_versions2"]["candidates"][0]["url"]
urlmd5 = hashlib.md5(imgurl).hexdigest()
print "wget '%s' -O %s.jpg" % (imgurl, urlmd5) # or run this command in Python or request img file again
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment