Skip to content

Commit

Permalink
Search tv series 4 times name, name_show_year, name_season_year, name…
Browse files Browse the repository at this point in the history
…_ep_year
  • Loading branch information
vasilky3 committed Jan 11, 2024
1 parent 52fed81 commit 85e71ce
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 25 deletions.
47 changes: 28 additions & 19 deletions src/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ def __init__(self, host, api_key):
self.indexers = []
self.session = None

async def create_session(self, host=None):
timeout = aiohttp.ClientTimeout(20)
self.session = aiohttp.ClientSession(host or self.host, timeout=timeout)

async def close_session(self):
if not self.session.closed:
await self.session.close()

async def send_request(self, url, params=None):
log.info(f"Sending to Jackett url: {url} params: {params}")
if params is None:
Expand All @@ -99,9 +107,6 @@ async def send_request(self, url, params=None):
body = await resp.text()

log.info(f"Jackett returned response in {response_time:.3f}s size {len(body)} b")
log.debug("===============================")
log.debug(body)
log.debug("===============================")

root = ElT.fromstring(body)
if root.tag == "error":
Expand Down Expand Up @@ -146,7 +151,7 @@ async def search_by_indexer(self, indexer, t, title, year, season=None, ep=None,
root = await self.send_request(self.query_by_indexer.format(indexer=indexer.id), params)

if not root:
return [], indexer
return {}, indexer
torr_list = parse_torrents(root)
log.info(f"Done searching. Got {len(torr_list)} torrents from {indexer.name}.")
return torr_list, indexer
Expand All @@ -173,29 +178,33 @@ def update_p_dialog():

async def search_movie(self, title, year=None, imdb_id=None, p_dialog_cb=None):
tasks = [self.search_by_indexer(ind, SearchType.MOVIE, title, year, imdb_id=imdb_id) for ind in self.indexers]
torrent_list = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_list(torrent_list)
torrent_dicts = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_dicts(torrent_dicts).values()

async def search_tv(self, title, year=None, season=None, ep=None, imdb_id=None, p_dialog_cb=None):
tasks = [self.search_by_indexer(ind, SearchType.TV, title, year, season, ep, imdb_id) for ind in
self.indexers]
if get_setting("search_season_on_episode", bool) and bool(season) and bool(ep):
tasks += [self.search_by_indexer(ind, SearchType.TV, title, year, season, imdb_id=imdb_id) for ind in
self.indexers]
torrent_list = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_list(torrent_list)
torrent_dicts = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_dicts(torrent_dicts).values()

async def search_query(self, title, year=None, imdb_id=None, p_dialog_cb=None):
tasks = [self.search_by_indexer(ind, SearchType.COMMON, title, year, imdb_id=imdb_id) for ind in self.indexers]
torrent_list = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_list(torrent_list)

async def create_session(self, host=None):
self.session = aiohttp.ClientSession(host or self.host)

async def close_session(self):
if not self.session.closed:
await self.session.close()
torrent_dicts = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_dicts(torrent_dicts).values()

async def search_tv_smart(self, title, ep_year=None, season_year=None, show_year=None, p_dialog_cb=None):
tasks = [self.search_by_indexer(ind, SearchType.TV, title, None) for ind in self.indexers]
if ep_year and ep_year != season_year:
tasks += [self.search_by_indexer(ind, SearchType.TV, title, ep_year) for ind in self.indexers]
if season_year and season_year != show_year:
tasks += [self.search_by_indexer(ind, SearchType.TV, title, season_year) for ind in self.indexers]
if show_year:
tasks += [self.search_by_indexer(ind, SearchType.TV, title, show_year) for ind in self.indexers]
torrent_dicts = await self.await_indexers(tasks, p_dialog_cb)
return utils.concat_dicts(torrent_dicts).values()

@staticmethod
def proceed_resp_error(code, description):
Expand All @@ -206,12 +215,12 @@ def proceed_resp_error(code, description):


def parse_torrents(root):
results = []
results = {}
items = root.findall("channel/item")
for item in items:
result = parse_item(item)
if result is not None:
results.append(result)
results[result['name']] = result
return results


Expand Down
2 changes: 1 addition & 1 deletion src/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ def tv_season_episode(results, season, season_name, episode, global_ep, ep_year,
continue
# Remove the season from the text
else:
log.debug("No season found")
log.debug("No season found, it's ok")

if not global_ep:
continue
Expand Down
5 changes: 3 additions & 2 deletions src/jackett.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,9 @@ async def search_jackett(p_dialog, payload, method):
p_dialog_cb=p_dialog.callback(query_weight))
elif method in ('season', 'episode', 'anime'):
if get_setting("use_smart_show_filter", bool):
res = await j_cli.search_tv(payload["search_title"], imdb_id=payload["imdb_id"],
p_dialog_cb=p_dialog.callback(query_weight))
res = await j_cli.search_tv_smart(payload["search_title"], payload.get('year', None),
payload.get('season_year', None), payload.get('show_year', None),
p_dialog_cb=p_dialog.callback(query_weight))
else:
res = await j_cli.search_tv(payload["search_title"], season=payload.get("season", None),
ep=payload.get("episode", None), imdb_id=payload["imdb_id"],
Expand Down
8 changes: 5 additions & 3 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ def check_season_name(title, season_name=""):
return season_name


def concat_list(li):
""" converts [[a],[b,c]] to [a, b, c] """
return list(itertools.chain.from_iterable(li))
def concat_dicts(dicts):
d4 = {}
for d in dicts:
d4.update(d)
return d4

0 comments on commit 85e71ce

Please sign in to comment.