Skip to content

Commit

Permalink
[ie/bilibili] Fix --geo-verification-proxy support (#9817)
Browse files Browse the repository at this point in the history
Closes #9797
Authored by: fireattack
  • Loading branch information
fireattack committed May 8, 2024
1 parent 06d52c8 commit 2338827
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions yt_dlp/extractor/bilibili.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ def extract_formats(self, play_info):

return formats

def _download_playinfo(self, video_id, cid):
def _download_playinfo(self, video_id, cid, headers=None):
return self._download_json(
'https://api.bilibili.com/x/player/playurl', video_id,
query={'bvid': video_id, 'cid': cid, 'fnval': 4048},
note=f'Downloading video formats for cid {cid}')['data']
note=f'Downloading video formats for cid {cid}', headers=headers)['data']

def json2srt(self, json_data):
srt_data = ''
Expand Down Expand Up @@ -493,7 +493,8 @@ class BiliBiliIE(BilibiliBaseIE):

def _real_extract(self, url):
video_id = self._match_id(url)
webpage, urlh = self._download_webpage_handle(url, video_id)
headers = self.geo_verification_headers()
webpage, urlh = self._download_webpage_handle(url, video_id, headers=headers)
if not self._match_valid_url(urlh.url):
return self.url_result(urlh.url)

Expand Down Expand Up @@ -531,7 +532,7 @@ def _real_extract(self, url):
self._download_json(
'https://api.bilibili.com/x/player/pagelist', video_id,
fatal=False, query={'bvid': video_id, 'jsonp': 'jsonp'},
note='Extracting videos in anthology'),
note='Extracting videos in anthology', headers=headers),
'data', expected_type=list) or []
is_anthology = len(page_list_json) > 1

Expand All @@ -552,7 +553,7 @@ def _real_extract(self, url):

festival_info = {}
if is_festival:
play_info = self._download_playinfo(video_id, cid)
play_info = self._download_playinfo(video_id, cid, headers=headers)

festival_info = traverse_obj(initial_state, {
'uploader': ('videoInfo', 'upName'),
Expand Down Expand Up @@ -666,14 +667,15 @@ class BiliBiliBangumiIE(BilibiliBaseIE):

def _real_extract(self, url):
episode_id = self._match_id(url)
webpage = self._download_webpage(url, episode_id)
headers = self.geo_verification_headers()
webpage = self._download_webpage(url, episode_id, headers=headers)

if '您所在的地区无法观看本片' in webpage:
raise GeoRestrictedError('This video is restricted')
elif '正在观看预览,大会员免费看全片' in webpage:
self.raise_login_required('This video is for premium members only')

headers = {'Referer': url, **self.geo_verification_headers()}
headers['Referer'] = url
play_info = self._download_json(
'https://api.bilibili.com/pgc/player/web/v2/playurl', episode_id,
'Extracting episode', query={'fnval': '4048', 'ep_id': episode_id},
Expand Down Expand Up @@ -724,7 +726,7 @@ def _real_extract(self, url):
'duration': float_or_none(play_info.get('timelength'), scale=1000),
'subtitles': self.extract_subtitles(episode_id, episode_info.get('cid'), aid=aid),
'__post_extractor': self.extract_comments(aid),
'http_headers': headers,
'http_headers': {'Referer': url},
}


Expand Down

0 comments on commit 2338827

Please sign in to comment.