forked from tianjyan/py-qzone-photo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
untils.py
68 lines (58 loc) · 2.14 KB
/
untils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding:utf-8 -*-
__author__ = 'young'
import json
import urllib2
import os
import Entity
def getAblums(qq, url):
ablums = list()
print url + qq + "&outstyle=2"
request = urllib2.Request(url + qq + "&outstyle=2")
f = urllib2.urlopen(request, timeout=10)
response = f.read().decode('gbk')
f.close()
response = response.replace('_Callback(', '')
response = response.replace(');', '')
#print response
if 'album' in json.loads(response):
for i in json.loads(response)['album']:
ablums.append(Entity.Album(i['id'], i['name'], i['total']))
return ablums
def getPhotosByAlum(album, qq, url):
photos = list()
print url + qq + "&albumid=" + album.ID + "&outstyle=json"
request = urllib2.Request(url + qq + "&albumid=" + album.ID + "&outstyle=json")
f = urllib2.urlopen(request, timeout=10)
response = f.read().decode('gbk')
f.close()
response = response.replace('_Callback(', '')
response = response.replace(');', '')
#print response
if 'pic' in json.loads(response):
for i in json.loads(response)['pic']:
photos.append(Entity.Photo(i['url'], i['name'], album))
return photos
def saveImage(path, photo, qq, index):
print index, photo.URL
url = photo.URL.replace('\\', '')
f = urllib2.urlopen(url, timeout=10)
data = f.read()
f.close()
if not os.path.exists(path+os.path.sep+qq):
os.mkdir(path+os.path.sep+qq)
with open(path+os.path.sep+qq+os.path.sep + index + '.jpeg', "wb") as code:
code.write(data)
code.close()
def savePhotos(qq, path=Entity.savepath):
print u'获取:'+qq+u'的相册信息'
ablums = getAblums(qq, Entity.albumbase1)
if len(ablums) > 0:
for i, a in enumerate(ablums):
if a.Count > 0:
print u'开始下载第'+str(i+1)+u'个相册'
photos = getPhotosByAlum(a, qq, Entity.photobase1)
for index, p in enumerate(photos):
saveImage(path, p, qq, str(i)+'_'+str(index))
print u'第'+str(i+1)+u'个相册下载完成'
else:
print u'读取到得相册个数为0'