Skip to content

Commit

Permalink
handle timeout search fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilky3 committed Jul 7, 2024
1 parent 8329197 commit ad7cb05
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 13 deletions.
8 changes: 8 additions & 0 deletions resources/language/English/strings.po
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,14 @@ msgctxt "#32703"
msgid "Critical error check log for more info"
msgstr ""

msgctxt "#32704"
msgid "Jackett timeout"
msgstr ""

msgctxt "#32705"
msgid "Jackett connection error"
msgstr ""

msgctxt "#32750"
msgid "Filtering result..."
msgstr ""
Expand Down
8 changes: 8 additions & 0 deletions resources/language/messages.pot
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ msgctxt "#32703"
msgid "Critical error check log for more info"
msgstr ""

msgctxt "#32704"
msgid "Jackett timeout"
msgstr ""

msgctxt "#32705"
msgid "Jackett connection error"
msgstr ""

msgctxt "#32750"
msgid "Filtering result..."
msgstr ""
Expand Down
23 changes: 19 additions & 4 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,17 @@ async def send_request(self, url, params=None):
return root

async def request_indexers(self):
root = await self.send_request(self.get_indexers_list)
try:
root = await self.send_request(self.get_indexers_list)
except asyncio.TimeoutError as e:
log.warn(f"Request to {self.get_indexers_list} timed out: {e}")
utils.notify(utils.get_localized_string(utils.MsgID.JACKETT_TIMEOUT))
return
except aiohttp.ClientError as e:
log.error(f"Request to {self.get_indexers_list} failed with exception: {e}")
utils.notify(utils.get_localized_string(utils.MsgID.JACKETT_CLIENT_ERR))
return

if not root:
return
self.indexers.clear()
Expand Down Expand Up @@ -147,10 +157,15 @@ async def search_by_indexer(self, indexer, t, title, year, season=None, ep=None,
params["q"] += " S{:0>2}".format(season)
if bool(ep):
params["q"] += "E{:0>2}".format(ep)

url = self.query_by_indexer.format(indexer=indexer.id)
try:
root = await self.send_request(self.query_by_indexer.format(indexer=indexer.id), params)
except TimeoutError:
log.warn(f"Timeout while search {indexer.name}")
root = await self.send_request(url, params)
except asyncio.TimeoutError as e:
log.warn(f"Request to {url} timed out: {e}")
return {}, indexer
except aiohttp.ClientError as e:
log.error(f"Request to {url} failed with exception: {e}")
return {}, indexer

if not root:
Expand Down
25 changes: 17 additions & 8 deletions src/jackett.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ async def validate_client():


def search(payload, method="general"):
log.info(f"got req from elementum:{payload}")
payload = parse_payload(method, payload)

log.debug(f"Searching with payload ({method}): f{payload}")
results = []
p_dialog = PDialog(utils.translation(32602))
try:
log.info(f"got req from elementum:{payload}")
payload = parse_payload(method, payload)

log.debug(f"Searching with payload ({method}): f{payload}")
results = []
p_dialog = PDialog(utils.translation(32602))

request_start_time = time.time()
results = asyncio.run(search_jackett(p_dialog, payload, method))
request_end_time = time.time()
Expand Down Expand Up @@ -112,8 +113,16 @@ def parse_payload(method, payload):
payload["search_title"] = payload["titles"][kodi_language]

if "search_title" not in payload:
log.info(f"Could not determine search title, falling back to normal title: {payload['title']}")
payload["search_title"] = payload["title"]
log.warn(f"Could not determine search language for title")
if utils.is_english(payload['title']):
log.info(f"falling back to original_en title: {payload['title']}")
payload["search_title"] = payload["title"]
elif "en" in payload["titles"]:
log.info(f"falling back to not original english title: {payload['titles']['en']}")
payload["search_title"] = payload["titles"]["en"]
else:
log.warn(f"no original english title found. Using: {payload['title']}")
payload["search_title"] = payload["title"]

payload['season_name'] = utils.check_season_name(payload["search_title"], payload.get('season_name', ""))

Expand Down
110 changes: 109 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import re
import itertools
from collections import OrderedDict
from enum import Enum

from kodi_six import xbmcgui

Expand Down Expand Up @@ -49,17 +50,115 @@
])


