Skip to content

Commit c1b6e56

Browse files
[script.module.autocompletion@matrix] 2.0.5 (#2303)
* [script.module.autocompletion] 2.0.5 * Fix attributions and license * Update maintainer status
1 parent 696764e commit c1b6e56

File tree

4 files changed

+81
-31
lines changed

4 files changed

+81
-31
lines changed
Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2-
<addon id="script.module.autocompletion" name="AutoCompletion library" version="2.0.3" provider-name="Philipp Temminghoff (phil65),sualfred">
2+
<addon id="script.module.autocompletion" name="AutoCompletion library" version="2.0.5" provider-name="Philipp Temminghoff (phil65), sualfred, bbaronSVK, finkleandeinhorn">
33
<requires>
44
<import addon="xbmc.python" version="3.0.0" />
55
<import addon="script.module.requests" version="2.9.1"/>
66
</requires>
77
<extension point="xbmc.python.module" library="lib" />
88
<extension point="xbmc.addon.metadata">
9-
<summary lang="en_GB">Module providing some AutoCompletion functions</summary>
10-
<description lang="en_GB">Module providing some AutoCompletion functions</description>
9+
<summary lang="en">Module providing some AutoCompletion functions</summary>
10+
<description lang="en">Module providing some AutoCompletion functions</description>
1111
<platform>all</platform>
1212
<license>GPL-2.0-or-later</license>
13-
<source>https://github.com/sualfred/script.module.autocompletion</source>
13+
<source>https://github.com/finkleandeinhorn/script.module.autocompletion</source>
1414
<assets>
15-
<icon>resources/icon.png</icon>
15+
<icon>icon.png</icon>
1616
</assets>
1717
</extension>
18-
</addon>
18+
</addon>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
1.1.2
2+
3+
- move some stuff out of this lib (to the places they belong to)
4+
5+
1.1.1
6+
7+
- fix wrong reference to extendedinfo script
8+
9+
1.1.0
10+
11+
- No results when keyboard with hidden input is active
12+
- big code rework
13+
- added bing as provider
14+
- fixed localdict provider
15+
16+
1.0.0
17+
18+
- Initial release
File renamed without changes.

script.module.autocompletion/lib/AutoCompletion.py

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33
# Copyright (C) 2015 - Philipp Temminghoff <[email protected]>
44
# This program is Free Software see LICENSE file for details
55

6-
import sys
7-
import codecs
6+
from urllib.parse import quote_plus
87
import os
98
import time
109
import hashlib
1110
import requests
1211
import json
13-
from urllib.parse import quote_plus
1412

13+
import xbmc
1514
import xbmcaddon
1615
import xbmcvfs
17-
import xbmc
1816

1917
HEADERS = {'User-agent': 'Mozilla/5.0'}
2018

@@ -24,8 +22,6 @@
2422
ADDON_ID = ADDON.getAddonInfo('id')
2523
ADDON_DATA_PATH = xbmcvfs.translatePath("special://profile/addon_data/%s" % ADDON_ID)
2624

27-
MONITOR = xbmc.Monitor()
28-
2925

3026
def get_autocomplete_items(search_str, limit=10, provider=None):
3127
"""
@@ -39,6 +35,8 @@ def get_autocomplete_items(search_str, limit=10, provider=None):
3935
provider = GoogleProvider(limit=limit)
4036
elif SETTING("autocomplete_provider") == "bing":
4137
provider = BingProvider(limit=limit)
38+
elif SETTING("autocomplete_provider") == "tmdb":
39+
provider = TmdbProvider(limit=limit)
4240
else:
4341
provider = LocalDictProvider(limit=limit)
4442
provider.limit = limit
@@ -53,9 +51,8 @@ def prep_search_str(text):
5351

5452

5553
class BaseProvider(object):
56-
5754
def __init__(self, *args, **kwargs):
58-
self.limit = int(kwargs.get("limit", 10))
55+
self.limit = kwargs.get("limit", 10)
5956

6057
def get_predictions(self, search_str):
6158
if not search_str:
@@ -86,7 +83,10 @@ def __init__(self, *args, **kwargs):
8683
self.youtube = kwargs.get("youtube", False)
8784

8885
def fetch_data(self, search_str):
89-
url = "search?hl=%s&q=%s&json=t&client=serp" % (SETTING("autocomplete_lang"), quote_plus(search_str))
86+
url = "search?hl=%s&q=%s&json=t&client=serp" % (
87+
SETTING("autocomplete_lang"),
88+
quote_plus(search_str),
89+
)
9090
if self.youtube:
9191
url += "&ds=yt"
9292
result = get_JSON_response(url=self.BASE_URL + url,
@@ -107,17 +107,47 @@ def __init__(self, *args, **kwargs):
107107

108108
def fetch_data(self, search_str):
109109
url = "query=%s" % (quote_plus(search_str))
110-
result = get_JSON_response(url=self.BASE_URL + url,
111-
headers=HEADERS,
112-
folder="Bing")
110+
result = get_JSON_response(
111+
url=self.BASE_URL + url, headers=HEADERS, folder="Bing"
112+
)
113113
if not result:
114114
return []
115115
else:
116116
return result[1]
117117

118118

119-
class LocalDictProvider(BaseProvider):
119+
class TmdbProvider(BaseProvider):
120+
121+
BASE_URL = "https://www.themoviedb.org/search/multi?"
120122

123+
def __init__(self, *args, **kwargs):
124+
super(TmdbProvider, self).__init__(*args, **kwargs)
125+
126+
def fetch_data(self, search_str):
127+
url = "language=%s&query=%s" % (
128+
SETTING("autocomplete_lang"),
129+
quote_plus(search_str),
130+
)
131+
result = get_JSON_response(
132+
url=self.BASE_URL + url, headers=HEADERS, folder="TMDB"
133+
)
134+
if not result or "results" not in result:
135+
return []
136+
out = []
137+
for i in result["results"]:
138+
title = None
139+
if "media_type" in i:
140+
if i["media_type"] == "movie":
141+
title = i["title"]
142+
elif i["media_type"] in ["tv", "person"]:
143+
title = i["name"]
144+
else:
145+
title = i
146+
out.append(title)
147+
return out
148+
149+
150+
class LocalDictProvider(BaseProvider):
121151
def __init__(self, *args, **kwargs):
122152
super(LocalDictProvider, self).__init__(*args, **kwargs)
123153

@@ -131,14 +161,14 @@ def get_predictions(self, search_str):
131161
search_str = search_str[k + 1:]
132162
local = SETTING("autocomplete_lang_local")
133163
path = os.path.join(ADDON_PATH, "resources", "data", "common_%s.txt" % (local if local else "en"))
134-
with codecs.open(path, encoding="utf8") as f:
135-
for line in f.readlines():
164+
with xbmcvfs.File(path) as f:
165+
for line in f.read().split('\n'):
136166
if not line.startswith(search_str) or len(line) <= 2:
137167
continue
138168
li = {"label": line,
139169
"search_string": line}
140170
listitems.append(li)
141-
if len(listitems) > self.limit:
171+
if len(listitems) > int(self.limit):
142172
break
143173
return listitems
144174

@@ -148,11 +178,11 @@ def get_JSON_response(url="", cache_days=7.0, folder=False, headers=False):
148178
get JSON response for *url, makes use of file cache.
149179
"""
150180
now = time.time()
151-
hashed_url = hashlib.md5(url.encode()).hexdigest()
181+
hashed_url = hashlib.md5(url.encode('utf-8')).hexdigest()
152182
if folder:
153-
cache_path = xbmcvfs.translatePath(os.path.join(ADDON_DATA_PATH, folder))
183+
cache_path = xbmc.translatePath(os.path.join(ADDON_DATA_PATH, folder))
154184
else:
155-
cache_path = xbmcvfs.translatePath(os.path.join(ADDON_DATA_PATH))
185+
cache_path = xbmc.translatePath(os.path.join(ADDON_DATA_PATH))
156186
path = os.path.join(cache_path, hashed_url + ".txt")
157187
cache_seconds = int(cache_days * 86400.0)
158188
if xbmcvfs.exists(path) and ((now - os.path.getmtime(path)) < cache_seconds):
@@ -183,16 +213,17 @@ def get_http(url=None, headers=False):
183213
"""
184214
succeed = 0
185215
if not headers:
186-
headers = HEADERS
187-
while succeed < 2 and not MONITOR.abortRequested():
216+
headers = {'User-agent': 'XBMC/16.0 ( [email protected] )'}
217+
monitor = xbmc.Monitor()
218+
while (succeed < 2) and (not monitor.abortRequested()):
188219
try:
189220
r = requests.get(url, headers=headers)
190221
if r.status_code != 200:
191222
raise Exception
192223
return r.text
193224
except Exception:
194225
log("get_http: could not get data from %s" % url)
195-
xbmc.sleep(1000)
226+
monitor.waitForAbort(1)
196227
succeed += 1
197228
return None
198229

@@ -203,8 +234,9 @@ def read_from_file(path="", raw=False):
203234
"""
204235
if not xbmcvfs.exists(path):
205236
return False
237+
206238
try:
207-
with open(path) as f:
239+
with xbmcvfs.File(path) as f:
208240
log("opened textfile %s." % (path))
209241
if raw:
210242
return f.read()
@@ -230,8 +262,8 @@ def save_to_file(content, filename, path=""):
230262
text_file_path = os.path.join(path, filename + ".txt")
231263
now = time.time()
232264

233-
with open(text_file_path, 'w') as f:
234-
json.dump(content, f)
265+
with xbmcvfs.File(text_file_path, "w") as text_file:
266+
json.dump(content, text_file)
235267

236268
log("saved textfile %s. Time: %f" % (text_file_path, time.time() - now))
237269
return True

0 commit comments

Comments
 (0)