Skip to content

Instantly share code, notes, and snippets.

@acgotaku
Last active June 10, 2016 07:21
Show Gist options
  • Save acgotaku/5f5d5bea3868449bc027 to your computer and use it in GitHub Desktop.
Save acgotaku/5f5d5bea3868449bc027 to your computer and use it in GitHub Desktop.
get instagram image 使用方法 : ./instagram http://instagram.com/mochio_munchkin
#!/usr/bin/env python2
# -*- encoding: utf-8 -*-
# vim: set et sw=4 ts=4 sts=4 ff=unix fenc=utf8:
import os.path
import sys
import json
import codecs
import httplib
from pyquery import PyQuery as pq
from urlparse import urlparse
def getXML(url):
conn = httplib.HTTPConnection(url.netloc)
if url.query:
conn.request("GET",url.path+"?"+url.query)
else:
conn.request("GET",url.path)
response=conn.getresponse()
xml=response.read()
conn.close()
return xml
def saveXML(xml,file_name):
data=xml.decode(encoding='utf8',errors='replace')
f=open(file_name,"w")
f.write(data)
f.close()
def readMore(max_id,file_name):
url="http://instagram.com/"+file_name+"/media?max_id="+ max_id
xml = getXML(urlparse(url))
data=xml.decode(encoding='utf8',errors='replace')
obj=json.loads(data)
media=obj["items"]
downloadImg(media,file_name)
if obj["more_available"] == True:
print (media[-1]["id"])
readMore(media[-1]["id"],file_name)
else:
print (obj["more_available"])
return
def read(file_name,xml):
data=xml.decode(encoding='utf8',errors='replace')
obj=json.loads(data)
media=obj["items"]
downloadImg(media,file_name)
if obj["more_available"] == True:
readMore(media[-1]["id"],file_name)
def downloadImg(media,file_name):
file_low=open(file_name+"low","a")
file_mid=open(file_name+"mid","a")
file_high=open(file_name+"high","a")
for item in media:
file_low.write(item["images"]["thumbnail"]["url"]+"\n")
file_mid.write(item["images"]["low_resolution"]["url"]+"\n")
file_high.write(item["images"]["standard_resolution"]["url"]+"\n")
file_low.close()
file_mid.close()
file_high.close()
def main(argv):
args = argv[1]+"/media"
url = urlparse(args)
file_name=url.path[1:-6]
print (url)
xml = getXML(url)
read(file_name,xml)
if __name__ == "__main__":
main(sys.argv)
@uncountablecat
Copy link

I keep getting the following error
socket.error: [Errno 61] Connection refused
Is it due to possible change on the Instagram server side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment