Skip to content

Commit

Permalink
release: v0.0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
newt-sc committed Apr 25, 2020
1 parent 9a9d866 commit 6bc7166
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* [v0.0.14](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-0.0.14):
* Fix API and normal usage at the same time

* [v0.0.13](https://github.com/newt-sc/a4kSubtitles/releases/tag/service.subtitles.a4ksubtitles%2Fservice.subtitles.a4ksubtitles-0.0.13):
* Exclude development files from release

Expand Down
8 changes: 3 additions & 5 deletions a4kSubtitles/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import json
import importlib

api_mode_env_name = 'A4KSUBTITLES_API_MODE'

class A4kSubtitlesApi(object):
def __init__(self, mocks=None):
if mocks is None:
Expand All @@ -19,13 +21,9 @@ def __init__(self, mocks=None):
}

api_mode.update(mocks)
os.environ['A4KSUBTITLES_API_MODE'] = json.dumps(api_mode)

os.environ[api_mode_env_name] = json.dumps(api_mode)
self.core = importlib.import_module('a4kSubtitles.core')

def __del__(self):
os.environ.pop('A4KSUBTITLES_API_MODE')

def __mock_video_meta(self, meta):
def get_info_label(label):
if label == 'VideoPlayer.Year':
Expand Down
10 changes: 5 additions & 5 deletions a4kSubtitles/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
from .download import download

core = sys.modules[__name__]
api_mode_enabled = os.getenv('A4KSUBTITLES_API_MODE') is not None

api_mode_enabled = True
handle = None
if not api_mode_enabled:
handle = int(sys.argv[1])

def main(paramstring):
def main(handle, paramstring):
core.api_mode_enabled = False
core.handle = handle

params = dict(utils.parse_qsl(paramstring))
if params['action'] in ('search', 'manualsearch'):
search(core, params)
Expand Down
4 changes: 2 additions & 2 deletions a4kSubtitles/lib/kodi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
else:
from . import kodi_mock

for target in api_mode.keys():
for target in ['xbmc', 'xbmcaddon', 'xbmcplugin', 'xbmcgui', 'xbmcvfs']:
if target == 'kodi':
continue
elif api_mode[target]:
elif api_mode.get(target, False):
mod = getattr(kodi_mock, target)
else:
mod = importlib.import_module(target)
Expand Down
4 changes: 3 additions & 1 deletion a4kSubtitles/lib/kodi_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,7 @@ def __copy(src_uri, dest):
archive_path = unquote(src_uri[:src_uri.find('.zip') + 4]).replace(__archive_proto, '')
member = unquote(src_uri[src_uri.find('.zip') + 5:]).replace(__archive_proto, '')
with ZipFile(archive_path, 'r') as zip_obj:
zip_obj.extract(member, os.path.dirname(dest))
dest_dir = os.path.dirname(dest)
zip_obj.extract(member, dest_dir)
os.rename(os.path.join(dest_dir, member), dest)
xbmcvfs.copy = __copy
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="0.0.13"
version="0.0.14"
provider-name="Unknown">
<requires>
<import addon="xbmc.python" version="2.25.0"/>
Expand All @@ -24,6 +24,9 @@ Supports: OpenSubtitles
<icon>icon.png</icon>
</assets>
<news>
[v0.0.14]:
* Fix API and normal usage at the same time

[v0.0.13]:
* Exclude development files from release

Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
# -*- coding: utf-8 -*-

import sys
from a4kSubtitles import core
import os
import importlib
from a4kSubtitles import api

if __name__ == '__main__':
core.main(sys.argv[2][1:])
os.environ.pop(api.api_mode_env_name, '')
core = importlib.import_module('a4kSubtitles.core')
core.main(int(sys.argv[1]), sys.argv[2][1:])
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="0.0.13"
version="0.0.14"
provider-name="Unknown">
<requires>
<import addon="xbmc.python" version="2.25.0"/>
Expand All @@ -27,6 +27,9 @@ Supports: OpenSubtitles
<icon>icon.png</icon>
</assets>
<news>
[v0.0.14]:
* Fix API and normal usage at the same time

