add location support (closes #377)

This commit is contained in:
sigma67 2023-06-15 20:07:42 +02:00
commit 2c99445d1b
No known key found for this signature in database
GPG key ID: 1C314227DF0103D0
3 changed files with 269 additions and 6 deletions

View file

@ -92,3 +92,243 @@ This is the case if a ytmusicapi method supports the ``limit`` parameter. The de
indicates the server-side pagination increment. ytmusicapi will keep fetching continuations from the server until it has
reached at least the ``limit`` parameter, and return all of these results.
Which values can I use for locations?
*************************************
Pick a value from the list below for your desired location and pass it using the `location` parameter.
.. raw:: html
<details>
<summary><a>Supported locations</a></summary>
.. container::
.. list-table::
* - Location
- Value
* - Algeria
- DZ
* - Argentina
- AR
* - Australia
- AU
* - Austria
- AT
* - Azerbaijan
- AZ
* - Bahrain
- BH
* - Bangladesh
- BD
* - Belarus
- BY
* - Belgium
- BE
* - Bolivia
- BO
* - Bosnia and Herzegovina
- BA
* - Brazil
- BR
* - Bulgaria
- BG
* - Cambodia
- KH
* - Canada
- CA
* - Chile
- CL
* - Colombia
- CO
* - Costa Rica
- CR
* - Croatia
- HR
* - Cyprus
- CY
* - Czechia
- CZ
* - Denmark
- DK
* - Dominican Republic
- DO
* - Ecuador
- EC
* - Egypt
- EG
* - El Salvador
- SV
* - Estonia
- EE
* - Finland
- FI
* - France
- FR
* - Georgia
- GE
* - Germany
- DE
* - Ghana
- GH
* - Greece
- GR
* - Guatemala
- GT
* - Honduras
- HN
* - Hong Kong
- HK
* - Hungary
- HU
* - Iceland
- IS
* - India
- IN
* - Indonesia
- ID
* - Iraq
- IQ
* - Ireland
- IE
* - Israel
- IL
* - Italy
- IT
* - Jamaica
- JM
* - Japan
- JP
* - Jordan
- JO
* - Kazakhstan
- KZ
* - Kenya
- KE
* - Kuwait
- KW
* - Laos
- LA
* - Latvia
- LV
* - Lebanon
- LB
* - Libya
- LY
* - Liechtenstein
- LI
* - Lithuania
- LT
* - Luxembourg
- LU
* - Malaysia
- MY
* - Malta
- MT
* - Mexico
- MX
* - Montenegro
- ME
* - Morocco
- MA
* - Nepal
- NP
* - Netherlands
- NL
* - New Zealand
- NZ
* - Nicaragua
- NI
* - Nigeria
- NG
* - North Macedonia
- MK
* - Norway
- NO
* - Oman
- OM
* - Pakistan
- PK
* - Panama
- PA
* - Papua New Guinea
- PG
* - Paraguay
- PY
* - Peru
- PE
* - Philippines
- PH
* - Poland
- PL
* - Portugal
- PT
* - Puerto Rico
- PR
* - Qatar
- QA
* - Romania
- RO
* - Russia
- RU
* - Saudi Arabia
- SA
* - Senegal
- SN
* - Serbia
- RS
* - Singapore
- SG
* - Slovakia
- SK
* - Slovenia
- SI
* - South Africa
- ZA
* - South Korea
- KR
* - Spain
- ES
* - Sri Lanka
- LK
* - Sweden
- SE
* - Switzerland
- CH
* - Taiwan
- TW
* - Tanzania
- TZ
* - Thailand
- TH
* - Tunisia
- TN
* - Turkey
- TR
* - Uganda
- UG
* - Ukraine
- UA
* - United Arab Emirates
- AE
* - United Kingdom
- GB
* - United States
- US
* - Uruguay
- UY
* - Venezuela
- VE
* - Vietnam
- VN
* - Yemen
- YE
* - Zimbabwe
- ZW
.. raw:: html
</details>

View file

@ -3,6 +3,19 @@ YTM_BASE_API = YTM_DOMAIN + '/youtubei/v1/'
YTM_PARAMS = '?alt=json'
YTM_PARAMS_KEY = "&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30"
USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0'
SUPPORTED_LANGUAGES = {
'ar', 'de', 'en', 'es', 'fr', 'hi', 'it', 'ja', 'ko', 'nl', 'pt', 'ru', 'tr', 'ur', 'zh_CN',
'zh_TW'
}
SUPPORTED_LOCATIONS = {
'AE', 'AR', 'AT', 'AU', 'AZ', 'BA', 'BD', 'BE', 'BG', 'BH', 'BO', 'BR', 'BY', 'CA', 'CH', 'CL',
'CO', 'CR', 'CY', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EC', 'EE', 'EG', 'ES', 'FI', 'FR', 'GB', 'GE',
'GH', 'GR', 'GT', 'HK', 'HN', 'HR', 'HU', 'ID', 'IE', 'IL', 'IN', 'IQ', 'IS', 'IT', 'JM', 'JO',
'JP', 'KE', 'KH', 'KR', 'KW', 'KZ', 'LA', 'LB', 'LI', 'LK', 'LT', 'LU', 'LV', 'LY', 'MA', 'ME',
'MK', 'MT', 'MX', 'MY', 'NG', 'NI', 'NL', 'NO', 'NP', 'NZ', 'OM', 'PA', 'PE', 'PG', 'PH', 'PK',
'PL', 'PR', 'PT', 'PY', 'QA', 'RO', 'RS', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'SN', 'SV', 'TH',
'TN', 'TR', 'TW', 'TZ', 'UA', 'UG', 'US', 'UY', 'VE', 'VN', 'YE', 'ZA', 'ZW'
}
OAUTH_CLIENT_ID = "861556708454-d6dlm3lh05idd8npek18k6be8ba3oc68.apps.googleusercontent.com"
OAUTH_CLIENT_SECRET = "SboVhoG9s0rNafixCSGGKXAT"
OAUTH_SCOPE = "https://www.googleapis.com/auth/youtube"

View file

@ -30,7 +30,8 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
user: str = None,
requests_session=True,
proxies: dict = None,
language: str = 'en'):
language: str = 'en',
location: str = ''):
"""
Create a new instance to interact with YouTube Music.
@ -62,6 +63,9 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
:param language: Optional. Can be used to change the language of returned data.
English will be used by default. Available languages can be checked in
the ytmusicapi/locales directory.
:param location: Optional. Can be used to change the location of the user.
No location will be set by default. This means it is determined by the server.
Available languages can be checked in the FAQ.
"""
self.auth = auth
@ -84,18 +88,24 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
# prepare context
self.context = initialize_context()
self.context['context']['client']['hl'] = language
locale_dir = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'locales'
supported_languages = [f for f in next(os.walk(locale_dir))[1]]
if language not in supported_languages:
if location:
if location not in SUPPORTED_LOCATIONS:
raise Exception("Location not supported. Check the FAQ for supported locations.")
self.context['context']['client']['gl'] = location
if language not in SUPPORTED_LANGUAGES:
raise Exception("Language not supported. Supported languages are "
+ (', '.join(supported_languages)) + ".")
+ (', '.join(SUPPORTED_LANGUAGES)) + ".")
self.context['context']['client']['hl'] = language
self.language = language
try:
locale.setlocale(locale.LC_ALL, self.language)
except locale.Error:
with suppress(locale.Error):
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
locale_dir = os.path.abspath(os.path.dirname(__file__)) + os.sep + 'locales'
self.lang = gettext.translation('base', localedir=locale_dir, languages=[language])
self.parser = Parser(self.lang)