Skip to content

Commit

Permalink
Feature/fix trakt tv show (#9018)
Browse files Browse the repository at this point in the history
* Fix saving trakt configuration on restart.

* Fix trakt error on episode snatch.
  • Loading branch information
p0psicles authored Jan 13, 2021
1 parent afbf8c4 commit 0a4be8b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 31 deletions.
64 changes: 36 additions & 28 deletions ext/trakt/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,41 +413,49 @@ def _refresh_token(s):
raise s.error_map[response.status_code]()


def load_config():
"""Manually load config from json config file."""
global CLIENT_ID, CLIENT_SECRET, OAUTH_TOKEN, OAUTH_EXPIRES_AT
global OAUTH_REFRESH, APPLICATION_ID, CONFIG_PATH
if (CLIENT_ID is None or CLIENT_SECRET is None) and \
os.path.exists(CONFIG_PATH):
# Load in trakt API auth data from CONFIG_PATH
with open(CONFIG_PATH) as config_file:
config_data = json.load(config_file)

if CLIENT_ID is None:
CLIENT_ID = config_data.get('CLIENT_ID', None)
if CLIENT_SECRET is None:
CLIENT_SECRET = config_data.get('CLIENT_SECRET', None)
if OAUTH_TOKEN is None:
OAUTH_TOKEN = config_data.get('OAUTH_TOKEN', None)
if OAUTH_EXPIRES_AT is None:
OAUTH_EXPIRES_AT = config_data.get('OAUTH_EXPIRES_AT', None)
if OAUTH_REFRESH is None:
OAUTH_REFRESH = config_data.get('OAUTH_REFRESH', None)
if APPLICATION_ID is None:
APPLICATION_ID = config_data.get('APPLICATION_ID', None)


def _bootstrapped(f):
"""Bootstrap your authentication environment when authentication is needed
and if a file at `CONFIG_PATH` exists. The process is completed by setting
the client id header.
"""
@wraps(f)
def inner(*args, **kwargs):
global CLIENT_ID, CLIENT_SECRET, OAUTH_TOKEN, OAUTH_EXPIRES_AT
global OAUTH_REFRESH, APPLICATION_ID
if (CLIENT_ID is None or CLIENT_SECRET is None) and \
os.path.exists(CONFIG_PATH):
# Load in trakt API auth data from CONFIG_PATH
with open(CONFIG_PATH) as config_file:
config_data = json.load(config_file)

if CLIENT_ID is None:
CLIENT_ID = config_data.get('CLIENT_ID', None)
if CLIENT_SECRET is None:
CLIENT_SECRET = config_data.get('CLIENT_SECRET', None)
if OAUTH_TOKEN is None:
OAUTH_TOKEN = config_data.get('OAUTH_TOKEN', None)
if OAUTH_EXPIRES_AT is None:
OAUTH_EXPIRES_AT = config_data.get('OAUTH_EXPIRES_AT', None)
if OAUTH_REFRESH is None:
OAUTH_REFRESH = config_data.get('OAUTH_REFRESH', None)
if APPLICATION_ID is None:
APPLICATION_ID = config_data.get('APPLICATION_ID', None)

# Check token validity and refresh token if needed
if (not OAUTH_TOKEN_VALID and OAUTH_EXPIRES_AT is not None and
OAUTH_REFRESH is not None):
_validate_token(args[0])
# For backwards compatability with trakt<=2.3.0
if api_key is not None and OAUTH_TOKEN is None:
OAUTH_TOKEN = api_key
global OAUTH_TOKEN_VALID, OAUTH_EXPIRES_AT
global OAUTH_REFRESH, OAUTH_TOKEN

load_config()
# Check token validity and refresh token if needed
if (not OAUTH_TOKEN_VALID and OAUTH_EXPIRES_AT is not None
and OAUTH_REFRESH is not None):
_validate_token(args[0])
# For backwards compatability with trakt<=2.3.0
if api_key is not None and OAUTH_TOKEN is None:
OAUTH_TOKEN = api_key

return f(*args, **kwargs)
return inner

Expand Down
1 change: 1 addition & 0 deletions medusa/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1019,6 +1019,7 @@ def initialize(self, console_logging=True):

# Initialize trakt config path.
trakt.core.CONFIG_PATH = os.path.join(app.CACHE_DIR, '.pytrakt.json')
trakt.core.load_config()

# reconfigure the logger
app_logger.reconfigure()
Expand Down
5 changes: 2 additions & 3 deletions medusa/notifiers/trakt.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ def add_episode_to_watchlist(episode):
:params episode: Episode Object.
"""
show_id = None
show_id = str(episode.series.externals.get('trakt_id', episode.series.name))

try:
tv_show = tv.TVShow(show_id)
tv_episode = tv.TVEpisode(tv_show, episode.season, episode.episode)
tv_episode = tv.TVEpisode(show_id, episode.season, episode.episode)
tv_episode.add_to_watchlist()
except TraktException as error:
log.warning('Unable to add episode to watchlist: {error!r}', {'error': error})
Expand Down

0 comments on commit 0a4be8b

Please sign in to comment.