Skip to content

Commit

Permalink
Added cache to Titlovi request to prevent doing the same request over…
Browse files Browse the repository at this point in the history
… and over again for each and every episode of a show.
  • Loading branch information
morpheus65535 committed May 7, 2021
1 parent ea24176 commit 29ad8c6
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions libs/subliminal_patch/providers/titlovi.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io
import logging
import re
from datetime import datetime
from datetime import datetime, timedelta
import dateutil.parser

import rarfile
Expand All @@ -31,6 +31,8 @@
from subliminal.cache import region
from six.moves import map

SHOW_EXPIRATION_TIME = timedelta(hours=3).total_seconds()

# parsing regex definitions
title_re = re.compile(r'(?P<title>(?:.+(?= [Aa][Kk][Aa] ))|.+)(?:(?:.+)(?P<altitle>(?<= [Aa][Kk][Aa] ).+))?')

Expand Down Expand Up @@ -191,9 +193,14 @@ def log_in(self):

except RequestException as e:
logger.error(e)

def terminate(self):
self.session.close()

@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME)
def get_result(self, search_url, search_params):
return self.session.get(search_url, params=search_params)

def query(self, languages, title, season=None, episode=None, year=None, imdb_id=None, video=None):
search_params = dict()

Expand All @@ -215,8 +222,8 @@ def query(self, languages, title, season=None, episode=None, year=None, imdb_id=
is_episode = False
if season and episode:
is_episode = True
search_params['season'] = season
search_params['episode'] = episode
#search_params['season'] = season
#search_params['episode'] = episode
#if year:
# search_params['year'] = year
if imdb_id:
Expand All @@ -232,12 +239,12 @@ def query(self, languages, title, season=None, episode=None, year=None, imdb_id=
search_params['userid'] = self.user_id
search_params['json'] = True

response = self.session.get(self.api_search_url, params=search_params)
#response = self.get_result(search_url=self.api_search_url, search_params=search_params)
response = self.get_result(self.api_search_url, search_params)
resp_json = response.json()
if resp_json['SubtitleResults']:
query_results.extend(resp_json['SubtitleResults'])


except Exception as e:
logger.error(e)

Expand All @@ -253,6 +260,12 @@ def query(self, languages, title, season=None, episode=None, year=None, imdb_id=

# handle movies and series separately
if is_episode:
# skip if season and episode number does not match
if season and season != sub.get('Season'):
continue
elif episode and episode != sub.get('Episode'):
continue

subtitle = self.subtitle_class(Language.fromtitlovi(sub.get('Lang')), sub.get('Link'), sub.get('Id'), sub.get('Release'), _title,
alt_title=alt_title, season=sub.get('Season'), episode=sub.get('Episode'),
year=sub.get('Year'), rating=sub.get('Rating'),
Expand Down

0 comments on commit 29ad8c6

Please sign in to comment.