Upgraded youtube_download plugin
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import functools
|
||||
import itertools
|
||||
import urllib.parse
|
||||
|
||||
from .common import InfoExtractor
|
||||
from .sproutvideo import VidsIoIE
|
||||
@@ -11,30 +10,38 @@ from ..utils import (
|
||||
ExtractorError,
|
||||
clean_html,
|
||||
determine_ext,
|
||||
extract_attributes,
|
||||
float_or_none,
|
||||
int_or_none,
|
||||
mimetype2ext,
|
||||
parse_iso8601,
|
||||
smuggle_url,
|
||||
str_or_none,
|
||||
update_url_query,
|
||||
url_or_none,
|
||||
urljoin,
|
||||
)
|
||||
from ..utils.traversal import require, traverse_obj, value
|
||||
from ..utils.traversal import (
|
||||
find_elements,
|
||||
require,
|
||||
traverse_obj,
|
||||
trim_str,
|
||||
value,
|
||||
)
|
||||
|
||||
|
||||
class PatreonBaseIE(InfoExtractor):
|
||||
@functools.cached_property
|
||||
def patreon_user_agent(self):
|
||||
# Patreon mobile UA is needed to avoid triggering Cloudflare anti-bot protection.
|
||||
# Newer UA yields higher res m3u8 formats for locked posts, but gives 401 if not logged-in
|
||||
# Patreon mobile UA yields higher res m3u8 for locked posts, but gives 401 if not logged-in
|
||||
if self._get_cookies('https://www.patreon.com/').get('session_id'):
|
||||
return 'Patreon/72.2.28 (Android; Android 14; Scale/2.10)'
|
||||
return 'Patreon/7.6.28 (Android; Android 11; Scale/2.10)'
|
||||
return 'Patreon/126.9.0.15 (Android; Android 14; Scale/2.10)'
|
||||
return None
|
||||
|
||||
def _call_api(self, ep, item_id, query=None, headers=None, fatal=True, note=None):
|
||||
if headers is None:
|
||||
headers = {}
|
||||
if 'User-Agent' not in headers:
|
||||
if 'User-Agent' not in headers and self.patreon_user_agent:
|
||||
headers['User-Agent'] = self.patreon_user_agent
|
||||
if query:
|
||||
query.update({'json-api-version': 1.0})
|
||||
@@ -43,7 +50,9 @@ class PatreonBaseIE(InfoExtractor):
|
||||
return self._download_json(
|
||||
f'https://www.patreon.com/api/{ep}',
|
||||
item_id, note=note if note else 'Downloading API JSON',
|
||||
query=query, fatal=fatal, headers=headers)
|
||||
query=query, fatal=fatal, headers=headers,
|
||||
# If not using Patreon mobile UA, we need impersonation due to Cloudflare
|
||||
impersonate=not self.patreon_user_agent)
|
||||
except ExtractorError as e:
|
||||
if not isinstance(e.cause, HTTPError) or mimetype2ext(e.cause.response.headers.get('Content-Type')) != 'json':
|
||||
raise
|
||||
@@ -121,6 +130,7 @@ class PatreonIE(PatreonBaseIE):
|
||||
'channel_is_verified': True,
|
||||
'chapters': 'count:4',
|
||||
'timestamp': 1423689666,
|
||||
'media_type': 'video',
|
||||
},
|
||||
'params': {
|
||||
'noplaylist': True,
|
||||
@@ -161,7 +171,7 @@ class PatreonIE(PatreonBaseIE):
|
||||
'uploader_url': 'https://www.patreon.com/loish',
|
||||
'description': 'md5:e2693e97ee299c8ece47ffdb67e7d9d2',
|
||||
'title': 'VIDEO // sketchbook flipthrough',
|
||||
'uploader': 'Loish ',
|
||||
'uploader': 'Loish',
|
||||
'tags': ['sketchbook', 'video'],
|
||||
'channel_id': '1641751',
|
||||
'channel_url': 'https://www.patreon.com/loish',
|
||||
@@ -274,8 +284,73 @@ class PatreonIE(PatreonBaseIE):
|
||||
'channel_id': '9346307',
|
||||
},
|
||||
'params': {'getcomments': True},
|
||||
}, {
|
||||
# Inlined media in post; uses _extract_from_media_api
|
||||
'url': 'https://www.patreon.com/posts/scottfalco-146966245',
|
||||
'info_dict': {
|
||||
'id': '146966245',
|
||||
'ext': 'mp4',
|
||||
'title': 'scottfalco 1080',
|
||||
'description': 'md5:a3f29bbd0a46b4821ec3400957c98aa2',
|
||||
'uploader': 'Insanimate',
|
||||
'uploader_id': '2828146',
|
||||
'uploader_url': 'https://www.patreon.com/Insanimate',
|
||||
'channel_id': '6260877',
|
||||
'channel_url': 'https://www.patreon.com/Insanimate',
|
||||
'channel_follower_count': int,
|
||||
'comment_count': int,
|
||||
'like_count': int,
|
||||
'duration': 7.833333,
|
||||
'timestamp': 1767061800,
|
||||
'upload_date': '20251230',
|
||||
},
|
||||
}]
|
||||
_RETURN_TYPE = 'video'
|
||||
_HTTP_HEADERS = {
|
||||
# Must be all-lowercase 'referer' so we can smuggle it to Generic, SproutVideo, and Vimeo.
|
||||
# patreon.com URLs redirect to www.patreon.com; this matters when requesting mux.com m3u8s
|
||||
'referer': 'https://www.patreon.com/',
|
||||
}
|
||||
|
||||
def _extract_from_media_api(self, media_id):
|
||||
attributes = traverse_obj(
|
||||
self._call_api(f'media/{media_id}', media_id, fatal=False),
|
||||
('data', 'attributes', {dict}))
|
||||
if not attributes:
|
||||
return None
|
||||
|
||||
info_dict = traverse_obj(attributes, {
|
||||
'title': ('file_name', {lambda x: x.rpartition('.')[0]}),
|
||||
'timestamp': ('created_at', {parse_iso8601}),
|
||||
'duration': ('display', 'duration', {float_or_none}),
|
||||
})
|
||||
info_dict['id'] = media_id
|
||||
|
||||
playback_url = traverse_obj(
|
||||
attributes, ('display', (None, 'viewer_playback_data'), 'url', {url_or_none}, any))
|
||||
download_url = traverse_obj(attributes, ('download_url', {url_or_none}))
|
||||
|
||||
if playback_url and mimetype2ext(attributes.get('mimetype')) == 'm3u8':
|
||||
info_dict['formats'], info_dict['subtitles'] = self._extract_m3u8_formats_and_subtitles(
|
||||
playback_url, media_id, 'mp4', fatal=False, headers=self._HTTP_HEADERS)
|
||||
for f in info_dict['formats']:
|
||||
f['http_headers'] = self._HTTP_HEADERS
|
||||
if transcript_url := traverse_obj(attributes, ('display', 'transcript_url', {url_or_none})):
|
||||
info_dict['subtitles'].setdefault('en', []).append({
|
||||
'url': transcript_url,
|
||||
'ext': 'vtt',
|
||||
})
|
||||
elif playback_url or download_url:
|
||||
info_dict['formats'] = [{
|
||||
# If playback_url is available, download_url is a duplicate lower resolution format
|
||||
'url': playback_url or download_url,
|
||||
'vcodec': 'none' if attributes.get('media_type') != 'video' else None,
|
||||
}]
|
||||
|
||||
if not info_dict.get('formats'):
|
||||
return None
|
||||
|
||||
return info_dict
|
||||
|
||||
def _real_extract(self, url):
|
||||
video_id = self._match_id(url)
|
||||
@@ -299,6 +374,7 @@ class PatreonIE(PatreonBaseIE):
|
||||
'comment_count': ('comment_count', {int_or_none}),
|
||||
})
|
||||
|
||||
seen_media_ids = set()
|
||||
entries = []
|
||||
idx = 0
|
||||
for include in traverse_obj(post, ('included', lambda _, v: v['type'])):
|
||||
@@ -320,6 +396,8 @@ class PatreonIE(PatreonBaseIE):
|
||||
'url': download_url,
|
||||
'alt_title': traverse_obj(media_attributes, ('file_name', {str})),
|
||||
})
|
||||
if media_id := traverse_obj(include, ('id', {str})):
|
||||
seen_media_ids.add(media_id)
|
||||
|
||||
elif include_type == 'user':
|
||||
info.update(traverse_obj(include, {
|
||||
@@ -340,34 +418,29 @@ class PatreonIE(PatreonBaseIE):
|
||||
'channel_follower_count': ('attributes', 'patron_count', {int_or_none}),
|
||||
}))
|
||||
|
||||
# Must be all-lowercase 'referer' so we can smuggle it to Generic, SproutVideo, and Vimeo.
|
||||
# patreon.com URLs redirect to www.patreon.com; this matters when requesting mux.com m3u8s
|
||||
headers = {'referer': 'https://www.patreon.com/'}
|
||||
if embed_url := traverse_obj(attributes, ('embed', 'url', {url_or_none})):
|
||||
# Convert useless vimeo.com URLs to useful player.vimeo.com embed URLs
|
||||
vimeo_id, vimeo_hash = self._search_regex(
|
||||
r'//vimeo\.com/(\d+)(?:/([\da-f]+))?', embed_url,
|
||||
'vimeo id', group=(1, 2), default=(None, None))
|
||||
if vimeo_id:
|
||||
embed_url = update_url_query(
|
||||
f'https://player.vimeo.com/video/{vimeo_id}',
|
||||
{'h': vimeo_hash or []})
|
||||
if VimeoIE.suitable(embed_url):
|
||||
entry = self.url_result(
|
||||
VimeoIE._smuggle_referrer(embed_url, self._HTTP_HEADERS['referer']),
|
||||
VimeoIE, url_transparent=True)
|
||||
else:
|
||||
entry = self.url_result(smuggle_url(embed_url, self._HTTP_HEADERS))
|
||||
|
||||
# handle Vimeo embeds
|
||||
if traverse_obj(attributes, ('embed', 'provider')) == 'Vimeo':
|
||||
v_url = urllib.parse.unquote(self._html_search_regex(
|
||||
r'(https(?:%3A%2F%2F|://)player\.vimeo\.com.+app_id(?:=|%3D)+\d+)',
|
||||
traverse_obj(attributes, ('embed', 'html', {str})), 'vimeo url', fatal=False) or '')
|
||||
if url_or_none(v_url) and self._request_webpage(
|
||||
v_url, video_id, 'Checking Vimeo embed URL', headers=headers,
|
||||
fatal=False, errnote=False, expected_status=429): # 429 is TLS fingerprint rejection
|
||||
entries.append(self.url_result(
|
||||
VimeoIE._smuggle_referrer(v_url, headers['referer']),
|
||||
VimeoIE, url_transparent=True))
|
||||
|
||||
embed_url = traverse_obj(attributes, ('embed', 'url', {url_or_none}))
|
||||
if embed_url and (urlh := self._request_webpage(
|
||||
embed_url, video_id, 'Checking embed URL', headers=headers,
|
||||
fatal=False, errnote=False, expected_status=403)):
|
||||
# Vimeo's Cloudflare anti-bot protection will return HTTP status 200 for 404, so we need
|
||||
# to check for "Sorry, we couldn’t find that page" in the meta description tag
|
||||
meta_description = clean_html(self._html_search_meta(
|
||||
'description', self._webpage_read_content(urlh, embed_url, video_id, fatal=False), default=None))
|
||||
# Password-protected vids.io embeds return 403 errors w/o --video-password or session cookie
|
||||
if ((urlh.status != 403 and meta_description != 'Sorry, we couldn’t find that page')
|
||||
or VidsIoIE.suitable(embed_url)):
|
||||
entries.append(self.url_result(smuggle_url(embed_url, headers)))
|
||||
if urlh := self._request_webpage(
|
||||
embed_url, video_id, 'Checking embed URL', headers=self._HTTP_HEADERS,
|
||||
fatal=False, errnote=False, expected_status=(403, 429), # Ignore Vimeo 429's
|
||||
):
|
||||
# Password-protected vids.io embeds return 403 errors w/o --video-password or session cookie
|
||||
if VidsIoIE.suitable(embed_url) or urlh.status != 403:
|
||||
entries.append(entry)
|
||||
|
||||
post_file = traverse_obj(attributes, ('post_file', {dict}))
|
||||
if post_file:
|
||||
@@ -381,13 +454,27 @@ class PatreonIE(PatreonBaseIE):
|
||||
})
|
||||
elif name == 'video' or determine_ext(post_file.get('url')) == 'm3u8':
|
||||
formats, subtitles = self._extract_m3u8_formats_and_subtitles(
|
||||
post_file['url'], video_id, headers=headers)
|
||||
post_file['url'], video_id, headers=self._HTTP_HEADERS)
|
||||
for f in formats:
|
||||
f['http_headers'] = self._HTTP_HEADERS
|
||||
entries.append({
|
||||
'id': video_id,
|
||||
'formats': formats,
|
||||
'subtitles': subtitles,
|
||||
'http_headers': headers,
|
||||
})
|
||||
if media_id := traverse_obj(post_file, ('media_id', {int}, {str_or_none})):
|
||||
seen_media_ids.add(media_id)
|
||||
|
||||
for media_id in traverse_obj(attributes, (
|
||||
'content', {find_elements(attr='data-media-id', value=r'\d+', regex=True, html=True)},
|
||||
..., {extract_attributes}, 'data-media-id',
|
||||
)):
|
||||
# Inlined media may be duplicates of what was extracted above
|
||||
if media_id in seen_media_ids:
|
||||
continue
|
||||
if media := self._extract_from_media_api(media_id):
|
||||
entries.append(media)
|
||||
seen_media_ids.add(media_id)
|
||||
|
||||
can_view_post = traverse_obj(attributes, 'current_user_can_view')
|
||||
comments = None
|
||||
@@ -538,14 +625,13 @@ class PatreonCampaignIE(PatreonBaseIE):
|
||||
'info_dict': {
|
||||
'id': '9631148',
|
||||
'title': 'Anything Else?',
|
||||
'description': 'md5:2ee1db4aed2f9460c2b295825a24aa08',
|
||||
'description': 'md5:b2f20eec4cb5520d9a4be4971f28add5',
|
||||
'uploader': 'dan ',
|
||||
'uploader_id': '13852412',
|
||||
'uploader_url': 'https://www.patreon.com/anythingelse',
|
||||
'channel': 'Anything Else?',
|
||||
'channel_id': '9631148',
|
||||
'channel_url': 'https://www.patreon.com/anythingelse',
|
||||
'channel_follower_count': int,
|
||||
'age_limit': 0,
|
||||
'thumbnail': r're:https?://.+/.+',
|
||||
},
|
||||
@@ -590,16 +676,15 @@ class PatreonCampaignIE(PatreonBaseIE):
|
||||
break
|
||||
|
||||
def _real_extract(self, url):
|
||||
|
||||
campaign_id, vanity = self._match_valid_url(url).group('campaign_id', 'vanity')
|
||||
if campaign_id is None:
|
||||
webpage = self._download_webpage(url, vanity, headers={'User-Agent': self.patreon_user_agent})
|
||||
campaign_id = traverse_obj(self._search_nextjs_data(webpage, vanity, default=None), (
|
||||
'props', 'pageProps', 'bootstrapEnvelope', 'pageBootstrap', 'campaign', 'data', 'id', {str}))
|
||||
if not campaign_id:
|
||||
campaign_id = traverse_obj(self._search_nextjs_v13_data(webpage, vanity), (
|
||||
((..., 'value', 'campaign', 'data'), lambda _, v: v['type'] == 'campaign'),
|
||||
'id', {str}, any, {require('campaign ID')}))
|
||||
results = self._call_api('search', vanity, query={
|
||||
'q': vanity,
|
||||
'page[size]': '5',
|
||||
})['data']
|
||||
campaign_id = traverse_obj(results, (
|
||||
lambda _, v: v['type'] == 'campaign-document' and v['attributes']['url'].lower().endswith(f'/{vanity.lower()}'),
|
||||
'id', {trim_str(start='campaign_')}, filter, any, {require('campaign ID')}))
|
||||
|
||||
params = {
|
||||
'json-api-use-default-includes': 'false',
|
||||
|
||||
Reference in New Issue
Block a user