Skip to content

Commit

Permalink
release: v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
newt-sc committed May 24, 2020
1 parent 23fb7d1 commit 68085eb
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* [v1.2.0](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-1.2.0):
* Use internal ZipFile for extraction with a fallback to vfs.libarchive

* [v1.1.0](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-1.1.0):
* Improve imdb id scraping

Expand Down
2 changes: 2 additions & 0 deletions a4kSubtitles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
import time
import hashlib
import re
import zipfile
from datetime import datetime
from base64 import b64encode
from xml.etree import ElementTree
from io import BytesIO

from .lib import (
cache,
Expand Down
27 changes: 21 additions & 6 deletions a4kSubtitles/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ def __extract_zip(core, archivepath, filename, episodeid):
sub_exts = ['.srt', '.sub']
sub_exts_secondary = ['.smi', '.ssa', '.aqt', '.jss', '.ass', '.rt', '.txt']

archivepath_ = core.utils.quote_plus(archivepath)
subfile = core.utils.find_file_in_archive(core, archivepath_, sub_exts, episodeid)
try:
using_libvfs = False
with open(archivepath, 'rb') as f:
zipfile = core.zipfile.ZipFile(core.BytesIO(f.read()))
namelist = core.utils.get_zipfile_namelist(zipfile)
except:
using_libvfs = True
archivepath_ = core.utils.quote_plus(archivepath)
(dirs, files) = core.kodi.xbmcvfs.listdir('archive://%s' % archivepath_)
namelist = [file.decode(core.utils.default_encoding) if core.utils.py2 else file for file in files]

subfile = core.utils.find_file_in_archive(core, namelist, sub_exts, episodeid)
if not subfile:
subfile = core.utils.find_file_in_archive(core, archivepath_, sub_exts_secondary, episodeid)
subfile = core.utils.find_file_in_archive(core, namelist, sub_exts_secondary, episodeid)

dest = core.os.path.join(core.utils.temp_dir, filename)
if not subfile:
Expand All @@ -41,12 +51,17 @@ def __extract_zip(core, archivepath, filename, episodeid):
core.os.rename(archivepath, dest)
return dest

src = 'archive://' + archivepath_ + '/' + subfile
core.kodi.xbmcvfs.copy(src, dest)
if not using_libvfs:
src = core.utils.extract_zipfile_member(zipfile, subfile, core.utils.temp_dir)
core.os.rename(src, dest)
else:
src = 'archive://' + archivepath_ + '/' + subfile
core.kodi.xbmcvfs.copy(src, dest)

return dest

def __insert_lang_code_in_filename(core, filename, lang):
filename_chunks = filename.split('.')
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)
Expand Down
40 changes: 32 additions & 8 deletions a4kSubtitles/lib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from urllib.parse import quote_plus, unquote, parse_qsl
from io import StringIO
import queue
unicode = lambda v, e: v
unicode = lambda v: v

__url_regex = r'[a-z0-9][a-z0-9-]{0,5}[a-z0-9]\.[a-z0-9]{2,20}\.[a-z]{2,5}'
__credit_part_regex = r'(sync|synced|fix|fixed|corrected|corrections)'
Expand All @@ -29,6 +29,9 @@
cp1251_garbled = u'аеио'.encode('cp1251').decode('raw_unicode_escape')
koi8r_garbled = u'аеио'.encode('koi8-r').decode('raw_unicode_escape')

zip_utf8_flag = 0x800
py3_zip_missing_utf8_flag_fallback_encoding = 'cp437'

py2 = sys.version_info[0] == 2
py3 = not py2

Expand Down Expand Up @@ -134,15 +137,10 @@ def get_json(path, filename):
with open(json_path) as json_result:
return json.load(json_result)

def find_file_in_archive(core, archivepath, exts, part_of_filename=''):
(dirs, files) = core.kodi.xbmcvfs.listdir('archive://%s' % archivepath)

def find_file_in_archive(core, namelist, exts, part_of_filename=''):
first_ext_match = None
exact_file = None
for file in files:
if core.utils.py2:
file = file.decode('utf8')

for file in namelist:
file_lower = file.lower()
if any(file_lower.endswith(ext) for ext in exts):
if not first_ext_match:
Expand All @@ -152,3 +150,29 @@ def find_file_in_archive(core, archivepath, exts, part_of_filename=''):
break

