-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVKDownloadImage.py
executable file
·42 lines (37 loc) · 1.27 KB
/
VKDownloadImage.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
import json
import sys
import urllib
def downloadImage(images, imgDir, imgInfoFile, isBig = True):
''' Download images from vk.com
return images which not found in vk servers
'''
size = -1 if isBig else 0
imgInfo = {
'img': {
}
}
error404List = []
for index, image in enumerate(images):
try:
u = urllib.request.urlopen(image['sizes'][size]['src'])
raw_data = u.read()
u.close()
f = open(imgDir + str(index) + '.jpg','wb+')
f.write(raw_data)
f.close()
imgInfo['img'][index] = {
'id': image['id'],
'owner_id': image['owner_id'],
'album_id': image['album_id'],
'text': image['text']
}
except urllib.error.HTTPError as err:
print('https://vk.com/photo-2481783_' + str(image['id']))
error404List.append(str(image['id']))
except Exception as err:
print('https://vk.com/photo-2481783_' + str(image['id']))
sys.exit(err)
outfile = open(imgInfoFile, 'w')
json.dump(imgInfo, outfile, ensure_ascii=False, sort_keys=True, indent=4, separators=(',', ': '))
outfile.close()
return error404List