-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvk_bot.py
112 lines (89 loc) · 4.05 KB
/
vk_bot.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import datetime
import os
from auth_vk import *
APP_ID = '6478436'
V = 9.85
# LIST_TO_SEND = ['deleted', 'relation', 'status', 'deactivated']
INCLUDE = ['deleted', 'relation'] # Свойства пользователя, которые отслеживать
EXCLUDE = ['track_code', 'online', 'status_audio'] # Свойства пользователя, которые не отслеживать
USER_ID = 133147577
REPORT_TIME = datetime.datetime.now()
VERSION = '1.0.00 (08.01.2020)'
AUTHOR = 'Deviur (https://github.com/deviur)'
def main():
print("\n", REPORT_TIME)
print(os.getcwd())
# Авторизация в ВК
session = auth_vk_token()
# Подключение к API
api = vk.API(session, v=V)
# # Проверка, удалась ли авторизация
# try:
# user = api.users.get()[0]
# except vk.exceptions.VkAPIError:
# print('Авторизация не удалась. Повторите попытку.\n')
# login_by_password()
# else:
# print("Приветствую, {}!".format(user['first_name']))
# print("Авторизация прошла успешно!")
friends_to_save = api.friends.get(user_id=0, fields='sex, bdate, city, relation, status')
try: # Проверяем наличие файла со списком друзей
file = open("friends.ini", 'r')
except IOError:
file = open("friends.ini", 'w')
file.writelines(str(friends_to_save) + '\n')
return
# Загружаем список друзей за предыдущий период из файла в словарь
# file = open("friends.ini", 'r')
old_friends = eval(file.read())
# Создаём словарь текущих друзей
keys, items = [f['id'] for f in friends_to_save['items']], [f for f in friends_to_save['items']]
friends = dict(zip(keys, items))
# Создаём словарь друзей предыдущего периода
keys, items = [f['id'] for f in old_friends['items']], [f for f in old_friends['items']]
old_friends = dict(zip(keys, items))
deleted = [f for f in old_friends.keys() if not(f in friends.keys())]
added = [f for f in friends.keys() if f not in old_friends.keys()]
changed = []
for id in friends.keys():
if id not in added:
for item in EXCLUDE:
friends[id].pop(item, None)
old_friends[id].pop(item, None)
if friends[id] != old_friends[id]:
changed.append(id)
message = ''
if len(deleted) > 0 and 'deleted' in INCLUDE:
message += '\nПользовател(ь/и) удалил(ся/ись):\n'
for d in deleted:
message += f'http://vk.com/id{d}\n'
print(deleted)
print(added)
print(changed)
# Печать изменений
message += '\nПользовател(ь/и) внес(ли) изменения:\n'
for c in changed:
print(f"Было: {old_friends[c]}")
print(f"Стало: {friends[c]}")
for key in friends[c]:
if key not in old_friends[c]:
# print("Было: ", key, ": -")
# print("Стало: ", key, ":", friends[c][key])
if key in INCLUDE:
message += f'http://vk.com/id{c}\n'
message += f'Было: {key}: -\n'
message += f'Стало: {key}: {friends[c][key]}\n'
elif friends[c][key] != old_friends[c][key]:
# print("Было: ", key, ":", old_friends[c][key])
# print("Стало: ", key, ":", friends[c][key])
if key in INCLUDE:
message += f'http://vk.com/id{c}\n'
message += f'Было: {key}: {old_friends[c][key]}\n'
message += f'Стало: {key}: {friends[c][key]}\n'
print(message)
file = open("friends.ini", 'w')
file.writelines(str(friends_to_save) + '\n')
if __name__ == "__main__":
main()