forked from plaxeen/vk-online-checker-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
184 lines (137 loc) · 5.6 KB
/
main.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# main.py
# (c) Oleg Plaxin 2018
# plaxoleg@gmail.com
from requests_html import HTMLSession
from prometheus_client import start_http_server, Gauge
import sys
import time
import os
import configparser
config_name = "Settings"
vkapiuri_tag = "VKAPIURI"
accesstoken_tag = "ACCESSTOKEN"
version_tag = "VKAPIVERSION"
user_state = Gauge('vk_user_state', 'Description of summary', ['id'])
def check_user(ids):
TAG = "user checker"
user_ids = ",".join(str(e) for e in ids)
get_link = vk_api_link + "users.get?user_ids=" + str(user_ids) + "&fields=sex,online,last_seen&access_token=" + \
access_token + "&v=" + v + "&lang=ru"
ms = int(round(time.time() * 1000))
content = HTMLSession().get(get_link)
json = content.json()
countMillis = int(round(time.time() * 1000)) - ms
sex = [["была в сети", "был в сети"], ["в сети", "в сети"]]
device = ["с мобильного", "с iPhone", "с iPad", "с Android", "с Windows Phone", "с Windows 10", "с ПК", "с VK Mobile"]
log("br", "")
title_mes = ""
user_online = 0
for i in range(num_of_user):
userinfo = json["response"][i]
userstat = userinfo["last_seen"]
nameofuser = userinfo["first_name"] + " " + userinfo["last_name"]
user_id = userinfo["id"]
ms_time = time.gmtime(int(userstat["time"]) + 10800)
log("online", nameofuser + " " + sex[int(userinfo["online"])][int(userinfo["sex"]) - 1] +
" " + device[int(userstat["platform"])-1] + ", " + time.strftime("%d %b %Y %H:%M:%S", ms_time))
if int(userinfo["online"]):
user_online += 1
user_state.labels(id=user_id).set(1)
else:
user_state.labels(id=user_id).set(0)
if len(users) > 1:
title_mes = str(num_of_user) + " наблюдаемых / " + str(user_online) + " в сети"
else:
title_mes = nameofuser + " " + sex[int(userinfo["online"])][int(userinfo["sex"]) - 1] + \
" " + device[int(userstat["platform"])-1] + ", " + time.strftime("%H:%M:%S", ms_time)
log("br", "")
log(TAG, title_mes)
log(TAG, "Задержка " + str(countMillis) + " мс.")
# def title_ch(title_string):
# print("title " + title_string)
def log(tag, message):
now = time.localtime()
# log_to_file = open(path + str(now.tm_mday) + ".log", "a")
nowtime = time.strftime("%d %b %Y %H:%M:%S", now)
if str(tag) == "i":
text_return = "\n" + str(nowtime) + ": " + str(message) + "\n"
elif str(tag) == "br":
text_return = ""
else:
text_return = str(nowtime) + ": " + str(tag) + ": " + str(message)
print(text_return)
# log_to_file.write(text_return + "\n")
# log_to_file.close()
def createConfig(path):
config = configparser.ConfigParser()
config.add_section(config_name)
config.set(config_name, vkapiuri_tag, "https://api.vk.com/method/")
config.set(config_name, accesstoken_tag, "2cbc788c2cbc788c2cbc788c082cde2a0322cbc2cbc788c7670e35f9811d37cde017f2b")
config.set(config_name, version_tag, "5.80")
with open(path, "w") as config_file:
config.write(config_file)
def readConfig(path, param):
if not os.path.exists(path):
createConfig(path)
config = configparser.ConfigParser()
config.read(path)
return config.get(config_name, param)
if __name__ == "__main__":
TAG = "main"
startup = time.time()
runtimes = 0
print("\n\tVKOC ver.1.1\t")
vk_api_link = str(readConfig("./config.ini", vkapiuri_tag))
access_token = str(readConfig("./config.ini", accesstoken_tag))
v = str(readConfig("./config.ini", version_tag))
print("\nИнициализация успешна.\nVK API URI = " + vk_api_link + "\nACCESS TOKEN = " + access_token +
"\nVK API VERSION = " + v + "\n")
num_of_user = 0
users = []
tdelay = 1
if len(sys.argv) >= 2:
users = str(sys.argv[1]).split(",")
num_of_user = len(users)
tdelay = sys.argv[2]
else:
num_of_user = int(input("Колличество отслеживаемых пользователей: "))
users = []
for i in range(num_of_user):
users.append(input("ID пользователя " + str(i + 1) + ": "))
tdelay = input("Введите задержку таймера (мин): ")
users.sort()
timer_delay = tdelay
now = time.localtime()
if now.tm_mon < 10:
month = "0" + str(now.tm_mon)
else:
month = str(now.tm_mon)
path = None
if len(users) > 1:
users_s = "; ".join(str(e) for e in users)
path = "logs/" + "few users/" + users_s + "/" + str(now.tm_year) + month + "/"
else:
path = "logs/" + str(users[0]) + "/" + str(now.tm_year) + month + "/"
if not os.path.exists(path):
os.makedirs(path)
start_http_server(8123)
while True:
runtimes += 1
log("br", "")
log(TAG, "### ###")
log(TAG, "Программный цикл: " + str(runtimes))
try:
if users is None and users == "":
break
else:
check_user(users)
timer_delay = tdelay
except Exception as e:
log("error", "Произошла ошибка: " + str(e))
timer_delay = 1
uptime = round(time.time() * 1000) - round(startup * 1000)
log(TAG, "Повтор команды через " + str(timer_delay) + " минут.")
log(TAG, "Uptime: " + str(uptime) + " ms")
time.sleep(60 * float(timer_delay))