Add Trakt OAuth support

This commit is contained in:
mza921 2019-11-18 14:02:42 -08:00
parent 8095f3c51d
commit ba648199b7
4 changed files with 64 additions and 5 deletions

View file

@ -13,6 +13,8 @@ from plex_tools import add_to_collection
from plex_tools import get_collection
from radarr_tools import add_to_radarr
from imdb_tools import tmdb_get_summary
from trakt import Trakt
import trakt_helpers
class Config:
def __init__(self):
@ -38,7 +40,6 @@ class Plex:
self.show_library = config['show_library']
self.Server = PlexServer(self.url, self.token, timeout=self.timeout)
self.Sections = self.Server.library.sections()
print(self.Sections)
# self.MovieLibrary = next((s for s in self.Sections if (s.title == self.movie_library)) and (isinstance(s, MovieSection)), None)
self.MovieLibrary = next((s for s in self.Sections if (s.title == self.movie_library)), None)
self.ShowLibrary = next(s for s in self.Sections if (s.title == self.show_library) and isinstance(s, ShowSection))
@ -61,11 +62,17 @@ class TMDB:
self.language = config['language']
class Trakt:
class TraktClient:
def __init__(self):
import copy
config = Config().trakt
self.client_id = config['client_id']
self.client_secret = config['client_secret']
self.authorization = config['authorization']
Trakt.configuration.defaults.client(self.client_id, self.client_secret)
self.authorization = trakt_helpers.authenticate(self.authorization)
Trakt.configuration.defaults.oauth.from_response(self.authorization)
trakt_helpers.save_authorization(Config().config_path, self.authorization)
class ImageServer:

View file

@ -5,3 +5,4 @@ requests
flask
git+git://github.com/pkkid/python-plexapi.git#egg=plexapi
trakt.py
ruamel.yaml

49
trakt_helpers.py Normal file
View file

@ -0,0 +1,49 @@
from trakt import Trakt
import json
import os
import six
import copy
import ruamel.yaml
def authenticate(authorization=None):
# authorization = os.environ.get('AUTHORIZATION')
if authorization['access_token']:
# Test authorization
with Trakt.configuration.oauth.from_response(authorization, refresh=True):
if Trakt['users/settings']:
return authorization
print('Navigate to: %s' % Trakt['oauth'].authorize_url('urn:ietf:wg:oauth:2.0:oob'))
code = six.moves.input('Authorization code: ')
if not code:
exit(1)
authorization = Trakt['oauth'].token(code, 'urn:ietf:wg:oauth:2.0:oob')
if not authorization:
exit(1)
print('Authorization: %r' % authorization)
# return authorization
return authorization
def save_authorization(config_file, authorization):
from ruamel.yaml.util import load_yaml_guess_indent
config, ind, bsi = load_yaml_guess_indent(open(config_file))
# config['trakt']['authorization'] = None
config['trakt']['authorization']['access_token'] = authorization['access_token']
config['trakt']['authorization']['token_type'] = authorization['token_type']
config['trakt']['authorization']['expires_in'] = authorization['expires_in']
config['trakt']['authorization']['refresh_token'] = authorization['refresh_token']
config['trakt']['authorization']['scope'] = authorization['scope']
config['trakt']['authorization']['created_at'] = authorization['created_at']
# with open(config_file, 'w') as fp:
# yaml.dump(config, fp)
ruamel.yaml.round_trip_dump(
config,
open(config_file, 'w'),
indent=ind,
block_seq_indent=bsi
)

View file

@ -4,7 +4,8 @@ import plex_tools
import trakt
def trakt_get_movies(plex, data):
trakt.Trakt.configuration.defaults.client(config_tools.Trakt().client_id, config_tools.Trakt().client_secret)
# trakt.Trakt.configuration.defaults.client(config_tools.Trakt().client_id, config_tools.Trakt().client_secret)
config_tools.TraktClient()
trakt_url = data
if trakt_url[-1:] == " ":
trakt_url = trakt_url[:-1]
@ -47,10 +48,11 @@ def trakt_get_movies(plex, data):
return matched_imbd_movies, missing_imdb_movies
else:
# No movies
return None, None
return False, False
def trakt_get_shows(plex, data):
trakt.Trakt.configuration.defaults.client(config_tools.Trakt().client_id, config_tools.Trakt().client_secret)
# trakt.Trakt.configuration.defaults.client(config_tools.Trakt().client_id, config_tools.Trakt().client_secret)
config_tools.TraktClient()
trakt_url = data
if trakt_url[-1:] == " ":
trakt_url = trakt_url[:-1]