Skip to content

Instantly share code, notes, and snippets.

@zs1621
Created November 26, 2012 09:03
Show Gist options
  • Save zs1621/4147293 to your computer and use it in GitHub Desktop.
Save zs1621/4147293 to your computer and use it in GitHub Desktop.
python save picture(papa)
#!/usr/bin/env python
#-*-coding="utf-8"-*-
#import MySQLdb
import httplib
import urllib
import simplejson
import time
import os
def papa(url):
i,j,x = 1,249,51
for n in range(1,1000000):
pa = httplib.HTTPConnection(url)
pa.request('GET','/api/v1/timeline/recommend?count=50&cursor='+str(j))
result = pa.getresponse()
# print result
if not result:
print 'fuck'
content = simplejson.loads(result.read())
response = content['response']
#print response[0]['post']
lista = []
listb = []
listc = []
dicta = {}
startime = time.clock()
if j == 249:
last = response[len(response)-1]['post']['id']
startime = time.clock()
for m in range(0,len(response)):
for a in response[m]['post']:
if a == "imageLarge":
lista.append(response[m]['post'][a])
if a == "author":
for b in response[m]['post'][a]:
if b == "id":
listc.append(response[m]['post'][a][b])
if b == "avatarLarge":
listb.append(response[m]['post'][a][b])
dicta[lista[m]] = listb[m]
m = 1
for key,value in dicta.iteritems():
data_image = urllib.urlopen(key).read()
data_avatar = urllib.urlopen(value).read()
name = listc[m-1]
path = os.getcwd()
if not (os.path.exists(path + '\download')):
os.mkdir('download')
path_image = path + '\download\image' +'_' +str(key[29:-8]) + '_' + name +'_'+str(m)+ '.jpg'
path_avatar = path + '\download\javatar' + '_'+str(key[29:-8]) + '_'+ name + '_'+ str(m) + '.jpg'
f1 = file(path_image,"wb")
f2 = file(path_avatar,"wb")
f1.write(data_image)
f2.write(data_avatar)
m = m + 1
endtime = time.clock()
# con.commit()
print endtime-startime
else:
if last != response[len(response)-1]['post']['id']:
#todo upsert into the first
key = response[0]['post']["imageLarge"]
value = response[0]['post']['author']['avatarLarge']
data_image = urllib.urlopen(key).read()
data_avatar = urllib.urlopen(value).read()
uid = response[0]['post']['author']['id']
path = os.getcwd()
if not (os.path.exists(path + '\download')):
os.mkdir('download')
path_image = path + '\download\image' + '_'+ str(key[29:-8]) + '_'+ uid +'_' + str(x) + '.jpg'
path_avatar = path + '\download\javatar' +'_'+ str(key[29:-8]) +'_' + uid + '_' +str(x) + '.jpg'
f1 = file(path_image,"wb")
f2 = file(path_avatar,"wb")
f1.write(data_image)
f2.write(data_avatar)
x = x + 1
last = response[len(response)-1]['post']['id']
print j,'dd'
time.sleep(1)
j = j+1
if __name__ == "__main__":
url = "api.papa.me"
papa(url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment