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

Commit 687a9fd

Browse files
committed
request改为aiohttp
1 parent 62d4bbf commit 687a9fd

File tree

3 files changed

+43
-22
lines changed

3 files changed

+43
-22
lines changed

LiveStreams.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def __init__(self, discord=None):
2020
self.discord = discord
2121

2222
def Add(self, name, channelId):
23-
for name, streamer in list(self.streamers.items()):
24-
if streamer.channelId == string or name == string:
23+
for streamername, streamer in list(self.streamers.items()):
24+
if streamer.channelId == channelId or streamername == name:
2525
print(f'已经添加过了[{name}]{streamer.channelId}')
2626
return f'已经添加过了[{name}]{streamer.channelId}'
2727
self.streamers[name] = Streamer(name, channelId, self.discord)

main.py

+13
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,26 @@ def exception_handler(loop, context):
3131
logging.error('Exception handler called')
3232
traceback.print_exc()
3333

34+
# https://github.com/klen/muffin/issues/18#issuecomment-182138773
35+
# avoid frustrating INFO messages about timeout caused by sleep(1)
36+
class SkipTimeouts(logging.Filter):
37+
def filter(self, rec):
38+
if(rec.levelno == logging.INFO and
39+
rec.msg.startswith('poll') and
40+
rec.msg.endswith(': timeout') and
41+
990 < rec.args[0] < 1000 and
42+
1000 < rec.args[1] < 1010):
43+
return False # hide this record
44+
return True
45+
3446
if __name__ == "__main__":
3547
logging.basicConfig(
3648
format='%(asctime)s[%(levelname)s]%(threadName)s>%(message)s', level=logging.DEBUG)
3749
logging.getLogger('discord').setLevel(logging.INFO)
3850
logging.info('eventloop已启动')
3951
asyncio.get_event_loop().set_debug(True)
4052
asyncio.get_event_loop().set_exception_handler(exception_handler)
53+
logging.getLogger('asyncio').addFilter(SkipTimeouts())
4154
########
4255
discord = Discord.DiscordClient()
4356
manager = LiveStreams.StreamerManager(discord)

youtube_util.py

+28-20
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
import logging
33
import re
44
import urllib.request
5+
import aiohttp
6+
7+
headers = {
8+
'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'}
59

610

711
async def getLiveVideoId(id):
@@ -15,26 +19,30 @@ async def getLiveVideoId(id):
1519

1620

1721
async def checkIsLive(videoid):
18-
fp = urllib.request.urlopen(urllib.request.Request(
19-
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'}))
20-
mybytes = fp.read()
21-
fp.close()
22-
htmlsource = mybytes.decode("utf-8")
23-
if re.search(r'"isLive\\":true,', htmlsource) is None: # \"isLive\":true,
24-
return None
25-
return id
22+
async with aiohttp.ClientSession() as session:
23+
async with session.get(f"https://www.youtube.com/watch?v={videoid}", headers=headers) as r:
24+
if r.status == 200:
25+
htmlsource = await r.text()
26+
if re.search(r'"isLive\\":true,', htmlsource) is None: # \"isLive\":true,
27+
return None
28+
return id
29+
else:
30+
logging.error('checkIsLive.status:'+r.status)
31+
return None
2632

2733

2834
async def channelId2videoId(channelId):
29-
fp = urllib.request.urlopen(urllib.request.Request(
30-
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'}))
31-
mybytes = fp.read()
32-
fp.close()
33-
htmlsource = mybytes.decode("utf-8")
34-
if re.search(r'"isLive\\":true,', htmlsource) is None: # \"isLive\":true,
35-
return None
36-
videoid = re.search(r'(?<="videoId\\":\\")(.*?)(?=\\",)', htmlsource)
37-
if re.search(r'(?<="videoId\\":\\")(.*?)(?=\\",)', htmlsource) is None:
38-
logging.warn(r're.search[videoId], htmlsource) is None')
39-
return None
40-
return videoid.group()
35+
async with aiohttp.ClientSession() as session:
36+
async with session.get(f"https://www.youtube.com/channel/{channelId}/live", headers=headers) as r:
37+
if r.status == 200:
38+
htmlsource = await r.text()
39+
if re.search(r'"isLive\\":true,', htmlsource) is None: # \"isLive\":true,
40+
return None
41+
videoid = re.search(r'(?<="videoId\\":\\")(.*?)(?=\\",)', htmlsource)
42+
if re.search(r'(?<="videoId\\":\\")(.*?)(?=\\",)', htmlsource) is None:
43+
logging.warn(r're.search[videoId], htmlsource) is None')
44+
return None
45+
return videoid.group()
46+
else:
47+
logging.error('channelId2videoId.status:'+r.status)
48+
return None

0 commit comments

Comments
 (0)