From 38253e1d188d501b29bc583eafea958c18bda7de Mon Sep 17 00:00:00 2001 From: taizan-hokuto <55448286+taizan-hokuto@users.noreply.github.com> Date: Sun, 26 Jan 2020 23:29:50 +0900 Subject: [PATCH] Modify usage of videoinfo --- pytchat/tool/__init__.py | 3 +++ pytchat/tool/downloader.py | 5 +++-- pytchat/tool/videoinfo.py | 42 +++++++++++++++++++++++--------------- 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pytchat/tool/__init__.py b/pytchat/tool/__init__.py index e69de29..c00d315 100644 --- a/pytchat/tool/__init__.py +++ b/pytchat/tool/__init__.py @@ -0,0 +1,3 @@ +from . videoinfo import VideoInfo +def videoinfo(video_id): + return VideoInfo(video_id) \ No newline at end of file diff --git a/pytchat/tool/downloader.py b/pytchat/tool/downloader.py index 59360b9..0889d58 100644 --- a/pytchat/tool/downloader.py +++ b/pytchat/tool/downloader.py @@ -5,10 +5,11 @@ import traceback from urllib.parse import quote from . import parser +from . import videoinfo from .. import config from .. import util from .. paramgen import arcparam - +from ..exceptions import InvalidVideoIdException logger = config.logger(__name__) headers=config.headers @@ -268,4 +269,4 @@ def check_duplicate(blocks): for i in range(len(blocks)-1): if ( is_same_offset(i) and is_same_id(i) and is_same_type(i) ): ret.append(blocks[i]) - return ret \ No newline at end of file + return ret diff --git a/pytchat/tool/videoinfo.py b/pytchat/tool/videoinfo.py index b63c165..8afa4e4 100644 --- a/pytchat/tool/videoinfo.py +++ b/pytchat/tool/videoinfo.py @@ -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)