mirror of
https://github.com/ytdl-org/youtube-dl.git
synced 2024-12-22 08:47:41 +00:00
[invidious] Add new extractor
This commit is contained in:
parent
195f22f679
commit
991ac059a4
2 changed files with 189 additions and 0 deletions
|
@ -1667,3 +1667,4 @@ from .zingmp3 import (
|
|||
)
|
||||
from .zoom import ZoomIE
|
||||
from .zype import ZypeIE
|
||||
from .invidious import InvidiousIE, InvidiousPlaylistIE
|
||||
|
|
188
youtube_dl/extractor/invidious.py
Normal file
188
youtube_dl/extractor/invidious.py
Normal file
|
@ -0,0 +1,188 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from .common import InfoExtractor
|
||||
from ..compat import compat_urllib_parse
|
||||
|
||||
INSTANCES = [
|
||||
'y.com.sb',
|
||||
'yt.artemislena.eu'
|
||||
]
|
||||
|
||||
INSTANCES_HOST_REGEX = '(?:' + '|'.join([instance.replace('.', r'\.') for instance in INSTANCES]) + ')'
|
||||
|
||||
|
||||
class InvidiousIE(InfoExtractor):
|
||||
_VALID_URL = r'(?:https?://)?(?:www\.)?' + INSTANCES_HOST_REGEX + r'/watch\?v=(?P<id>.+)'
|
||||
_TEST = {
|
||||
'url': 'https://y.com.sb/watch?v=xKTygGa6hg0',
|
||||
'md5': 'fb95b1847f5a90af14fda5273fc84fc0',
|
||||
'info_dict': {
|
||||
'id': 'xKTygGa6hg0',
|
||||
'ext': 'mp4',
|
||||
'title': 'Coding in C++ - Creating a Player Controller - CRYENGINE Summer Academy S1E5 - [Tutorial]',
|
||||
'uploader': 'CRYENGINE',
|
||||
'uploader_id': 'UCtaXcIVFp8HEpthm7qwtKCQ',
|
||||
'description': 'md5:7aa75816d40ffccdbf3e15a90b05fca3',
|
||||
}
|
||||
}
|
||||
|
||||
def __init__(self, downloader=None):
|
||||
super().__init__(downloader)
|
||||
|
||||
# type is either 'video' or 'audio'
|
||||
# ext is the file extension
|
||||
@staticmethod
|
||||
def _get_additional_format_data(format_type, bitrate, resolution, fps):
|
||||
out = {}
|
||||
|
||||
try:
|
||||
type_and_ext, codecs = format_type.split(';')
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
type_, ext = type_and_ext.split('/')
|
||||
# codec = codecs.split('"')[1]
|
||||
out['ext'] = ext
|
||||
# if type_ == 'audio':
|
||||
# out['acodec'] = codec
|
||||
# elif type_ == 'video':
|
||||
# out['vcodec'] = codec
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
bitrate = float(bitrate) / 1000
|
||||
# if type_ == 'audio':
|
||||
# out['abr'] = bitrate
|
||||
# elif type_ == 'video':
|
||||
# out['vbr'] = bitrate
|
||||
# out['tbr'] = bitrate
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
if type_ == 'audio':
|
||||
out['resolution'] = type_and_ext + ' @ ' + str(bitrate) + 'k - audio only'
|
||||
elif type_ == 'video':
|
||||
out['resolution'] = resolution + ' - ' + type_and_ext + ' @ ' + str(fps) + 'fps - video only'
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return out
|
||||
|
||||
def _patch_url(self, url):
|
||||
return compat_urllib_parse.urlparse(url)._replace(netloc=self.url_netloc).geturl()
|
||||
|
||||
def _get_formats(self, api_response):
|
||||
all_formats = []
|
||||
|
||||
# Video/audio only
|
||||
for format_ in api_response.get('adaptiveFormats') or []:
|
||||
all_formats.append({
|
||||
'url': self._patch_url(format_['url']),
|
||||
'format_id': format_.get('itag'),
|
||||
# 'fps': format_.get('fps'),
|
||||
# 'container': format_.get('container')
|
||||
} | InvidiousIE._get_additional_format_data(format_.get('type'), format_.get('bitrate'), format_.get('resolution'), format_.get('fps')))
|
||||
|
||||
# Both video and audio
|
||||
for format_ in api_response.get('formatStreams') or []:
|
||||
all_formats.append({
|
||||
'url': self._patch_url(format_['url']),
|
||||
'format_id': format_.get('itag'),
|
||||
# 'fps': format_.get('fps'),
|
||||
# 'container': format_.get('container')
|
||||
} | InvidiousIE._get_additional_format_data(format_.get('type'), format_.get('bitrate'), format_.get('resolution'), format_.get('fps')))
|
||||
|
||||
return all_formats
|
||||
|
||||
def _get_thumbnails(self, api_response):
|
||||
thumbnails = []
|
||||
video_thumbnails = api_response.get('videoThumbnails') or []
|
||||
|
||||
for inversed_quality, thumbnail in enumerate(video_thumbnails):
|
||||
thumbnails.append({
|
||||
'id': thumbnail.get('quality'),
|
||||
'url': thumbnail.get('url'),
|
||||
'quality': len(video_thumbnails) - inversed_quality,
|
||||
'width': thumbnail.get('width'),
|
||||
'height': thumbnail.get('height')
|
||||
})
|
||||
|
||||
return thumbnails
|
||||
|
||||
def _real_extract(self, url):
|
||||
video_id = self._match_id(url)
|
||||
webpage = None
|
||||
|
||||
# host_url will contain `http[s]://example.com` where `example.com` is the used invidious instance.
|
||||
url_parsed = compat_urllib_parse.urlparse(url)
|
||||
self.url_netloc = url_parsed.netloc
|
||||
host_url = url_parsed.scheme + '://' + url_parsed.netloc
|
||||
|
||||
api_response = self._download_json(host_url + '/api/v1/videos/' + video_id, video_id)
|
||||
|
||||
def download_webpage_and(fn, fatal=True):
|
||||
global webpage
|
||||
if webpage is None:
|
||||
webpage = self._download_webpage(url, video_id, fatal=fatal)
|
||||
return fn()
|
||||
|
||||
out = {
|
||||
'id': video_id,
|
||||
'title': api_response.get('title') or download_webpage_and(lambda: self._og_search_title(webpage)),
|
||||
'description': api_response.get('description') or download_webpage_and(lambda: self._og_search_description(webpage)),
|
||||
|
||||
'release_timestamp': api_response.get('published'),
|
||||
|
||||
'uploader': api_response.get('author'),
|
||||
'uploader_id': api_response.get('authorId'),
|
||||
'channel': api_response.get('author'),
|
||||
'channel_id': api_response.get('authorId'),
|
||||
'channel_url': host_url + api_response.get('authorUrl'),
|
||||
|
||||
'duration': api_response.get('lengthSeconds'),
|
||||
|
||||
'view_count': api_response.get('viewCount'),
|
||||
'like_count': api_response.get('likeCount'),
|
||||
'dislike_count': api_response.get('dislikeCount'),
|
||||
|
||||
# 'isFamilyFriendly': 18 if api_response.get('isFamilyFriendly') == False else None
|
||||
|
||||
'tags': api_response.get('keywords'),
|
||||
'is_live': api_response.get('liveNow'),
|
||||
|
||||
'formats': self._get_formats(api_response),
|
||||
'thumbnails': self._get_thumbnails(api_response)
|
||||
}
|
||||
|
||||
if api_response.get('isFamilyFriendly') is False:
|
||||
out['age_limit'] = 18
|
||||
|
||||
return out
|
||||
|
||||
|
||||
class InvidiousPlaylistIE(InfoExtractor):
|
||||
_VALID_URL = r'(?:https?://)?(?:www\.)?' + INSTANCES_HOST_REGEX + r'/playlist\?list=(?P<id>.+)'
|
||||
|
||||
def _get_entries(self, api_response):
|
||||
out = []
|
||||
for video in api_response['videos']:
|
||||
out.append(InvidiousIE(self._downloader)._real_extract(self.host_url + '/watch?v=' + video['videoId']))
|
||||
return out
|
||||
|
||||
def _real_extract(self, url):
|
||||
playlist_id = self._match_id(url)
|
||||
|
||||
# host_url will contain `http[s]://example.com` where `example.com` is the used invidious instance.
|
||||
url_parsed = compat_urllib_parse.urlparse(url)
|
||||
self.host_url = url_parsed.scheme + '://' + url_parsed.netloc
|
||||
|
||||
api_response = self._download_json(self.host_url + '/api/v1/playlists/' + playlist_id, playlist_id)
|
||||
return InfoExtractor.playlist_result(self._get_entries(api_response), playlist_id, api_response.get('title'), api_response.get('description')) | {
|
||||
'release_timestamp': api_response.get('updated'),
|
||||
|
||||
'uploader': api_response.get('author'),
|
||||
'uploader_id': api_response.get('authorId'),
|
||||
}
|
Loading…
Reference in a new issue