-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
71 lines (59 loc) · 2.11 KB
/
main.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
69
70
71
import requests
from urllib import request
import sys
import os
folder = 'F:\\vk_bot\\main\\photo\\'
group_name = input('input group name:\t')
def download_photo(photo_url, photo_id):
""" This function download photo """
path_to_download = folder + group_name + '\\'
try:
request.urlretrieve(photo_url, path_to_download + str(photo_id) + '.jpg')
except:
return 'download error: {}'.format(sys.exc_info())
return 'done'
def get_photo_url(items):
""" Here taking all urls of photo most big resolution """
""" and call function 'download_photo', which to giving url and id, for download """
links = []
for item in items:
try:
for att in item['attachments']:
for photo in att['photo']['sizes']:
if photo['type'] == 'z':
photo_id = att['photo']['id']
photo_url = photo['url']
links.append(photo_url)
print(download_photo(photo_url, photo_id))
except:
print('error')
pass
return links
def get_post(domain, count=100):
""" This function get response from the wall group that you give in the function """
url = 'https://api.vk.com/method/wall.get'
access_token = ''
version = '5.84'
offset = 0
items = []
group = domain
if not(os.path.exists(folder + domain)):
os.mkdir(folder + domain)
while offset < 100:
response = requests.get(
url,
params={
'access_token': access_token,
'v': version,
'domain': domain,
'count': count,
'offset': offset,
}
)
items.extend(response.json()['response']['items'])
offset += 100
# Here happens call to function 'get_photo_url', in that to giving array with items
url = get_photo_url(items)
return url, print('downloading complete. All file: {}'.format(len(url)))
response = get_post(group_name)
input('exit')