Skip to content

Commit

Permalink
优化niconico直播相关逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
auqhjjqdo committed Jul 30, 2023
1 parent 2ea96c7 commit 7251618
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- [x] 抖音
- [x] YouTube
- [x] Twitch
- [x] NicoNico
- [x] TwitCasting
- [x] Afreeca
- [ ] 更多平台欢迎PR
Expand Down Expand Up @@ -128,6 +129,12 @@ YouTube的频道ID一般是由`UC`开头的一段字符,由于YouTube可以自
获取YouTube的频道ID可以在打开频道主页后,按F12打开开发者工具,在控制台输入`ytInitialData.metadata.channelMetadataRenderer.externalId`
,返回的字符即YouTube的频道ID

#### NicoNico的用户ID和频道ID

NicoNico的直播分为用户直播和频道直播,其ID分别以`co``ch`开头再加上一段数字,但NicoNico的直播间一般是以`lv`开头的视频ID,获取用户ID或频道ID可在F12开发者工具的控制台输入`NicoGoogleTagManagerDataLayer[0].content`,在返回的数据中`community_id``channel_id`的值即对应的用户ID或频道ID

其中部分频道在使用频道ID时无法获取到最新直播,此问题暂时无解,请使用`lv`视频ID代替

#### TwitCasting的检测间隔

由于直播检测请求使用了HTTP
Expand Down
5 changes: 5 additions & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
"id": "kanotic_",
"name": "桜帆鹿乃"
},
{
"platform": "Niconico",
"id": "ch2648052",
"name": "niconico-Vtuber"
},
{
"platform": "Twitcasting",
"id": "kano_2525",
Expand Down
32 changes: 16 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,22 @@ async def run(self):
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')


class Niconico(LiveRecoder):
async def run(self):
url = f'https://live.nicovideo.jp/watch/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=url
)).text
if '"content_status":"ON_AIR"' in response:
title = json.loads(
re.search(r'<script type="application/ld\+json">(.*?)</script>', response).group(1)
)['name']
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')


class Twitcasting(LiveRecoder):
async def run(self):
url = f'https://twitcasting.tv/{self.id}'
Expand Down Expand Up @@ -371,22 +387,6 @@ async def run(self):
await asyncio.to_thread(self.run_record, stream, url, title, 'ts')


class NicoNico(LiveRecoder):
async def run(self):
url = f'https://live.nicovideo.jp/watch/{self.id}'
if url not in recording:
response = (await self.request(
method='GET',
url=url,
)).text
live_json = json.loads(re.search('<script type="application/ld\+json">(.*?)</script>', response).group(1))
end_ts = datetime.strptime(live_json['publication']['endDate'], '%Y-%m-%dT%H:%M:%S%z').timestamp()
now_ts = datetime.now().timestamp()
if end_ts > now_ts:
stream = self.get_streamlink().streams(url).get('best') # HLSStream[mpegts]
await asyncio.to_thread(self.run_record, stream, url, live_json['name'], 'flv')


async def run():
with open('config.json', 'r', encoding='utf-8') as f:
config = json.load(f)
Expand Down

0 comments on commit 7251618

Please sign in to comment.