Fix unsupported locale errors on Linux

This commit is contained in:
sigma67 2020-07-09 19:11:47 +02:00
commit 81df1505fd
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
2 changed files with 18 additions and 15 deletions

View file

@ -50,8 +50,7 @@ def get_authorization(auth):
return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest() return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest()
def to_int(string, language): def to_int(string):
locale.setlocale(locale.LC_ALL, language)
number = string.split(' ')[0] number = string.split(' ')[0]
return locale.atoi(number) return locale.atoi(number)

View file

@ -5,6 +5,7 @@ import gettext
import pkg_resources import pkg_resources
import ntpath import ntpath
import os import os
from contextlib import suppress
from typing import List, Dict, Union, Tuple from typing import List, Dict, Union, Tuple
from ytmusicapi.helpers import * from ytmusicapi.helpers import *
from ytmusicapi.parsers import * from ytmusicapi.parsers import *
@ -69,15 +70,20 @@ class YTMusic:
supported_languages = [ supported_languages = [
f for f in pkg_resources.resource_listdir('ytmusicapi', 'locales') f for f in pkg_resources.resource_listdir('ytmusicapi', 'locales')
] ]
if language in supported_languages: if language not in supported_languages:
self.language = language raise Exception("Language not supported. Supported languages are "
self.lang = gettext.translation('base', ', '.join(supported_languages))
localedir=pkg_resources.resource_filename(
'ytmusicapi', 'locales'), try:
languages=[language]) locale.setlocale(locale.LC_ALL, language)
self.parser = Parser(self.lang) except locale.Error:
else: with suppress(locale.Error):
raise Exception("Language not supported. Supported languages are ") locale.setlocale(locale.LC_ALL, 'en_US.UTF8')
self.lang = gettext.translation('base',
localedir=pkg_resources.resource_filename(
'ytmusicapi', 'locales'),
languages=[language])
self.parser = Parser(self.lang)
if user: if user:
self.context['context']['user']['onBehalfOfUser'] = user self.context['context']['user']['onBehalfOfUser'] = user
@ -822,8 +828,7 @@ class YTMusic:
if len(header['secondSubtitle']['runs']) > 1: if len(header['secondSubtitle']['runs']) > 1:
song_count = to_int( song_count = to_int(
unicodedata.normalize("NFKD", header['secondSubtitle']['runs'][0]['text']), unicodedata.normalize("NFKD", header['secondSubtitle']['runs'][0]['text']))
self.language)
playlist['duration'] = header['secondSubtitle']['runs'][2]['text'] playlist['duration'] = header['secondSubtitle']['runs'][2]['text']
else: else:
playlist['duration'] = header['secondSubtitle']['runs'][0]['text'] playlist['duration'] = header['secondSubtitle']['runs'][0]['text']
@ -1167,8 +1172,7 @@ class YTMusic:
album['year'] = nav(header, SUBTITLE3) album['year'] = nav(header, SUBTITLE3)
if len(header['secondSubtitle']['runs']) > 1: if len(header['secondSubtitle']['runs']) > 1:
album['trackCount'] = to_int(header['secondSubtitle']['runs'][0]['text'], album['trackCount'] = to_int(header['secondSubtitle']['runs'][0]['text'])
self.language)
album['duration'] = header['secondSubtitle']['runs'][2]['text'] album['duration'] = header['secondSubtitle']['runs'][2]['text']
else: else:
album['duration'] = header['secondSubtitle']['runs'][0]['text'] album['duration'] = header['secondSubtitle']['runs'][0]['text']