class MsgID(Enum):
JACKETT = 32000
HOST = 32001
API_KEY = 32002
JACKETT_SETTINGS_VALID = 32003
VALIDATE_SETTINGS = 32004
VALIDATING = 32005
SUCCESSFULLY_CONNECTED_TO_JACKETT = 32006
ENABLE_SEARCH_WITH_IMDB_ID = 32007
LIMIT_TORRENT_COUNT = 32008
CONNECTING_TO_JACKETT_FAILED = 32009
FILTERING = 32050
GENERAL = 32051
SORT_RETURNED_RESULTS_BY = 32052
RESOLUTION = 32053
SEEDS = 32054
SIZE = 32055
BALANCED = 32056
HIDE_TORRENTS_WITHOUT_SEEDS = 32057
SECONDARY_SEARCH_FOR_SEASON = 32058
USE_SMART_FILTER_FOR_SHOWS = 32059
FILTER_KEYWORDS = 32100
ENABLE = 32101
BLOCK = 32102
REQUIRE = 32103
FILTER_SIZE = 32150
INCLUDE_UNKNOWN_FILE_SIZE = 32152
MIN_SIZE_GB = 32153
MAX_SIZE_GB = 32154
MIN_MOVIE_SIZE_GB = 32155
MAX_MOVIE_SIZE_GB = 32156
MIN_SEASON_SIZE_GB = 32157
MAX_SEASON_SIZE_GB = 32158
MIN_EPISODE_SIZE_GB = 32159
MAX_EPISODE_SIZE_GB = 32160
FILTER_RESOLUTION = 32200
RESOLUTION_ENABLE = 32201
RESOLUTION_4K = 32202
RESOLUTION_2K = 32203
RESOLUTION_1080P = 32204
RESOLUTION_720P = 32205
RESOLUTION_480P = 32206
RESOLUTION_240P = 32207
RESOLUTION_UNKNOWN = 32208
FILTER_RELEASE_TYPE = 32250
RELEASE_TYPE_ENABLE = 32251
BRRIP_BDRIP_BLURAY = 32252
WEBDL_WEBRIP = 32253
HDRIP = 32254
HDTV = 32255
DVDRIP = 32256
H26X = 32257
DVDSCR = 32258
SCREENER_SCR = 32259
RELEASE_TYPE_3D = 32260
TELE_SYNC_TS_TC = 32261
CAM_HDCAM = 32262
TVRIP_SATRIP = 32263
IPTVRIP = 32264
VHSRIP = 32265
TRAILER = 32266
WORKPRINT = 32267
LINE = 32268
RELEASE_TYPE_UNKNOWN = 32269
ADVANCED = 32300
DEBUGGER = 32301
ADDITIONAL_LIBRARIES = 32303
DEBUGGER_HOST = 32304
DEBUGGER_PORT = 32305
JACKETT_HOST_INVALID = 32600
JACKETT_API_KEY_INVALID = 32601
SEARCHING = 32602
UNABLE_TO_CONNECT_TO_JACKETT = 32603
REQUESTING_RESULTS_FROM_JACKETT = 32604
JACKETT_ERROR = 32700
UNABLE_TO_DETERMINE_JACKETT_CAPABILITIES = 32701
JACKETT_UNABLE_TO_SEARCH = 32702
CRITICAL_ERROR = 32703
JACKETT_TIMEOUT = 32704
JACKETT_CLIENT_ERR = 32705
FILTERING_RESULT = 32750
RESOLVED_MAGNET_LINKS = 32751
WAITING = 32752
SORTING = 32753
REQUESTS_DONE = 32754
RETURNING_TO_ELEMENTUM = 32755
ADDON_IS_PROVIDER = 32800


def get_icon_path(icon='icon.png'):
return os.path.join(addon.PATH, 'resources', 'images', icon)


def get_localized_string(key: MsgID):
"""Fetches the localized string for the given key."""
if isinstance(key, MsgID):
return translation(key.value)
else:
log.warn(f"'{key}' translation not found. Please fix MsgID enum")
return f"'{key}' translation not found"


def translation(id_value):
return addon.ADDON.getLocalizedString(id_value)


def notify(message, image=None):
dialog = xbmcgui.Dialog()
dialog.notification(addon.NAME, message, icon=image, sound=False)
dialog.notification(addon.NAME, message, icon=image, sound=False, time=5000)
del dialog


Expand Down Expand Up @@ -132,3 +231,12 @@ def concat_dicts(dicts):
for d in dicts:
d4.update(d)
return d4


def is_english(s):
try:
s.encode(encoding='utf-8').decode('ascii')
except UnicodeDecodeError:
return False
else:
return True

0 comments on commit ad7cb05

Please sign in to comment.