-
Notifications
You must be signed in to change notification settings - Fork 1
/
vk_photo_downloader.py
executable file
·52 lines (46 loc) · 1.44 KB
/
vk_photo_downloader.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import vk_api
import urllib.request
import os.path
import sys, getopt
import time
def main():
vk_token = ''
owner_id = ''
argv = sys.argv[1:]
opts = []
try:
opts, args = getopt.getopt(argv, "t:o:")
except:
print("Error")
for opt, arg in opts:
if opt in ['-t']:
vk_token = arg
elif opt in ['-o']:
owner_id = arg
vk_session = vk_api.VkApi(token = vk_token)
vk = vk_session.get_api()
wall = vk.wall.get(owner_id = owner_id)
wall_items = wall['items']
image_dir = str(owner_id).lstrip('-')
isExist = os.path.exists(image_dir)
if not isExist:
os.makedirs(image_dir)
for item in wall_items:
if 'attachments' in item:
attachments = item['attachments'][0]
if 'photo' in attachments:
photo = attachments['photo']['sizes']
for size in photo:
if size['type'] == 'y':
url = size['url']
filename = image_dir + "/" + str(attachments['photo']['id']) + ".jpg"
if os.path.isfile(filename):
print("File exists with name " + filename)
else:
urllib.request.urlretrieve(url, filename)
time.sleep(1)
if __name__ == '__main__':
main()