From 3da3733d0623581f4da8aebf6d1dba1535f8ffcb Mon Sep 17 00:00:00 2001 From: c-basalt <117849907+c-basalt@users.noreply.github.com> Date: Mon, 30 Sep 2024 02:41:09 -0400 Subject: [PATCH] use built-in geo handler --- yt_dlp/extractor/neteasemusic.py | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/yt_dlp/extractor/neteasemusic.py b/yt_dlp/extractor/neteasemusic.py index 4865331ba6..c8aaeccb84 100644 --- a/yt_dlp/extractor/neteasemusic.py +++ b/yt_dlp/extractor/neteasemusic.py @@ -34,7 +34,6 @@ class NetEaseMusicBaseIE(InfoExtractor): 'sky', # SVIP tier; 沉浸环绕声 (Surround Audio); flac ) _API_BASE = 'http://music.163.com/api/' - _GEO_BYPASS = False @staticmethod def _kilo_or_none(value): @@ -68,6 +67,8 @@ def _download_eapi_json(self, path, video_id, query_body, headers={}, **kwargs): 'MUSIC_U': ('MUSIC_U', {lambda i: i.value}), }), } + if self._x_forwarded_for_ip: + headers.setdefault('X-Real-IP', self._x_forwarded_for_ip) return self._download_json( urljoin('https://interface3.music.163.com/', f'/eapi{path}'), video_id, data=self._create_eapi_cipher(f'/api{path}', query_body, cookies), headers={ @@ -76,23 +77,20 @@ def _download_eapi_json(self, path, video_id, query_body, headers={}, **kwargs): **headers, }, **kwargs) - def _call_player_api(self, song_id, level, headers={}): + def _call_player_api(self, song_id, level): return self._download_eapi_json( '/song/enhance/player/url/v1', song_id, {'ids': f'[{song_id}]', 'level': level, 'encodeType': 'flac'}, - headers=headers, note=f'Downloading song URL info: level {level}') + note=f'Downloading song URL info: level {level}') - def _extract_formats(self, info, try_geo_bypass=False): + def _extract_formats(self, info): formats = [] song_id = info['id'] - headers = {'X-Real-IP': '118.88.88.88'} if try_geo_bypass else {} for level in self._LEVELS: song = traverse_obj( - self._call_player_api(song_id, level, headers=headers), ('data', lambda _, v: url_or_none(v['url']), any)) - if not song: # Media is not available due to removal or geo-restriction - if not try_geo_bypass: - return self._extract_formats(info, try_geo_bypass=True) - break + self._call_player_api(song_id, level), ('data', lambda _, v: url_or_none(v['url']), any)) + if not song: + break # Media is not available due to removal or geo-restriction actual_level = song.get('level') if actual_level and actual_level != level: if level in ('lossless', 'jymaster'):