Skip to content
This repository was archived by the owner on Sep 26, 2022. It is now read-only.

Commit 61a055d

Browse files
committed
fix bugs
1 parent 33f67ea commit 61a055d

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

LiveStreams.py

+15-4
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ def Del(self, string):
3535

3636

3737
class Streamer:
38+
state = None
39+
rbcThread = None
40+
queue = None
41+
3842
def __init__(self, name, channelId):
3943
self.name = name
4044
self.channelId = channelId
@@ -46,6 +50,7 @@ def cancel(self):
4650
async def autocheck(self):
4751
while True:
4852
state = await self.check()
53+
logging.debug(f'state:{state}--self.state:{self.state}')
4954
if state != self.state:
5055
self.changeRebroadcast(state)
5156
self.state = state
@@ -54,14 +59,20 @@ async def autocheck(self):
5459
def changeRebroadcast(self, state):
5560
logging.debug(f'改变转播状态:[{self.name}]{state}')
5661
if not state is None: # 正在直播中
57-
if self.rbcThread is None:
58-
self.queue = queue.Queue()
59-
self.rbcThread = Rebroadcast.RebroadcastThread(self.queue)
60-
self.queue.put(self.name, self.channelId)
62+
if not self.rbcThread is None:
63+
self.queue.put('stop')
64+
self.rbcThread = None
65+
self.queue = None
66+
self.queue = queue.Queue()
67+
self.queue.put((self.name, state))
68+
self.rbcThread = Rebroadcast.RebroadcastThread(self.queue)
6169
self.rbcThread.start()
70+
6271
else: # 不在直播中
6372
if not self.rbcThread is None:
6473
self.queue.put('stop')
74+
self.rbcThread = None
75+
self.queue = None
6576

6677
async def check(self):
6778
logging.debug('直播状态检测:'+self.name)

Rebroadcast.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import time
33
import queue
44
import logging
5+
import subprocess
56

67
TemplateArg = r'streamlink --hls-live-edge 1 "https://www.youtube.com/watch?v={}" "best" -O | ffmpeg -re -i pipe:0 -c copy -f flv rtmp://localhost/flv/{}'
78
workingdir = r'/web/hls'
@@ -13,6 +14,7 @@ def __init__(self, queue):
1314
# self.threadID = threadID
1415
self.queue = queue
1516
self.name, self.id = queue.get()
17+
logging.debug(f'RebroadcastThread:[name]{self.name}[id]{self.id}')
1618

1719
def run(self):
1820
arg = TemplateArg.format(self.id, self.name)

main.py

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import logging
66
import sys
77
import asyncio
8+
import traceback
89
import aioconsole
910

1011

@@ -25,11 +26,20 @@ async def mainConsole(manager):
2526
else:
2627
print('未知的控制台命令')
2728

29+
def exception_handler(loop, context):
30+
logging.error('Exception handler called')
31+
traceback.print_tb(context['exception'])
32+
2833
if __name__ == "__main__":
2934
logging.basicConfig(
3035
format='%(asctime)s[%(levelname)s]%(threadName)s>%(message)s', level=logging.DEBUG)
3136
logging.info('eventloop已启动')
3237
manager = LiveStreams.StreamerManager()
38+
# manager.Add('tamaki','UC8NZiqKx6fsDT3AVcMiVFyA')
39+
# add matsuri UCQ0UDLQCjY0rmuxCDE38FGg
40+
# add kizunaai UC4YaOt1yT-ZeyB0OmxHgolA
41+
asyncio.get_event_loop().set_debug(True)
3342
asyncio.ensure_future(manager.run())
3443
asyncio.ensure_future(mainConsole(manager))
44+
asyncio.get_event_loop().set_exception_handler(exception_handler)
3545
asyncio.get_event_loop().run_forever()

youtube_util.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ async def getLiveVideoId(id):
1515

1616

1717
async def checkIsLive(videoid):
18-
fp = urllib.request.urlopen(Request(
18+
fp = urllib.request.urlopen(urllib.request.Request(
1919
f"https://www.youtube.com/watch?v={videoid}", headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.24 Safari/537.36'}))
2020
mybytes = fp.read()
2121
fp.close()
@@ -26,7 +26,7 @@ async def checkIsLive(videoid):
2626

2727

2828
async def channelId2videoId(channelId):
29-
fp = urllib.request.urlopen(Request(
29+
fp = urllib.request.urlopen(urllib.request.Request(
3030
f"https://www.youtube.com/channel/{channelId}/live", headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.24 Safari/537.36'}))
3131
mybytes = fp.read()
3232
fp.close()

0 commit comments

Comments
 (0)