mirror of
https://github.com/mza921/Plex-Auto-Collections
synced 2024-11-15 00:37:13 +00:00
Add Trakt OAuth support
This commit is contained in:
parent
8095f3c51d
commit
ba648199b7
4 changed files with 64 additions and 5 deletions
|
@ -13,6 +13,8 @@ from plex_tools import add_to_collection
|
||||||
from plex_tools import get_collection
|
from plex_tools import get_collection
|
||||||
from radarr_tools import add_to_radarr
|
from radarr_tools import add_to_radarr
|
||||||
from imdb_tools import tmdb_get_summary
|
from imdb_tools import tmdb_get_summary
|
||||||
|
from trakt import Trakt
|
||||||
|
import trakt_helpers
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -38,7 +40,6 @@ class Plex:
|
||||||
self.show_library = config['show_library']
|
self.show_library = config['show_library']
|
||||||
self.Server = PlexServer(self.url, self.token, timeout=self.timeout)
|
self.Server = PlexServer(self.url, self.token, timeout=self.timeout)
|
||||||
self.Sections = self.Server.library.sections()
|
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)) and (isinstance(s, MovieSection)), None)
|
||||||
self.MovieLibrary = next((s for s in self.Sections if (s.title == self.movie_library)), 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))
|
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']
|
self.language = config['language']
|
||||||
|
|
||||||
|
|
||||||
class Trakt:
|
class TraktClient:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
import copy
|
||||||
config = Config().trakt
|
config = Config().trakt
|
||||||
self.client_id = config['client_id']
|
self.client_id = config['client_id']
|
||||||
self.client_secret = config['client_secret']
|
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:
|
class ImageServer:
|
||||||
|
|
|
@ -5,3 +5,4 @@ requests
|
||||||
flask
|
flask
|
||||||
git+git://github.com/pkkid/python-plexapi.git#egg=plexapi
|
git+git://github.com/pkkid/python-plexapi.git#egg=plexapi
|
||||||
trakt.py
|
trakt.py
|
||||||
|
ruamel.yaml
|
||||||
|
|
49
trakt_helpers.py
Normal file
49
trakt_helpers.py
Normal 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
|
||||||
|
)
|
|
@ -4,7 +4,8 @@ import plex_tools
|
||||||
import trakt
|
import trakt
|
||||||
|
|
||||||
def trakt_get_movies(plex, data):
|
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
|
trakt_url = data
|
||||||
if trakt_url[-1:] == " ":
|
if trakt_url[-1:] == " ":
|
||||||
trakt_url = 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
|
return matched_imbd_movies, missing_imdb_movies
|
||||||
else:
|
else:
|
||||||
# No movies
|
# No movies
|
||||||
return None, None
|
return False, False
|
||||||
|
|
||||||
def trakt_get_shows(plex, data):
|
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
|
trakt_url = data
|
||||||
if trakt_url[-1:] == " ":
|
if trakt_url[-1:] == " ":
|
||||||
trakt_url = trakt_url[:-1]
|
trakt_url = trakt_url[:-1]
|
||||||
|
|
Loading…
Reference in a new issue