[extractor/niconico] Download comments from the new endpoint (#6773)

Authored by: Lesmiscore
This commit is contained in:
Lesmiscore 2023-04-12 01:19:34 +09:00 committed by GitHub
parent 26010b5cec
commit 52ecc33e22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 15 deletions

View File

@ -477,23 +477,32 @@ class NiconicoIE(InfoExtractor):
user_id_str = session_api_data.get('serviceUserId') user_id_str = session_api_data.get('serviceUserId')
thread_ids = traverse_obj(api_data, ('comment', 'threads', lambda _, v: v['isActive'])) thread_ids = traverse_obj(api_data, ('comment', 'threads', lambda _, v: v['isActive']))
raw_danmaku = self._extract_all_comments(video_id, thread_ids, user_id_str, comment_user_key) legacy_danmaku = self._extract_legacy_comments(video_id, thread_ids, user_id_str, comment_user_key) or []
if not raw_danmaku:
new_comments = traverse_obj(api_data, ('comment', 'nvComment'))
new_danmaku = self._extract_new_comments(
new_comments.get('server'), video_id,
new_comments.get('params'), new_comments.get('threadKey'))
if not legacy_danmaku and not new_danmaku:
self.report_warning(f'Failed to get comments. {bug_reports_message()}') self.report_warning(f'Failed to get comments. {bug_reports_message()}')
return return
return { return {
'comments': [{ 'comments': [{
'ext': 'json', 'ext': 'json',
'data': json.dumps(raw_danmaku), 'data': json.dumps(legacy_danmaku + new_danmaku),
}], }],
} }
def _extract_all_comments(self, video_id, threads, user_id, user_key): def _extract_legacy_comments(self, video_id, threads, user_id, user_key):
auth_data = { auth_data = {
'user_id': user_id, 'user_id': user_id,
'userkey': user_key, 'userkey': user_key,
} if user_id and user_key else {'user_id': ''} } if user_id and user_key else {'user_id': ''}
api_url = traverse_obj(threads, (..., 'server'), get_all=False)
# Request Start # Request Start
post_data = [{'ping': {'content': 'rs:0'}}] post_data = [{'ping': {'content': 'rs:0'}}]
for i, thread in enumerate(threads): for i, thread in enumerate(threads):
@ -532,17 +541,32 @@ class NiconicoIE(InfoExtractor):
# Request Final # Request Final
post_data.append({'ping': {'content': 'rf:0'}}) post_data.append({'ping': {'content': 'rf:0'}})
for api_url in self._COMMENT_API_ENDPOINTS: return self._download_json(
comments = self._download_json( f'{api_url}/api.json', video_id, data=json.dumps(post_data).encode(), fatal=False,
api_url, video_id, data=json.dumps(post_data).encode(), fatal=False, headers={
headers={ 'Referer': f'https://www.nicovideo.jp/watch/{video_id}',
'Referer': 'https://www.nicovideo.jp/watch/%s' % video_id, 'Origin': 'https://www.nicovideo.jp',
'Origin': 'https://www.nicovideo.jp', 'Content-Type': 'text/plain;charset=UTF-8',
'Content-Type': 'text/plain;charset=UTF-8', },
}, note='Downloading comments', errnote=f'Failed to access endpoint {api_url}')
note='Downloading comments', errnote=f'Failed to access endpoint {api_url}')
if comments: def _extract_new_comments(self, endpoint, video_id, params, thread_key):
return comments comments = self._download_json(
f'{endpoint}/v1/threads', video_id, data=json.dumps({
'additionals': {},
'params': params,
'threadKey': thread_key,
}).encode(), fatal=False,
headers={
'Referer': 'https://www.nicovideo.jp/',
'Origin': 'https://www.nicovideo.jp',
'Content-Type': 'text/plain;charset=UTF-8',
'x-client-os-type': 'others',
'x-frontend-id': '6',
'x-frontend-version': '0',
},
note='Downloading comments (new)', errnote='Failed to download comments (new)')
return traverse_obj(comments, ('data', 'threads', ..., 'comments', ...))
class NiconicoPlaylistBaseIE(InfoExtractor): class NiconicoPlaylistBaseIE(InfoExtractor):