-
Notifications
You must be signed in to change notification settings - Fork 35
/
nicoHeartBeat.py
71 lines (65 loc) · 2.72 KB
/
nicoHeartBeat.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
# (C) 2019-2021 lifegpc
# This file is part of bili.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from requests import Session
from time import time, sleep
from Logger import Logger
from json import dumps
from inspect import currentframe
from threading import Thread
def sendNicoHeartBeat(r: Session, session: dict, url: str, logg: Logger) -> (dict, int):
'''NicoNico发送心跳包
- session Session字典
- url Session URL
返回新Session'''
url = f"{url}/{session['id']}?_format=json&_method=PUT"
t = round(time() * 1000)
session['modified_time'] = t
para = {"session": session}
if logg:
logg.write(f"POST {url}\nPOST DATA: {dumps(para)}", currentframe(), "Send HeartBeat")
re = r.post(url, json=para)
if logg:
logg.write(f"status = {re.status_code}\n{re.text}", currentframe(), "HeartBeat Result")
if re.status_code >= 400:
return None, None
return re.json()['data']['session'], t / 1000
class nicoNormalVideoHeartBeatThread(Thread):
def __init__(self, threadName: str, r: Session, session: dict, url: str, logg: Logger):
Thread.__init__(self, name=f"HeartBeat:{threadName}")
self._r = r
self._session = session
self._url = url
self._logg = logg
self._mystop = False
def kill(self):
self._mystop = True
if self._logg:
self._logg.write(f"{self.name}: Get Kill Signial", currentframe(), "NicoNico Normal Video Heart Beat Thread Get Kill")
def run(self):
self._session, lastSendHeartBeat = sendNicoHeartBeat(self._r, self._session, self._url, self._logg)
if self._session is None:
return
while True:
if self._mystop:
break
if time() < lastSendHeartBeat + 90:
sleep(1)
else:
self._session, lastSendHeartBeat = sendNicoHeartBeat(self._r, self._session, self._url, self._logg)
if self._session is None:
return
if self._logg:
self._logg.write(f"{self.name}: Exited", currentframe(), "NicoNico Normal Video Heart Beat Thread Killed")