return exact_file if exact_file is not None else first_ext_match

def get_zipfile_namelist(zipfile):
infolist = zipfile.infolist()
namelist = []

if py2:
for info in infolist:
namelist.append(info.filename.decode(default_encoding))
else:
for info in infolist:
filename = info.filename
if not info.flag_bits & zip_utf8_flag:
filename = info.filename.encode(py3_zip_missing_utf8_flag_fallback_encoding).decode(default_encoding)
namelist.append(filename)

return namelist

def extract_zipfile_member(zipfile, filename, dest):
if py2:
return zipfile.extract(filename.encode(default_encoding), dest)
else:
try:
return zipfile.extract(filename, dest)
except:
filename = filename.encode(default_encoding).decode(py3_zip_missing_utf8_flag_fallback_encoding)
return zipfile.extract(filename, dest)
5 changes: 4 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="1.1.0"
version="1.2.0"
provider-name="Unknown">
<requires>
<import addon="xbmc.python" version="2.25.0"/>
Expand All @@ -27,6 +27,9 @@ Supports: OpenSubtitles, BSPlayer, Podnadpisi.NET, SubDB, Subscene, Addic7ed
<screenshot>screenshot-03.png</screenshot>
</assets>
<news>
[v1.2.0]:
* Use internal ZipFile for extraction with a fallback to vfs.libarchive

[v1.1.0]:
* Improve imdb id scraping

Expand Down
5 changes: 4 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="1.1.0"
version="1.2.0"
provider-name="Unknown">
<requires>
<import addon="xbmc.python" version="2.25.0"/>
Expand All @@ -30,6 +30,9 @@ Supports: OpenSubtitles, BSPlayer, Podnadpisi.NET, SubDB, Subscene, Addic7ed
<screenshot>screenshot-03.png</screenshot>
</assets>
<news>
[v1.2.0]:
* Use internal ZipFile for extraction with a fallback to vfs.libarchive

[v1.1.0]:
* Improve imdb id scraping

Expand Down
2 changes: 1 addition & 1 deletion packages/addons.xml.crc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cf0e071b1e659fd1525d23b9445319824f58c943
d114c77b06cd3bc4c8e557687867387d148363d4
43 changes: 42 additions & 1 deletion tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@
"subdb_hash": "2aec1b70afe702e67ab39a0af776ba5a",
}

__tvshow_unicode_video_meta = {
"year": "2019",
"title": "In the Dark Night of the Soul It's Always 3:30 in the Morning",
"tvshow": "The Morning Show",
"imdb_id": "tt7203552",
"season": "1",
"episode": "1",
"filename": "The Morning Show (2019) S01E01 In the Dark Night of the Soul it's Always 330 in the Morning.mkv",
"filesize": 2135679767,
"filehash": "631fb55f3db5709c",
"subdb_hash": "b35c5f8e4afffd8ee09442e98f943410",
}

def __remove_meta_cache(a4ksubtitles_api):
try:
os.remove(a4ksubtitles_api.core.cache.__meta_cache_filepath)
Expand Down Expand Up @@ -79,7 +92,7 @@ def __search(a4ksubtitles_api, settings={}, video_meta={}):
}

search.settings = {
'general.timeout': '20',
'general.timeout': '30',
'general.results_limit': '20',
'opensubtitles.enabled': 'false',
'opensubtitles.username': '',
Expand Down Expand Up @@ -109,6 +122,11 @@ def __search_tvshow(a4ksubtitles_api, settings={}, video_meta={}):
tvshow_video_meta.update(video_meta)
return __search(a4ksubtitles_api, settings, tvshow_video_meta)

def __search_unicode_tvshow(a4ksubtitles_api, settings={}, video_meta={}):
tvshow_video_meta = __tvshow_unicode_video_meta.copy()
tvshow_video_meta.update(video_meta)
return __search(a4ksubtitles_api, settings, tvshow_video_meta)

def test_api():
def get_error_msg(e):
return str(e.value).replace('\'', '')
Expand Down Expand Up @@ -221,6 +239,29 @@ def test_opensubtitles_tvshow():

assert filepath != ''

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

# search
settings = {
'opensubtitles.enabled': 'true',
}
search = __search_unicode_tvshow(a4ksubtitles_api, settings)

# download
item = search.results[0]

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

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

assert filepath != ''

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

0 comments on commit 68085eb

Please sign in to comment.