-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
179 lines (150 loc) · 4.96 KB
/
app.py
File metadata and controls
179 lines (150 loc) · 4.96 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import vkApi
import json
from time import sleep
import sys
import requests
import os
import asyncio
login = ''
password = ''
vk = vkApi.messages(login, password)
SLEEP_TIME = 0.3
ROOT_FOLDER = login + '_photos'
def setStatus(statusStr):
sys.stdout.write('\r{}'.format(statusStr))
sys.stdout.flush()
def getChats():
print('Getting list of dialogs')
def formUsers(ids):
users = []
js = vk.method('users.get', user_ids = ids)
js = json.loads(js['payload'][1][0])
for item in js['response']:
user = {
'name': item['first_name'] + ' ' + item['last_name'],
'id': item['id']
}
users.append(user)
return users
def formGroups(ids):
groups = []
js = vk.method('groups.getById', group_ids = ids)
js = json.loads(js['payload'][1][0])
if 'response' not in js:
print(js)
return []
for item in js['response']:
group = {
'name': item['name'],
'id': item['id']
}
groups.append(group)
return groups
def formChats(items):
chats = []
users = []
groups = []
for item in items:
chatType = item['conversation']['peer']['type']
chatId = item['conversation']['peer']['id']
if chatType == 'chat':
chat = {
'name': item['conversation']['chat_settings']['title'],
'id': chatId
}
chats.append(chat)
elif chatType == 'user':
users.append(chatId)
else:
groups.append(chatId)
if len(users) != 0:
users = [str(user) for user in users]
users = formUsers(','.join(users))
chats.extend(users)
sleep(SLEEP_TIME)
if len(groups) != 0:
groups = [str(-group) for group in groups]
groups = formGroups(','.join(groups))
chats.extend(groups)
sleep(SLEEP_TIME)
return chats
cnt = 200
js = vk.method('messages.getConversations', count = cnt, filter = 'all')
js = json.loads(js['payload'][1][0])
js = js['response']
chats = formChats(js['items'])
total = js['count']
ofst = cnt
setStatus('{} dialogs received'.format(len(chats)))
while(len(chats)<total):
js = vk.method('messages.getConversations', count = cnt, filter = 'all', offset = ofst)
js = json.loads(js['payload'][1][0])
js = js['response']
temp_chats = formChats(js['items'])
chats.extend(temp_chats)
ofst= ofst+cnt
setStatus('{} dialogs received'.format(len(chats)))
sleep(SLEEP_TIME)
print('\nDone!')
for chat in chats:
chat['name'] = chat['name'].replace('/',' ')
return chats
def getPhotosList(id):
ofst = 0
photos = []
cnt = 200
print('Getting list of photos')
while (True):
js = vk.method('messages.getHistoryAttachments',
peer_id = id,
media_type = 'photo',
count = cnt,
start_from = ofst
)
js = json.loads(js['payload'][1][0])
if 'response' not in js:
print(js)
js = js['response']
if( len(js['items']) == 0):
setStatus('{} photos received'.format(len(photos)))
break
temp_photos = [item['attachment']['photo']['sizes'][-1]['url'] for item in js['items']]
photos.extend(temp_photos)
setStatus('{} photos received'.format(len(photos)))
ofst = js['next_from']
sleep(SLEEP_TIME)
sleep(SLEEP_TIME)
print('\nDone!')
return photos
def downloadPhotos(lst, dir_):
folder = ROOT_FOLDER+'/'+dir_
i = 0
lst_len = len(lst)
if dir_ not in os.listdir(ROOT_FOLDER):
os.mkdir(os.path.normpath(folder))
print('Downloading photos')
setStatus('{} photos downloaded from {}'.format(i, lst_len))
for url in lst:
r = requests.get(url, allow_redirects=True)
filename = folder+'/'+url.split('/')[-1]
filename = os.path.normpath(filename)
with open(filename, 'wb') as file:
file.write(r.content)
i+=1
msg = '{} photos downloaded from {}'.format(i, lst_len)
setStatus(msg)
print('\nDone!')
if ROOT_FOLDER not in os.listdir():
os.mkdir(ROOT_FOLDER)
ignore_list = []
with open('ignore_list', 'r') as f:
ignore_list = [int(line.split('\n')[0]) for line in f]
chats = getChats()
chats = list(filter(lambda item: item['id'] not in ignore_list, chats))
lst_len = len(chats)
for num, chat in enumerate(chats):
print('-----------------------------------------')
print('Working with {} ({} of {})\nDialog id:{}'.format(chat['name'], num+1, lst_len,chat['id']))
photos = getPhotosList(chat['id'])
if len(photos) != 0:
downloadPhotos(photos, chat['name'])