forked from emn0/vkdumper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
277 lines (204 loc) · 10.7 KB
/
Copy pathmain.py
File metadata and controls
277 lines (204 loc) · 10.7 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
import json
import os
import time
from colorama import init
from shutil import copy
from vk_api import VkApi
from vk_api.exceptions import AuthError, VkApiError
TZ = 3600 * 3
USER_AGENT = 'KateMobileAndroid/52.4 (Android 8.1; SDK 27; armeabi-v7a; Xiaomi Redmi 5A; ru)'
config = {}
class Colors:
INFO = '\033[34;1m'
OK = '\033[32;1m'
WARNING = '\033[33;1m'
ERROR = '\033[31;1m'
RESET = '\033[0m'
def con(text):
print(text + Colors.RESET)
def captcha_handler(captcha):
r = input('Введите капчу ' + captcha.get_url() + ': ').strip()
return captcha.try_again(r)
def main():
init()
global config
try:
with open('config.json') as f:
config = json.load(f)
except FileNotFoundError:
config = {'limit': 1000, 'types': {'chats': True, 'groups': True, 'users': True}}
with open('config.json', 'w') as f:
json.dump(config, f, indent=2)
if config['limit'] <= 0:
config['limit'] = 1e10
s = input('Введите "логин:пароль" или токен: ')
if len(s) >= 85:
vk = VkApi(token=s, captcha_handler=captcha_handler)
vk.http.headers.update({
'User-agent': USER_AGENT
})
elif ':' in s:
sp = s.split(':')
vk = VkApi(sp[0], sp[1], app_id=2685278, captcha_handler=captcha_handler)
vk.http.headers.update({
'User-agent': USER_AGENT
})
try:
vk.auth(token_only=True)
except AuthError:
con(Colors.ERROR + 'Неверный логин или пароль')
return
else:
con(Colors.WARNING + 'Введите данные для входа в виде "логин:пароль" или "токен"')
return
try:
user = vk.method('users.get')[0]
except VkApiError as ex:
if ex.__dict__['error']['error_code'] == 5:
error_text = 'неверный токен'
else:
error_text = str(ex)
con(Colors.ERROR + 'Ошибка входа: ' + error_text)
return
con(Colors.OK + 'Вход выполнен')
infos = []
count = vk.method('messages.getConversations', {'count': 0})['count']
for offset in range((count - 1) // 200 + 1):
peers = vk.method('messages.getConversations', {'count': 200, 'extended': 1, 'offset': offset * 200})
for peer in peers['items']:
peer_id = peer['conversation']['peer']['id']
peer_type = peer['conversation']['peer']['type']
if peer_type == 'group' and config['types']['groups']:
info = [i for i in peers['groups'] if i['id'] == -peer_id][0]
name = info['screen_name'] if 'screen_name' in info else 'club' + str(info['id'])
infos.append((peer_id, name, info['name']))
elif peer_type == 'user' and config['types']['users']:
info = [i for i in peers['profiles'] if i['id'] == peer_id][0]
name = info['screen_name'] if 'screen_name' in info else 'id' + str(info['id'])
infos.append((peer_id, name, info['first_name'] + ' ' + info['last_name']))
elif peer_type == 'chat' and config['types']['chats']:
infos.append((peer_id, '', peer['conversation']['chat_settings']['title']))
base_dir = 'messages_' + str(user['id']) + '/'
if not os.path.exists(base_dir):
os.makedirs(base_dir)
copy('files/favicon.ico', base_dir)
copy('files/style.css', base_dir)
name = user['first_name'] + ' ' + user['last_name']
with open('files/index_pre.html', encoding='utf-8') as f:
index_file = f.read().replace('\n', '').format(user['id'], name)
for i in infos:
if i[1]:
index_file += '<div class="item"><div class="item__main"><a href="messages/{0}.html">{1}</a></div><div class="item__tertiary">@{2}</div></div>'.format(i[0], i[2], i[1])
else:
index_file += '<div class="item"><a href="messages/{0}.html">{1}</a></div>'.format(i[0], i[2])
with open('files/index_post.html', encoding='utf-8') as f:
index_file += f.read().replace('\n', '')
with open(base_dir + 'index.html', 'w', encoding='utf-8') as f:
f.write(index_file)
messages_dir = base_dir + 'messages/'
if not os.path.exists(messages_dir):
os.makedirs(messages_dir)
total = ' \\ ' + str(len(infos))
for num in range(len(infos)):
info = infos[num]
msgs = []
msg_count = vk.method('messages.getHistory', {'peer_id': info[0], 'count': 0})['count']
con(Colors.INFO + 'Сохранение диалога ' + str(num + 1) + total)
for offset in range(min(((msg_count - 1) // 200 + 1), config['limit'] // 200)):
try:
chunk = vk.method('messages.getHistory', {'peer_id': info[0], 'count': 200, 'extended': 1, 'offset': offset * 200})
for msg in chunk['items']:
if msg['from_id'] < 0:
item = [i for i in chunk['groups'] if i['id'] == -msg['from_id']][0]
sender = 'club' + str(-msg['from_id'])
sender_name = item['name']
else:
item = [i for i in chunk['profiles'] if i['id'] == msg['from_id']][0]
sender = 'id' + str(msg['from_id'])
sender_name = item['first_name'] + ' ' + item['last_name']
text = 'Служебное сообщение ' + msg['action']['type'] if 'action' in msg else msg['text']
a = []
for i in msg['attachments']:
link = ''
if i['type'] == 'photo':
photo = ''
current = 0
sizes = i['photo']['sizes']
for size in sizes:
if size['type'] == 'w':
photo = size['url']
break
elif size['type'] == 's' and current < 1:
current = 1
photo = size['url']
elif size['type'] == 'm' and current < 2:
current = 2
photo = size['url']
elif size['type'] == 'x' and current < 3:
current = 3
photo = size['url']
elif size['type'] == 'y' and current < 4:
current = 4
photo = size['url']
elif size['type'] == 'z' and current < 5:
current = 5
photo = size['url']
desc = 'Фотография'
link = photo
elif i['type'] == 'video':
desc = 'Видеозапись'
link = 'https://vk.com/video' + str(i['video']['owner_id']) + '_' + str(i['video']['id'])
elif i['type'] == 'audio':
desc = 'Аудиозапись ' + i['audio']['title']
elif i['type'] == 'doc':
desc = 'Документ'
link = i['doc']['url']
elif i['type'] == 'link':
desc = 'Ссылка'
link = i['link']['url']
elif i['type'] == 'market':
desc = 'Товар'
link = 'https://vk.com/market' + str(i['market']['owner_id']) + '_' + str(i['market']['id'])
elif i['type'] == 'wall':
desc = 'Запись на стене'
link = 'https://vk.com/wall' + str(i['wall']['to_id']) + '_' + str(i['wall']['id'])
elif i['type'] == 'wall_reply':
if 'deleted' in i['wall_reply']:
desc = 'Комментарий на стене (удалён)'
else:
desc = 'Комментарий на стене'
link = 'https://vk.com/wall' + str(i['wall_reply']['owner_id']) + '_' + str(i['wall_reply']['post_id']) + '?reply=' + str(i['wall_reply']['id'])
elif i['type'] == 'sticker':
desc = 'Стикер ID ' + str(i['sticker']['sticker_id'])
elif i['type'] == 'gift':
desc = 'Подарок ID ' + str(i['gift']['id'])
elif i['type'] == 'audio_message':
desc = 'Аудиосообщение'
link = i['audio_message']['link_mp3']
elif i['type'] == 'poll':
desc = 'Опрос'
link = 'https://vk.com/poll' + str(i['poll']['owner_id']) + '_' + str(i['poll']['id'])
else:
desc = ''
attach = '<div class="attachment__description">' + desc + '</div>'
if link:
attach += '<a class="attachment__link" href="' + link + '" target="_blank">' + link + '</a>'
a.append(attach)
msgs.append((sender, msg['date'], sender_name, text, a))
except KeyError:
con(Colors.WARNING + 'Ошибка при сохранении диалога ' + str(info[0]))
with open('files/messages_pre.html', encoding='utf-8') as f:
file = f.read().replace('\n', '').format(user['id'], name, info[2])
for msg in msgs:
link = '<a href="https://vk.com/' + str(msg[0]) + '" target="_blank">' + msg[2] + '</a>, '
tm = time.strftime('%d.%m.%Y %H:%M:%S', time.gmtime(msg[1] + TZ))
attach = '<div class="kludges">' + '\n'.join(msg[4]) + '</div>' if msg[4] else ''
file += '<div class="item"><div class="item__main"><div class="message"><div class="message__header">{0}{2}</div><div>{1}</div></div></div></div>'.format(link + tm, msg[3], attach)
with open('files/index_post.html', encoding='utf-8') as f:
file += f.read().replace('\n', '')
with open(messages_dir + str(info[0]) + '.html', 'w', encoding='utf-8') as f:
f.write(file)
con(Colors.OK + 'Готово!')
if __name__ == "__main__":
main()