Fix extract video info

This commit is contained in:
taizan-hokuto
2020-09-04 01:46:10 +09:00
parent 306b0a4564
commit 235d6b7212
3 changed files with 30 additions and 3 deletions

View File

@@ -93,7 +93,8 @@ class VideoInfo:
result = re.search(pattern, text) result = re.search(pattern, text)
if result is None: if result is None:
raise PatternUnmatchError(text) raise PatternUnmatchError(text)
res = json.loads(result.group(1)[:-1]) decoder = json.JSONDecoder()
res = decoder.raw_decode(result.group(1)[:-1])[0]
response = self._get_item(res, item_response) response = self._get_item(res, item_response)
if response is None: if response is None:
self._check_video_is_private(res.get("args")) self._check_video_is_private(res.get("args"))

View File

@@ -1,7 +1,6 @@
from json.decoder import JSONDecodeError from json.decoder import JSONDecodeError
from pytchat.tool.videoinfo import VideoInfo from pytchat.tool.videoinfo import VideoInfo
from pytchat.exceptions import InvalidVideoIdException, PatternUnmatchError from pytchat.exceptions import InvalidVideoIdException, PatternUnmatchError
from pytchat import util
def _open_file(path): def _open_file(path):
@@ -32,7 +31,7 @@ def test_archived_page(mocker):
def test_live_page(mocker): def test_live_page(mocker):
_set_test_data('tests/testdata/videoinfo/live_page.txt', mocker) _set_test_data('tests/testdata/videoinfo/live_page.txt', mocker)
info = VideoInfo('__test_id__') info = VideoInfo('__test_id__')
'''live page :duration = 0''' '''live page: duration==0'''
assert info.get_duration() == 0 assert info.get_duration() == 0
assert info.video_id == '__test_id__' assert info.video_id == '__test_id__'
assert info.get_channel_name() == 'BGM channel' assert info.get_channel_name() == 'BGM channel'
@@ -88,3 +87,15 @@ def test_pattern_unmatch(mocker):
assert False assert False
except PatternUnmatchError: except PatternUnmatchError:
assert True assert True
def test_extradata_handling(mocker):
'''Test case the extracted data are JSON lines.'''
_set_test_data(
'tests/testdata/videoinfo/extradata_page.txt', mocker)
try:
_ = VideoInfo('__test_id__')
assert True
except JSONDecodeError as e:
print(e.doc)
assert False

File diff suppressed because one or more lines are too long