[v0.0.13]:
* Exclude development files from release

Expand Down
2 changes: 1 addition & 1 deletion packages/addons.xml.crc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5dcf3bacbb37f4f778c448436388f4d4cb2ded44
0e400f04fed7e1ce5a14695acd56ea293f04af07
52 changes: 50 additions & 2 deletions tests/test_suite.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import sys
import os
import json
import re
import pytest

dir_name = os.path.dirname(__file__)
main = os.path.join(dir_name, '..')
Expand All @@ -28,6 +30,7 @@ def __search(a4ksubtitles_api, creds=None):
search.settings = {
'general.timeout': '15',
'general.results_limit': '20',
'general.remove_ads': 'false',
'opensubtitles.enabled': 'true',
'opensubtitles.username': creds.username,
'opensubtitles.password': creds.password,
Expand All @@ -43,6 +46,33 @@ def __search(a4ksubtitles_api, creds=None):

return search

def test_api():
def get_error_msg(e):
return str(e.value).replace('\'', '')

with pytest.raises(ImportError) as e:
api.A4kSubtitlesApi()
assert get_error_msg(e) == "No module named xbmc"

with pytest.raises(ImportError) as e:
api.A4kSubtitlesApi({'xbmc': True})
assert get_error_msg(e) == "No module named xbmcaddon"

with pytest.raises(ImportError) as e:
api.A4kSubtitlesApi({'xbmc': True, 'xbmcaddon': True})
assert get_error_msg(e) == "No module named xbmcplugin"

with pytest.raises(ImportError) as e:
api.A4kSubtitlesApi({'xbmc': True, 'xbmcaddon': True, 'xbmcplugin': True})
assert get_error_msg(e) == "No module named xbmcgui"

with pytest.raises(ImportError) as e:
api.A4kSubtitlesApi({'xbmc': True, 'xbmcaddon': True, 'xbmcplugin': True, 'xbmcgui': True})
assert get_error_msg(e) == "No module named xbmcvfs"

api.A4kSubtitlesApi({'xbmc': True, 'xbmcaddon': True, 'xbmcplugin': True, 'xbmcgui': True, 'xbmcvfs': True})
api.A4kSubtitlesApi({'kodi': True})

def test_search_missing_imdb_id():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})
log_error_spy = utils.spy_fn(a4ksubtitles_api.core.logger, 'error')
Expand All @@ -58,6 +88,7 @@ def test_search_missing_imdb_id():
def test_opensubtitles():
a4ksubtitles_api = api.A4kSubtitlesApi({'kodi': True})

# search
creds = lambda: None
creds.username = os.getenv('A4KSUBTITLES_OPENSUBTITLES_USERNAME', '')
creds.password = os.getenv('A4KSUBTITLES_OPENSUBTITLES_PASSWORD', '')
Expand All @@ -66,6 +97,7 @@ def test_opensubtitles():
assert len(search.results) == 20
assert search.results[0]['name'] == search.video_meta['filename']

# download
item = search.results[0]
item['action_args']['filename'] = item['name']

Expand All @@ -75,6 +107,22 @@ def test_opensubtitles():
'action_args': item['action_args']
}

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

assert filepath != ''

# remove_ads
with open(filepath, 'r') as f:
sub_contents = f.read()

assert re.match(r'.*OpenSubtitles.*', sub_contents, re.DOTALL) is not None

search.settings['general.remove_ads'] = 'true'
filepath = a4ksubtitles_api.download(params, search.settings)

assert filepath != ''

with open(filepath, 'r') as f:
sub_contents = f.read()

assert result != ''
assert re.match(r'.*OpenSubtitles.*', sub_contents, re.DOTALL) is None

0 comments on commit 6bc7166

Please sign in to comment.