Implement module for getting video information
This commit is contained in:
@@ -41,3 +41,6 @@ class IllegalFunctionCall(Exception):
|
||||
get()を呼び出した場合の例外
|
||||
'''
|
||||
pass
|
||||
|
||||
class InvalidVideoIdException(Exception):
|
||||
pass
|
||||
|
||||
0
pytchat/tool/__init__.py
Normal file
0
pytchat/tool/__init__.py
Normal file
30
pytchat/tool/videoinfo.py
Normal file
30
pytchat/tool/videoinfo.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import json
|
||||
import re
|
||||
import requests
|
||||
from .. import config
|
||||
from .. import util
|
||||
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:]
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user