-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
643 lines (588 loc) · 27.3 KB
/
Copy pathmain.py
File metadata and controls
643 lines (588 loc) · 27.3 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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
# -*- coding: utf-8 -*-
from PyQt5 import QtWidgets, QtCore
from forms.main_form import Ui_MainWindow
from forms.auth_form import Ui_Auth_form
from forms.info_form import Ui_info_form
from PyQt5.QtWidgets import QTableWidgetItem, QWidget, QFileDialog, QMessageBox
import sys
import requests
import collections
import os
import datetime
from vkauth import VKAuth
import pyperclip
v = 5.95
# Окно авторизации
class AuthWindow(QWidget, Ui_Auth_form):
def __init__(self):
QWidget.__init__(self)
self.setupUi(self)
self.login_line.setText(str(''))
self.password_line.setText(str(''))
self.pushButton.clicked.connect(self.auth_btn)
def auth_btn(self):
vk = VKAuth(['1073737727'], '7021586', '5.95', email=self.login_line.text(), pswd=self.password_line.text())
vk.auth()
self.user_token = vk.get_token()
if vk.get_token() is None:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText('Невозможно авторизоваться:')
msg.setInformativeText('Неверно указан логин/пароль')
msg.setWindowTitle('Ошибка при авторизации')
msg.exec_()
else:
print(self.user_token)
self.window = MainWindow(self.user_token)
application.hide()
self.window.show()
# Окно программы
class InfoWindow(QWidget):
def __init__(self):
super(InfoWindow, self).__init__()
self.ui = Ui_info_form()
self.ui.setupUi(self)
# Основной код
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, user_token):
super(MainWindow, self).__init__()
print(user_token)
self.likes_commenst_list = []
self.users_allinfo_list = []
self.user_token = user_token
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.searchCommunityButton.clicked.connect(self.search_groups)
self.ui.searchUsersButton.clicked.connect(self.activity)
self.ui.saveIDsButton.clicked.connect(self.save_ids)
self.ui.loadIDsButton.clicked.connect(self.load_ids)
self.ui.filterButton.clicked.connect(self.filter)
self.ui.copyBufferButton.clicked.connect(self.buffer)
self.ui.action_2.triggered.connect(self.info)
self.ui.communityCountEdit.setText(str('1'))
self.ui.publicationsCountEdit.setText(str('50'))
self.ui.activityLikesEdit.setText(str('4'))
self.ui.activityCommentsEdit.setText((str('2')))
def info(self):
self.window = InfoWindow()
self.window.show()
# Получение списка с группами
def search_groups(self):
groups_all_fields = []
words_list = self.ui.searchCommunityEdit.toPlainText().splitlines()
screen_name_list = []
count = self.ui.communityCountEdit.text()
sort = 1
if self.ui.searchCommunityEdit.toPlainText() == '':
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Ошибка")
msg.setInformativeText('Не указаны фразы для поиска.')
msg.setWindowTitle("Поиск сообщест невозможен!")
msg.exec_()
else:
if count == '' or int(count) <= 0:
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Ошибка")
msg.setInformativeText('Неверное значение для количества сообществ на запрос')
msg.setWindowTitle("Неверное значение")
msg.exec_()
else:
count = int(self.ui.communityCountEdit.text())
if self.ui.typeSortComboBox.currentText() == 'Cортировать по скорости роста':
sort = 1
if self.ui.typeSortComboBox.currentText() == 'Дневная посещаемость к количеству пользователей':
sort = 2
if self.ui.typeSortComboBox.currentText() == 'Количество лайков к количеству пользователей':
sort = 3
if self.ui.typeSortComboBox.currentText() == \
'Отношение количества комментариев к количеству пользователей':
sort = 4
if self.ui.typeSortComboBox.currentText() == \
'Отношение количества записей в обсуждениях к количеству пользователей':
sort = 5
for search_word in words_list:
response = requests.get('https://api.vk.com/method/groups.search',
params={
'access_token': self.user_token,
'v': v,
'q': search_word,
'count': count,
'sort': sort
}
)
data = response.json()['response']['items']
groups_all_fields.extend(data)
for screen_name in groups_all_fields:
screen_name_list.append(screen_name['screen_name'])
for list_item in screen_name_list:
self.ui.communityListEdit.append(list_item)
if self.ui.communityListEdit.toPlainText() == '':
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle('Сообщества не найдены')
msg.setText('Сообщества не найдены. Попробуйте другой запрос.')
msg.exec_()
self.ui.label_7.setText('Всего сообществ: ' + str(len(screen_name_list)))
# Получение списка постов
def take_posts(self):
all_posts = []
groups = self.ui.communityListEdit.toPlainText().splitlines()
domain_names = [group.replace('https://vk.com/', '').replace(' ', '') for group in groups]
amount = int(self.ui.publicationsCountEdit.text())
offset = 1
if amount > 100:
count = amount % 100
else:
count = amount
while count <= amount:
for domain_name in domain_names:
try:
response = requests.get('https://api.vk.com/method/wall.get',
params={
'access_token': self.user_token,
'v': v,
'domain': domain_name,
'count': count,
'offset': offset
})
data = response.json()['response']['items']
except:
pass
offset += 100
count += 100
all_posts.extend(data)
return all_posts
# Получение списка с ID паблика и поста
def export_id(self, all_posts):
posts = [(post['owner_id'], post['id']) for post in all_posts]
return posts
# Получение списка ID пользователей лайк/репост
def likes(self, owner_id, item_id):
type = 'post'
likes_id = []
response = requests.get('https://api.vk.com/method/likes.getList',
params={
'access_token': self.user_token,
'v': v,
'type': type,
'owner_id': owner_id,
'item_id': item_id
}
)
data = response.json()['response']['items']
likes_id.extend(data)
return likes_id
# Получение ID комментариев
def comments(self, owner_id, post_id):
comments_list = []
count = 100
offset = 0
while offset < 1000:
response = requests.get('https://api.vk.com/method/wall.getComments',
params={
'access_token': self.user_token,
'v': v,
'owner_id': owner_id,
'post_id': post_id,
'count': count,
'offset': offset,
}
)
data = response.json()['response']['items']
offset += 100
comments_list.extend(data)
return comments_list
# Получение активной аудитории
def activity(self):
activity_likes = int(self.ui.activityLikesEdit.text())
activity_comments = int(self.ui.activityCommentsEdit.text())
check = 0
likes_comments_list = []
self.ui.tableWidget.clear()
if self.ui.communityListEdit.toPlainText() == '':
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Невозможно получить список пользователей")
msg.setInformativeText('Не указано ни одно сообщество' + '\n' 'для поиска')
msg.setWindowTitle("Ошибка")
msg.exec_()
else:
if self.ui.likesCheckBox.isChecked():
check = 1
elif self.ui.commentsCheckBox.isChecked():
check = 2
elif self.ui.likesCheckBox.isChecked() & self.ui.commentsCheckBox.isChecked():
check = 3
else:
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle('Некорректные критерии активности')
msg.setText('Вы не указали критерии активности!')
msg.exec_()
# Если выбраны лайки/репосты
if check == 1:
likes_id = [self.likes(x, y) for x, y in self.export_id(self.take_posts())]
all_likes = [y for x in likes_id for y in x]
l = [list_item for list_item in ([item for item, count in collections.Counter(all_likes).items()
if count > activity_likes])]
likes_comments_list = l
# Если выбраны комментарии
elif check == 2:
comments_id = []
comments_id_ = [(self.comments(a, b)) for a, b in self.export_id(self.take_posts())]
for x in comments_id_:
for comment in x:
try:
comments_id.append(comment['from_id'])
except:
pass
sorted_list = [sort for sort in comments_id if sort > 0]
c = [list_item for list_item in ([item for item, count in collections.Counter(sorted_list).items()
if count > activity_comments])]
likes_comments_list = c
# Если выбраны лайки/репосты и комментарии
elif check == 3:
likes_id = [self.likes(x, y) for x, y in self.export_id(self.take_posts())]
all_likes = [y for x in likes_id for y in x]
l = [list_item for list_item in ([item for item, count in collections.Counter(all_likes).items()
if count > activity_likes])]
comments_id = []
comments_id_ = [(self.comments(a, b)) for a, b in self.export_id(self.take_posts())]
for x in comments_id_:
for comment in x:
try:
comments_id.append(comment['from_id'])
except:
pass
sorted_list = [sort for sort in comments_id if sort > 0]
c = [list_item for list_item in ([item for item, count in collections.Counter(sorted_list).items()
if count > activity_comments])]
likes_comments_list = list(set(l + c))
else:
pass
# Получение данных о пользователях
fields = 'id, sex, education, universities, city, country, bdate'
lang = 'ru'
users_allinfo_list = []
dta = []
country_list_sort = []
country_list = []
city_list_sort = []
city_list = []
vuz_list_sort = []
vuz_list = []
for user_id in likes_comments_list:
try:
response = requests.get('https://api.vk.com/method/users.get',
params={
'lang': lang,
'access_token': self.user_token,
'v': v,
'user_ids': user_id,
'fields': fields
}
)
data = response.json()['response']
users_allinfo_list.extend(data)
except:
pass
self.users_allinfo_list = users_allinfo_list
test = [x for x in users_allinfo_list]
print(len(test))
# Создание заголовков колонок
self.ui.tableWidget.setRowCount(len(users_allinfo_list))
self.ui.tableWidget.setHorizontalHeaderLabels(('Имя', 'Фамилия', 'Пол', 'Возраст', 'Страна', 'Город',
'Место учебы'))
for info in users_allinfo_list:
try:
if info['sex'] == 2:
sex = "Мужской"
else:
sex = 'Женский'
try:
a = info['bdate']
a = a.split('.')
aa = datetime.date(int(a[2]), int(a[1]), int(a[0]))
bb = datetime.date.today()
cc = (bb - aa) // 365
date = str(cc).split()[0]
except:
date = 'скрыт'
try:
country = info['country']['title']
except:
country = 'скрыта'
try:
city = info['city']['title']
except:
city = 'скрыт'
try:
if info['university_name'] == '':
vuz = 'не указано'
else:
vuz = info['university_name']
except:
vuz = 'не указано'
dta.append((info['first_name'], info['last_name'], sex, date, country, city, vuz))
if country == 'скрыта':
pass
else:
country_list_sort.append(country)
country_list = list(set(country_list_sort))
if city == 'скрыт':
pass
else:
city_list_sort.append(city)
city_list = list(set(city_list_sort))
if vuz == 'не указано':
pass
else:
vuz_list_sort.append(vuz)
vuz_list = list(set(vuz_list_sort))
except:
pass
self.ui.countryComboBox.addItems(country_list)
self.ui.cityComboBox.addItems(city_list)
self.ui.educationComboBox.addItems(vuz_list)
# Вывод данных в таблицу
row = 0
for tup in dta:
col = 0
for item in tup:
cellinfo = QTableWidgetItem(item)
cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
self.ui.tableWidget.setItem(row, col, cellinfo)
col += 1
row += 1
self.ui.tableWidget.setSortingEnabled(True)
if row == 0:
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle('Пользователи не найдены')
msg.setText('Пользователи не найдены. Попробуйте другие сообщества.')
msg.exec_()
self.likes_commenst_list = likes_comments_list
# Сохранение в файл ID полученной аудитории
def save_ids(self):
filename = QFileDialog.getSaveFileName(self, '', os.getcwd(), "Text (*.txt)")[0]
with open(filename, "w", encoding='utf8') as file:
for x in self.likes_commenst_list:
file.write('%s\n' % x)
# Сохранение в буфер ID полученной аудитории
def buffer(self):
string = '\n'.join(str(x) for x in self.likes_commenst_list)
pyperclip.copy(string)
# Загрузка ID из файла
def load_ids(self):
users_allinfo_list = []
dta = []
users = ''
fields = 'id, sex, education, universities, city, country, bdate'
lang = 'ru'
filename = QFileDialog.getOpenFileName(self, '', os.getcwd(), "Text (*.txt)")[0]
try:
with open(filename, 'r') as f:
users = f.read().splitlines()
except:
pass
likes_comments_list = [user for user in users]
for user_id in likes_comments_list:
try:
response = requests.get('https://api.vk.com/method/users.get',
params={
'lang': lang,
'access_token': self.user_token,
'v': v,
'user_ids': user_id,
'fields': fields
}
)
data = response.json()['response']
users_allinfo_list.extend(data)
except:
pass
self.ui.tableWidget.setRowCount(len(users_allinfo_list))
self.ui.tableWidget.setHorizontalHeaderLabels(('Имя', 'Фамилия', 'Пол', 'Возраст', 'Страна', 'Город',
'Место учебы'))
for info in users_allinfo_list:
try:
if info['sex'] == 2:
sex = "Мужской"
else:
sex = 'Женский'
try:
a = info['bdate']
a = a.split('.')
aa = datetime.date(int(a[2]), int(a[1]), int(a[0]))
bb = datetime.date.today()
cc = (bb - aa) // 365
date = str(cc).split()[0]
except:
date = 'скрыт'
try:
country = info['country']['title']
except:
country = 'скрыта'
try:
city = info['city']['title']
except:
city = 'скрыт'
try:
if info['university_name'] == '':
vuz = 'не указано'
else:
vuz = info['university_name']
except:
vuz = 'не указано'
dta.append((info['first_name'], info['last_name'], sex, date, country, city, vuz))
except:
pass
row = 0
for tup in dta:
col = 0
for item in tup:
cellinfo = QTableWidgetItem(item)
cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
self.ui.tableWidget.setItem(row, col, cellinfo)
col += 1
row += 1
self.ui.tableWidget.setSortingEnabled(True)
self.likes_commenst_list = likes_comments_list
# Применение фильтра
def filter(self):
dta = []
filtered_users = []
filtered_users_info = []
fields = 'id, sex, education, universities, city, country, bdate'
lang = 'ru'
if self.ui.sexComboBox.currentText() == 'Мужской':
s = 2
else:
s = 1
for info in self.users_allinfo_list:
try:
id = info['id']
filtered_users.append(id)
if info['sex'] == 2:
sex = 'Мужской'
else:
sex = 'Женский'
try:
a = info['bdate']
a = a.split('.')
aa = datetime.date(int(a[2]), int(a[1]), int(a[0]))
bb = datetime.date.today()
cc = (bb - aa) // 365
date = str(cc).split()[0]
except:
date = 'скрыт'
try:
country = info['country']['title']
except:
country = 'скрыта'
try:
city = info['city']['title']
except:
city = 'скрыт'
try:
if info['university_name'] == '':
vuz = 'не указано'
else:
vuz = info['university_name']
except:
vuz = 'не указано'
if self.ui.ageFromEdit.text() != '':
if self.ui.ageFromEdit.text() > date or date == 'скрыт':
filtered_users.remove(id)
if self.ui.ageToEdit.text() != '':
if self.ui.ageToEdit.text() < date or date == 'скрыт':
filtered_users.remove(id)
if self.ui.sexComboBox.currentText() != 'Не менять':
if info['sex'] != s:
filtered_users.remove(id)
if self.ui.countryComboBox.currentText() != 'Не менять':
if country != self.ui.countryComboBox.currentText() or country == 'скрыта':
filtered_users.remove(id)
if self.ui.cityComboBox.currentText() != 'Не менять':
if city != self.ui.cityComboBox.currentText() or city == 'скрыт':
filtered_users.remove(id)
if self.ui.educationComboBox.currentText() != 'Не менять':
if vuz != self.ui.educationComboBox.currentText() or vuz == 'не указано':
filtered_users.remove(id)
except:
pass
print(filtered_users, len(filtered_users))
for user_id in filtered_users:
try:
response = requests.get('https://api.vk.com/method/users.get',
params={
'lang': lang,
'access_token': self.user_token,
'v': v,
'user_ids': user_id,
'fields': fields
}
)
data = response.json()['response']
filtered_users_info.extend(data)
except:
pass
self.ui.tableWidget.setRowCount(len(filtered_users_info))
self.ui.tableWidget.setHorizontalHeaderLabels(('Имя', 'Фамилия', 'Пол', 'Возраст', 'Страна', 'Город',
'Место учебы'))
for info in filtered_users_info:
try:
if info['sex'] == 2:
sex = "Мужской"
else:
sex = 'Женский'
try:
a = info['bdate']
a = a.split('.')
aa = datetime.date(int(a[2]), int(a[1]), int(a[0]))
bb = datetime.date.today()
cc = (bb - aa) // 365
date = str(cc).split()[0]
except:
date = 'скрыт'
try:
country = info['country']['title']
except:
country = 'скрыта'
try:
city = info['city']['title']
except:
city = 'скрыт'
try:
if info['university_name'] == '':
vuz = 'не указано'
else:
vuz = info['university_name']
except:
vuz = 'не указано'
dta.append((info['first_name'], info['last_name'], sex, date, country, city, vuz))
except:
pass
row = 0
for tup in dta:
col = 0
for item in tup:
cellinfo = QTableWidgetItem(item)
cellinfo.setFlags(QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled)
self.ui.tableWidget.setItem(row, col, cellinfo)
col += 1
row += 1
self.ui.tableWidget.setSortingEnabled(True)
if row == 0:
msg = QMessageBox()
msg.setIcon(QMessageBox.Information)
msg.setWindowTitle('Пользователи не найдены')
msg.setText('Пользователи не найдены. Попробуйте другие сообщества.')
msg.exec_()
self.likes_commenst_list = list(set(filtered_users))
app = QtWidgets.QApplication([])
application = AuthWindow()
application.show()
sys.exit(app.exec())