-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcore.py
337 lines (258 loc) · 8.16 KB
/
core.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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/python
"""
Driven (Tuchka) program core
yepIwt, 2022
"""
import os, sys, requests
import zipfile
import vk_api
import confs
import shutil
from loguru import logger
import random, string
VK_MESSAGE_CONSTANT = 2000000000
def get_vk_api(login: str = None, passw: str = None, token: str = None) -> tuple:
vk_session = vk_api.VkApi(
login = login,
password = passw,
token = token
)
api = vk_session.get_api()
try:
api.wall.get()
except Exception as error:
return False, error
else:
return True, api
def make_zip_dir(path):
zf = zipfile.ZipFile("decrypted.zip", "w", zipfile.ZIP_DEFLATED)
abs_src = os.path.abspath(path)
for dirname, subdirs, files in os.walk(path):
for filename in files:
absname = os.path.abspath(os.path.join(dirname, filename))
arcname = absname[len(abs_src) + 1:]
zf.write(absname, arcname)
zf.close()
return 'decrypted.zip'
def make_unzip_file(path_to_archive, folder_path):
with zipfile.ZipFile(path_to_archive, 'r') as file:
file.extractall(folder_path)
class DrivenCore:
cfg = None
_vk_api = None
_current_chat_selected = None # peer_id
def __init__(self, config: confs.Config):
"""
Recommendation: use only working vk api token or add a handler before
"""
self.cfg = config
try:
_, self._vk_api = get_vk_api(token=self.cfg.data['vk_api_token'])
except:
sys.exit()
logger.success('VK API granted!')
def _create_new_chat(self, title):
self._vk_api.messages.createChat(title = title)
def _get_all_chats(self, with_pictures = False) -> list:
"""
Returns list with: peer_id, chat_title, url_to_chat_pic
"""
chats = []
logger.debug("Start 'get_all_chats' function")
answer = self._vk_api.messages.getConversations(count=200, extended=1)
offset = 0
while answer['items']:
for i in answer['items']:
if i['conversation']['peer']['type'] not in ['user', 'group']:
chat_pic = None
if with_pictures:
chat_pic = self._get_chat_picture_url_by_peer_id(i['conversation']['peer']['id'])
chats.append(
(
i['conversation']['peer']['id'],
i['conversation']['chat_settings']['title'],
chat_pic
)
)
offset += len(answer['items'])
answer = self._vk_api.messages.getConversations(count=200, extended=1, offset=offset)
logger.success(f"End 'get_all_chats' function with len(result) = {len(chats)}")
return chats
def _get_chat_title_by_peer_id(self, peer_id: int) -> str:
logger.debug(f"Start 'get_chat_title_by_peer_id' function with peer_id = {peer_id}")
answer = self._vk_api.messages.getChat(
chat_id=peer_id - VK_MESSAGE_CONSTANT,
)
chat_title = answer['title']
logger.success("End 'get_all_chats' function")
return chat_title
def _get_chat_picture_url_by_peer_id(self, peer_id: int) -> str:
logger.debug(f"Start 'get_chat_picture_url_by_peer_id' function with peer_id = {peer_id}")
answer = self._vk_api.messages.getChat(
chat_id=peer_id - VK_MESSAGE_CONSTANT,
)
url_to_picture = answer.get("photo_200")
logger.success("End 'get_chat_picture_url_by_peer_id' function")
return url_to_picture
def _search_chat_by_title(self, text: str) -> list:
"""
Returns list with: peer_id, chat_title, url_to_chat_pic
"""
chats = []
logger.debug(f"Start 'search_chat_by_title' function with q = {text}")
answer = self._vk_api.messages.search(
q=text,
count=100,
)
offset = 0
while answer['items']:
for i in answer['items']:
if i['peer_id'] > VK_MESSAGE_CONSTANT: # This is a chat
flag = False
for pid, _, _ in chats:
if pid == i['peer_id']:
flag = True
if not flag:
chats.append(
(
i['peer_id'],
self._get_chat_title_by_peer_id(i['peer_id']),
self._get_chat_picture_url_by_peer_id(i['peer_id'])
)
)
offset += len(answer['items'])
answer = self._vk_api.messages.search(q=text, count=100, offset=offset)
logger.success(f"End 'search_chat_by_title' function with len(result) = {len(chats)}")
return chats
def _get_f_l_by_user_id(self, uid) -> str:
"""
Return string with: First_Name + Last_Name
Warning: Group error expected.
"""
answ = self._vk_api.users.get(user_ids = uid)[0]
return answ['first_name'] + " " + answ['last_name']
def _get_history_attachments_by_peer_id(self, peer_id: str) -> list:
"""
Returns list with tuple: from_id, unix_date, url_to_file, commit_message
Warning: Duplicate files expected.
"""
files = []
logger.debug(f"Start 'get_history_attachments_by_peer_id' with peer_id = {peer_id}")
answer = self._vk_api.messages.getHistoryAttachments(
peer_id=peer_id,
media_type="doc",
count=200
)
while answer['items']:
for it in answer['items']:
message_id = it['message_id']
answ = self._vk_api.messages.getById(message_ids = message_id)
from_id = it['from_id']
date_unix = it['attachment']['doc']['date']
url_to_file = it['attachment']['doc']['url'],
commit_message = answ['items'][0]['text']
files.append(
(
from_id,
date_unix,
url_to_file,
commit_message
)
)
answer = self._vk_api.messages.getHistoryAttachments(
peer_id=peer_id,
media_type="doc",
count=200,
start_from=answer['next_from']
)
new_files, new_order = self.add_order_to_files(files, self.cfg.data['order'])
self.cfg.data['order'] = new_order
self.cfg.save()
return new_files
def add_order_to_files(self, files, order):
finished_order = [] # значит, что объект из очереди выгрузился в вк
for ordered in order:
for attachments in files:
if attachments[1] == ordered[1]:
finished_order.append(ordered)
new_order = list(set(order) - set(finished_order))
if new_order:
files = new_order + files
return files, new_order
def _upload_file(self, file_path, peer_id) -> dict:
if os.access(file_path, os.R_OK):
letters = string.ascii_lowercase + string.ascii_lowercase.capitalize()
file_title = ''.join(random.choice(letters) for i in range(10))
f = open(file_path, 'rb')
up = vk_api.VkUpload(self._vk_api)
file_data = up.document_message(
title = file_title,
doc = f,
peer_id = peer_id
)
return file_data
return {}
def _send_file_to_chat_id(self, file_data, commit_message, chat_id):
owner_id = file_data['doc']['owner_id']
file_id = file_data['doc']['id']
self._vk_api.messages.send(
peer_id = chat_id,
message = commit_message,
attachment = f"doc{owner_id}_{file_id}",
random_id = vk_api.utils.get_random_id(),
)
def change_release(self, url_to_file, folder):
r = requests.get(url_to_file)
with open("encrypted", 'wb') as f:
f.write(r.content)
self.cfg.decrypt("encrypted")
if not os.access(folder, os.R_OK):
os.mkdir(folder)
shutil.rmtree(folder)
make_unzip_file("decrypted.zip", folder)
os.remove("decrypted.zip")
os.remove("encrypted")
def synchronization(self, chat_id, commit_message): # Отправка на сервер
for i in range(len(self.cfg.data['archives'])):
if self.cfg.data['archives'][i]['id'] == chat_id:
n = i
break
folder = self.cfg.data['archives'][n]['folder']
if not os.access(folder, os.R_OK):
os.mkdir(folder)
path_to_archive = make_zip_dir(folder)
self.cfg.encrypt(path_to_archive)
file_data = self._upload_file("encrypted", chat_id)
self._send_file_to_chat_id(file_data, commit_message, chat_id)
os.remove("decrypted.zip")
os.remove("encrypted")
self.cfg.data['archives'][n]['current'] = file_data['doc']['date']
self.cfg.save()
files = self._get_history_attachments_by_peer_id(peer_id = chat_id)
answ = (
files,
file_data['doc']['date'],
file_data['doc']['owner_id'],
(file_data['doc']['url'],0),
commit_message
)
return answ
if __name__ == "__main__":
c = confs.Config()
if c.config_here:
unlocked = False
while not unlocked:
password = input("Enter password: ")
unlocked = c.open(password)
print("Done!")
else:
status = False
api_token = ""
while not status:
api_token = input("Enter VK API token: ")
status, err = get_vk_api(token=api_token)
if not status:
print(err)
new_password = input("Enter new password: ")
c.new_cfg(api_token, new_password)
print("Done!")