Skip to content

Commit

Permalink
Merge 3a49c70 into d6fc173
Browse files Browse the repository at this point in the history
  • Loading branch information
newt-sc committed Mar 21, 2022
2 parents d6fc173 + 3a49c70 commit 9798a19
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[flake8]
max-complexity = 10
max-line-length=140
max-line-length=200
statistics = True
show-source = True

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* [v3.1.0](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-3.1.0):
* Force proper embedded subs selection based on preferred language overriding KODI
* Subs encoding fallback to predefined list in case auto detection fails

* [v3.0.0](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-3.0.0):
* Use cf bypass for Subscene
* Auto-detect subs encoding and convert to utf-8
Expand Down
22 changes: 14 additions & 8 deletions a4kSubtitles/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,26 @@ def __extract_zip(core, archivepath, filename, episodeid):

return dest

def __insert_lang_code_in_filename(core, filename, lang):
def __insert_lang_code_in_filename(core, filename, lang_code):
filename_chunks = core.utils.strip_non_ascii_and_unprintable(filename).split('.')
lang_code = core.kodi.xbmc.convertLanguage(lang, core.kodi.xbmc.ISO_639_2)
filename_chunks.insert(-1, lang_code)
return '.'.join(filename_chunks)

def __postprocess(core, filepath):
def __postprocess(core, filepath, lang_code):
try:
with open(filepath, 'rb') as f:
text_bytes = f.read()

encoding = ''
if core.utils.py3:
encoding = core.utils.chardet.detect(text_bytes)['encoding']
else:
encoding = core.utils.default_encoding
detection = core.utils.chardet.detect(text_bytes)
detected_lang_code = core.kodi.xbmc.convertLanguage(detection['language'], core.kodi.xbmc.ISO_639_2)
if detection['confidence'] == 1.0 or detected_lang_code == lang_code:
encoding = detection['encoding']

if not encoding:
encoding = core.utils.code_pages.get(lang_code, core.utils.default_encoding)

text = text_bytes.decode(encoding)

try:
Expand Down Expand Up @@ -110,7 +115,8 @@ def download(core, params):
core.kodi.xbmcvfs.mkdirs(core.utils.temp_dir)

actions_args = params['action_args']
filename = __insert_lang_code_in_filename(core, actions_args['filename'], actions_args['lang'])
lang_code = core.kodi.xbmc.convertLanguage(actions_args['lang'], core.kodi.xbmc.ISO_639_2)
filename = __insert_lang_code_in_filename(core, actions_args['filename'], lang_code)
archivepath = core.os.path.join(core.utils.temp_dir, 'sub.zip')

service_name = params['service_name']
Expand All @@ -128,7 +134,7 @@ def download(core, params):
episodeid = actions_args.get('episodeid', '')
filepath = __extract_zip(core, archivepath, filename, episodeid)

__postprocess(core, filepath)
__postprocess(core, filepath, lang_code)

if core.api_mode_enabled:
return filepath
Expand Down
2 changes: 2 additions & 0 deletions a4kSubtitles/lib/kodi_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@

__player = lambda: None
__player.getPlayingFile = lambda: ''
__player.getAvailableSubtitleStreams = lambda: []
__player.setSubtitles = lambda s: None
__player.setSubtitleStream = lambda i: None
xbmc.Player = lambda: __player

__monitor = lambda: None
Expand Down
1 change: 1 addition & 0 deletions a4kSubtitles/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
base_encoding = 'raw_unicode_escape'
cp1251_garbled = u'аеио'.encode('cp1251').decode('raw_unicode_escape')
koi8r_garbled = u'аеио'.encode('koi8-r').decode('raw_unicode_escape')
code_pages = {'ara': 'cp1256', 'ar': 'cp1256', 'ell': 'cp1253', 'el': 'cp1253', 'heb': 'cp1255', 'he': 'cp1255', 'tur': 'cp1254', 'tr': 'cp1254', 'rus': 'cp1251', 'ru': 'cp1251', 'bg': 'cp1251'}

zip_utf8_flag = 0x800
py3_zip_missing_utf8_flag_fallback_encoding = 'cp437'
Expand Down
12 changes: 9 additions & 3 deletions a4kSubtitles/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ def start(api):
continue

has_done_subs_check = True
has_subtitles = False

has_subtitles_enabled = core.kodi.xbmc.getCondVisibility('VideoPlayer.SubtitlesEnabled')
has_subtitles = core.kodi.xbmc.getCondVisibility('VideoPlayer.HasSubtitles') and has_subtitles_enabled
preferredlang = core.kodi.get_kodi_setting('locale.subtitlelanguage')

try:
preferredlang_code = core.kodi.xbmc.convertLanguage(preferredlang, core.kodi.xbmc.ISO_639_2)
sub_lang_codes = [core.kodi.xbmc.convertLanguage(s, core.kodi.xbmc.ISO_639_2) for s in core.kodi.xbmc.Player().getAvailableSubtitleStreams()]
core.kodi.xbmc.Player().setSubtitleStream(sub_lang_codes.index(preferredlang_code))
has_subtitles = True
except: pass

if has_subtitles:
continue
Expand All @@ -35,7 +42,6 @@ def start(api):
continue

