Skip to content

Instantly share code, notes, and snippets.

@from1to9
Created May 11, 2012 11:14
Show Gist options
  • Save from1to9/2659030 to your computer and use it in GitHub Desktop.
Save from1to9/2659030 to your computer and use it in GitHub Desktop.
auto download photos from fanfou 5th birthday page
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code by from1to9
#05/11/2012
#from1to9[at]gmail.com
#
#you have to make a folder by yourself named "photo" where photos will be stored in the same folder of this script
#a file named "last.txt" will be made in this folder, it keeps the start photo id, delete it and the script will download all photos again
import codecs
import json, random
import urllib, urllib2
def put_last_id(id):
try:
f = file("last.txt","w")
f.write(str(id))
f.close
except:
pass
def get_last_id():
try:
f = file("last.txt","r")
rev = f.readline()
f.close
except:
rev = 0
return rev
def getURL(url):
try:
req = urllib2.Request(url = url)
response = urllib2.urlopen(req)
context = response.read()
rev = json.loads(context.decode('utf-8'))
except:
rev = json.loads("{}")
return rev
def savePic(url, id):
try:
req = urllib2.Request(url)
response = urllib2.urlopen(req)
context = response.read()
f = file("photo\%s_%d.jpg" % ( id , random.randrange(0, 999) ),"wb")
f.write(context)
f.close()
except:
pass
def do_list(list, last_id):
for photo in list:
pid = photo["id"]
if pid > last_id:
savePic(photo["largeurl"], photo["user_id"])
try:
print "finish @%s" % photo["user_screen_name"]
except:
print "finish a new photo"
return pid
last_id = int(get_last_id())
list = getURL( "http://5.fanfou.com/exp/ajax/photos")
if len(list) > 0:
first_id = list["photos"][0]["id"]
put_last_id(first_id)
save_id = do_list(list["photos"], last_id)
while list["has_more"] and save_id > last_id:
if len(list) > 0:
list = getURL( "http://5.fanfou.com/exp/ajax/photos?max_id=" + str(save_id))
save_id = do_list(list["photos"], last_id)
print "all done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment