Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions liveMan.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,24 @@ def __init__(self, live_id, abogus_file='a_bogus.js'):
self.headers = {
'User-Agent': self.user_agent
}
self._running = False

def start(self):
self._connectWebSocket()
self._running = True
while self._running:
try:
self._connectWebSocket()
if self._running:
print("【i】WebSocket 连接意外断开,5秒后将重新连接...")
time.sleep(5)
except Exception as e:
print("【X】WebSocket 连接意外断开:", e)
time.sleep(5)

def stop(self):
self.ws.close()
self._running = False
if self.ws:
self.ws.close()

@property
def ttwid(self):
Expand Down Expand Up @@ -268,11 +280,7 @@ def _connectWebSocket(self):
on_message=self._wsOnMessage,
on_error=self._wsOnError,
on_close=self._wsOnClose)
try:
self.ws.run_forever()
except Exception:
self.stop()
raise
self.ws.run_forever()

def _sendHeartbeat(self):
"""
Expand Down
24 changes: 20 additions & 4 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@
# @Author: bubu
# @Project: douyinLiveWebFetcher

import signal
from liveMan import DouyinLiveWebFetcher

room: DouyinLiveWebFetcher


def signal_handler(sig, frame):
print("Received signal:", sig)
if room:
room.stop()


if __name__ == '__main__':
live_id = '510200350291'
room = DouyinLiveWebFetcher(live_id)
# room.get_room_status() # 失效
room.start()
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)

try:
live_id = '510200350291'
room = DouyinLiveWebFetcher(live_id)
# room.get_room_status() # 失效
room.start()
except Exception as e:
print(e)