languages = core.kodi.get_kodi_setting('subtitles.languages')
preferredlang = core.kodi.get_kodi_setting('locale.subtitlelanguage')
params = {
'action': 'search',
'languages': ','.join(languages),
Expand Down
6 changes: 5 additions & 1 deletion addon.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="service.subtitles.a4ksubtitles"
name="a4kSubtitles"
version="3.0.0"
version="3.1.0"
provider-name="Unknown">
<requires>
<import addon="script.module.requests"/>
Expand All @@ -27,6 +27,10 @@ Supports: OpenSubtitles, BSPlayer, Podnadpisi.NET, Subscene, Addic7ed
<screenshot>screenshot-03.png</screenshot>
</assets>
<news>
[v3.1.0]:
* Force proper embedded subs selection based on preferred language overriding KODI
* Subs encoding fallback to predefined list in case auto detection fails

[v3.0.0]:
* Use cf bypass for Subscene
* Auto-detect subs encoding and convert to utf-8
Expand Down
6 changes: 5 additions & 1 deletion packages/addons.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<addons>
<addon id="service.subtitles.a4ksubtitles"
name="a4kSubtitles"
version="3.0.0"
version="3.1.0"
provider-name="Unknown">
<requires>
<import addon="script.module.requests"/>
Expand All @@ -30,6 +30,10 @@ Supports: OpenSubtitles, BSPlayer, Podnadpisi.NET, Subscene, Addic7ed
<screenshot>screenshot-03.png</screenshot>
</assets>
<news>
[v3.1.0]:
* Force proper embedded subs selection based on preferred language overriding KODI
* Subs encoding fallback to predefined list in case auto detection fails

[v3.0.0]:
* Use cf bypass for Subscene
* Auto-detect subs encoding and convert to utf-8
Expand Down
2 changes: 1 addition & 1 deletion packages/addons.xml.crc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
764ddf5590e9ac09114e509ea2983e7bc1a8adb1
fb5e52c161a624e3a552853ba87732e2a74f1939
46 changes: 0 additions & 46 deletions tests/test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,6 @@ def test_service_start_when_enabled():

assert get_cond_visibility_spy.call_count > 0

def test_service_when_video_has_subtitles():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})

restore = __mock(a4ksubtitles_api, {
'general.auto_search': 'true',
})
restore_get_cond_visibility = __mock_get_cond_visibility(a4ksubtitles_api, {
'VideoPlayer.Content(movies)': True,
'Player.HasDuration': True,
'VideoPlayer.HasSubtitles': True,
'VideoPlayer.SubtitlesEnabled': True,
})

executebuiltin_spy = utils.spy_fn(a4ksubtitles_api.core.kodi.xbmc, 'executebuiltin')

service.start(a4ksubtitles_api)

restore()
restore_get_cond_visibility()
executebuiltin_spy.restore()

assert executebuiltin_spy.call_count == 0

def test_service_when_video_does_not_have_subtitles():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})

Expand Down Expand Up @@ -148,29 +125,6 @@ def test_service_when_video_has_disabled_subtitles():

assert executebuiltin_spy.call_count == 1

def test_service_when_subs_check_done():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})

restore = __mock(a4ksubtitles_api, {
'general.auto_search': 'true',
})
restore_get_cond_visibility = __mock_get_cond_visibility(a4ksubtitles_api, {
'VideoPlayer.Content(movies)': True,
'Player.HasDuration': True,
'VideoPlayer.HasSubtitles': True,
'VideoPlayer.SubtitlesEnabled': True,
})

executebuiltin_spy = utils.spy_fn(a4ksubtitles_api.core.kodi.xbmc, 'executebuiltin')

service.start(a4ksubtitles_api)

restore()
restore_get_cond_visibility()
executebuiltin_spy.restore()

assert executebuiltin_spy.call_count == 0

def test_service_when_does_not_have_video_duration():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})

Expand Down
34 changes: 32 additions & 2 deletions tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ def __search(a4ksubtitles_api, settings={}, video_meta={}, languages='English'):

return search

def __search_movie(a4ksubtitles_api, settings={}, video_meta={}):
def __search_movie(a4ksubtitles_api, settings={}, video_meta={}, languages='English'):
movie_video_meta = __movie_video_meta.copy()
movie_video_meta.update(video_meta)
return __search(a4ksubtitles_api, settings, movie_video_meta)
return __search(a4ksubtitles_api, settings, movie_video_meta, languages)

def __search_tvshow(a4ksubtitles_api, settings={}, video_meta={}, languages='English'):
tvshow_video_meta = __tvshow_video_meta.copy()
Expand Down Expand Up @@ -628,6 +628,36 @@ def test_subscene_tvshow_persian():

assert filepath != ''

def test_subscene_arabic():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})
__remove_all_cache(a4ksubtitles_api)

# search
settings = {
'subscene.enabled': 'true',
}

if os.getenv('CI', None) is not None:
time.sleep(4)

search = __search_movie(a4ksubtitles_api, settings, {}, 'Arabic')

# download
item = search.results[0]

params = {
'action': 'download',
'service_name': 'subscene',
'action_args': item['action_args']
}

if os.getenv('CI', None) is not None:
time.sleep(4)

filepath = a4ksubtitles_api.download(params, search.settings)

assert filepath != ''

def test_addic7ed_tvshow():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})
__remove_all_cache(a4ksubtitles_api)
Expand Down

0 comments on commit 9798a19

Please sign in to comment.