Modify usage of videoinfo

This commit is contained in:
taizan-hokuto
2020-01-26 23:29:50 +09:00
parent cc78551e90
commit 38253e1d18
3 changed files with 31 additions and 19 deletions

View File

@@ -7,24 +7,32 @@ from ..exceptions import InvalidVideoIdException
headers = config.headers
pattern=re.compile(r"yt\.setConfig\({'PLAYER_CONFIG': ({.*})}\);")
def _parse(html):
result = re.search(pattern, html)
res= json.loads(result.group(1))
response = res["args"].get("embedded_player_response")
if response is None:
raise InvalidVideoIdException("Invalid video_id.")
renderer = (json.loads(response))["embedPreview"]["thumbnailPreviewRenderer"]
return {
"duration": int(renderer["videoDurationSeconds"]),
"title" : [''.join(run["text"]) for run in renderer["title"]["runs"]][0],
"channelId" : renderer["videoDetails"]["embeddedPlayerOverlayVideoDetailsRenderer"]["channelThumbnailEndpoint"]["channelThumbnailEndpoint"]["urlEndpoint"]["urlEndpoint"]["url"][9:]
}
class VideoInfo:
def __init__(self,video_id):
self.video_id = video_id
self.info = self._get_info(video_id)
def get_info(video_id):
url = f"https://www.youtube.com/embed/{video_id}"
resp= requests.get(url, headers = headers)
resp.raise_for_status()
return _parse(resp.text)
def _get_info(self,video_id):
url = f"https://www.youtube.com/embed/{video_id}"
resp= requests.get(url, headers = headers)
resp.raise_for_status()
return self._parse(resp.text)
def _parse(self,html):
result = re.search(pattern, html)
res= json.loads(result.group(1))
response = res["args"].get("embedded_player_response")
if response is None:
raise InvalidVideoIdException("動画IDが無効です。")
renderer = (json.loads(response))["embedPreview"]["thumbnailPreviewRenderer"]
return {
"duration": int(renderer["videoDurationSeconds"]),
"title" : [''.join(run["text"]) for run in renderer["title"]["runs"]][0],
"channelId" : renderer["videoDetails"]["embeddedPlayerOverlayVideoDetailsRenderer"]["channelThumbnailEndpoint"]["channelThumbnailEndpoint"]["urlEndpoint"]["urlEndpoint"]["url"][9:]
}
def get(self,item):
return self.info.get(item)