Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

To do: refresh Trakt token on each run #50

Closed
burkasaurusrex opened this issue Oct 16, 2020 · 19 comments
Closed

To do: refresh Trakt token on each run #50

burkasaurusrex opened this issue Oct 16, 2020 · 19 comments

Comments

@burkasaurusrex
Copy link

Trakt provides the ability to refresh the Trakt token. Seems like the script should refresh the token on each run or on some interval (maybe set the interval in the yaml with a logical default) to avoid auth silently failing? I've come across the issue myself given that Trakt tokens expire every 3 months.

Looks like #14 and #48 are probably both caused by this.

@meisnate12
Copy link

at around line 216 in config_tools.py I have this

            def check_trakt (auth):
                try:
                    #TODO: Validate Trakt Connection
                    return True
                except:
                    return False

I was having problems figuring out a way to validate the credentials. If we can test right here to see if it fails somehow it should try to reauthenticate on start-up. If you have any idea how to validate it we should be good to go

@burkasaurusrex
Copy link
Author

burkasaurusrex commented Oct 16, 2020

I would probably test by retrieving the user's settings? As long as that request returns a 200 instead of a 403, then auth should work. Here's the full list of status codes.

I don't know trakt.py but seems like Trakt['users/settings'].get() or Trakt['users'].settings() or something might be workable. Not sure if there's a way to force trakt.py to surface http status codes or not.

@burkasaurusrex
Copy link
Author

Or I guess you wouldn't have to worry about the http status codes - you could just test whether that API call return an expected settings object or not.

@mza921
Copy link
Owner

mza921 commented Oct 17, 2020

I was using https://github.com/fuzeman/trakt.py/blob/master/tests/oauth/test_oauth.py as a reference for this and thought the refresh=true from

with Trakt.configuration.oauth.from_response(authorization, refresh=True):
would have covered it. Looks like I'm missing something.

@meisnate12
Copy link

this can be closed now

@burkasaurusrex
Copy link
Author

Not sure this is ready to be closed? I'll take a look this week but basically the script would pass the refresh_token back to Trakt to get a renewed authentication. The renewed authentication would need to overwrite the existing YAML authentication.

@meisnate12
Copy link

ok I see what you're saying I thought you meant us telling the program to validate and refresh on startup I can look into this cause it sounds way better lol

@mza921
Copy link
Owner

mza921 commented Nov 9, 2020

I've tested the token refresh and have it working in a different branch. One problem I encountered is that the refresh token can only be used once, so if multiple config files share the same tokens, the other config files break. To make it worse, if execution is automated with --update, the user might not notice that the script is failing for the particular config file (and will probably run into #102). This makes a good case for #78 and separating the authorization configs.

One workaround is for each config file to have its own trakt tokens.

@meisnate12
Copy link

I like the format I have in this comment and think being able to run multiple config files would be beneficial

@burkasaurusrex
Copy link
Author

For now, what about just creating a setting on whether to refresh the token or not?

Agreed that we need to find a better way to separate auth from config. It's going to be tricky though since there are so many settings that may be different for each set of collections. For example, in your comment @meisnate12, how would I set one set of collections to sync while the other set appends?

@meisnate12
Copy link

I was thinking of having sync_mode net setable at the global level, library level, and collection level

@mza921
Copy link
Owner

mza921 commented Nov 10, 2020

config.yml

plex:
  token: ###################
  url: http://192.168.1.5:32400
radarr:
  url: http://192.168.1.5:7878/radarr/
  token: ###########################
  quality_profile_id: 4
  root_folder_path: /mnt/user/PlexMedia/movies
  add_movie: false
  search_movie: false
tmdb:
  apikey: ############################
  language: en
tautulli:
  url: http://192.168.1.5:8181
  apikey: ############################
trakt:
  client_id: ############################
  client_secret: ############################
  # Below is filled in automatically when the script is run
  authorization:
    access_token:
    token_type:
    expires_in:
    refresh_token:
    scope:
    created_at:
image_server:
  poster_directory: ../config/posters
  background_directory: ../config/backgrounds
  image_directory: ../config/images

The configuration for the collections can be in a separate directory (similar to Plex-Python-Libraries' recipes). They will either have to be called out individually as arguments or the script can just iterate through everything in the collections directory.

collections/movies.yml

collections:
  IMDb Top 250:
    imdb_list: https://www.imdb.com/search/title/?groups=top_250&count=25
    sync_mode: sync
    collection_mode: show_items
  Most Popular Movies (30 Days):
    sync_mode: sync
    collection_mode: show_items
    tautulli:
      list_type: popular
      list_days: 30
      list_size: 10
plex:
  library: Movies
  library_type: movie
  sync_mode: append

collections/movies-4k.yml

collections:
  Trakt Trending:
    trakt_trending: 30
    sync_mode: sync
  Pixar:
    studio: Pixar
    summary: A collection of Pixar movies
    sort_title: *Pixar
    content_rating: G
    collection_mode: show_items
    collection_order: alpha
plex:
  library: Movies - 4K
  library_type: movie
  sync_mode: append

collections/tv.yml

collections:
  Trakt Trending:
    trakt_trending: 30
    sync_mode: sync
plex:
  library: TV Shows
  library_type: show
  sync_mode: append

@meisnate12
Copy link

To make it easier to find the library files I was saying we have the settings all in the config file and just the collections in their own like below. This will let us not have to do so much checking in the file system of each file to see if it is a collection file. We can just check and see if the file the user specified exists vs having to check all the files around the program and it allows users to put library files wherever they want.

config.yml

plex:
  libraries:
    Movies:    #Library Name
      library_config_path: /movie_collection.yml
      library_type: movie
      sync_mode: append
    Movies - 4K
      library_config_path: /4kmovie_collection.yml
      library_type: movie
      sync_mode: append
    TV Shows:    #Library Name
      library_config_path: /show_collection.yml
      library_type: show
      sync_mode: append
  token: ###################
  url: http://192.168.1.5:32400
radarr:
  url: http://192.168.1.5:7878/radarr/
  token: ###########################
  quality_profile_id: 4
  root_folder_path: /mnt/user/PlexMedia/movies
  add_movie: false
  search_movie: false
tmdb:
  apikey: ############################
  language: en
tautulli:
  url: http://192.168.1.5:8181
  apikey: ############################
trakt:
  client_id: ############################
  client_secret: ############################
  # Below is filled in automatically when the script is run
  authorization:
    access_token:
    token_type:
    expires_in:
    refresh_token:
    scope:
    created_at:
image_server:
  poster_directory: ../config/posters
  background_directory: ../config/backgrounds
  image_directory: ../config/images

movie_collection.yml

collections:
  IMDb Top 250:
    imdb_list: https://www.imdb.com/search/title/?groups=top_250&count=25
    sync_mode: sync
    collection_mode: show_items
  Most Popular Movies (30 Days):
    sync_mode: sync
    collection_mode: show_items
    tautulli:
      list_type: popular
      list_days: 30
      list_size: 10

4kmovie_collection.yml

collections:
  Trakt Trending:
    trakt_trending: 30
    sync_mode: sync
  Pixar:
    studio: Pixar
    summary: A collection of Pixar movies
    sort_title: *Pixar
    content_rating: G
    collection_mode: show_items
    collection_order: alpha

show_collection.yml

collections:
  Trakt Trending:
    trakt_trending: 30
    sync_mode: sync

@mza921
Copy link
Owner

mza921 commented Nov 10, 2020

I think the library belongs in the collection file. What if I don't want a list applied against a certain library, or if the list isn't applicable to a library type? Or what if I name a movie library and tv library the same thing? Plex allows this, but then you'd have duplicate keys in under libraries:

@meisnate12
Copy link

What if I don't want a list applied against a certain library,

im not really sure what your asking/ how its any different from now?

or if the list isn't applicable to a library type?

it wont run lists it cant same as it does now when they're in the same file

Or what if I name a movie library and tv library the same thing?

Thats a good point i guess we would have to have a show_library attribute and movie_library attribute which would also allow us to get rid of having the user specify which library_type. this is all of course if we went with my design

plex:
  movie_libraries:
    Movies:    #Library Name
      library_config_path: /movie_collection.yml
      sync_mode: append
    Movies - 4K
      library_config_path: /4kmovie_collection.yml
      sync_mode: append
  show_libraries:
    TV Shows:    #Library Name
      library_config_path: /show_collection.yml
      sync_mode: append
  token: ###################
  url: http://192.168.1.5:32400

I think the library belongs in the collection file.

If you want the plex attribute in each file thats fine but we should still have a list of files in the config like this

plex:
  libraries:
      - /movie_collection.yml
      - /4kmovie_collection.yml
      - /show_collection.yml
  token: ###################
  url: http://192.168.1.5:32400

@mza921
Copy link
Owner

mza921 commented Nov 10, 2020

What if I don't want a list applied against a certain library,

im not really sure what your asking/ how its any different from now?

or if the list isn't applicable to a library type?

it wont run lists it cant same as it does now when they're in the same file

You're right. I misunderstood.

Or what if I name a movie library and tv library the same thing?

Thats a good point i guess we would have to have a show_library attribute and movie_library attribute which would also allow us to get rid of having the user specify which library_type. this is all of course if we went with my design

plex:
  movie_libraries:
    Movies:    #Library Name
      library_config_path: /movie_collection.yml
      sync_mode: append
    Movies - 4K
      library_config_path: /4kmovie_collection.yml
      sync_mode: append
  show_libraries:
    TV Shows:    #Library Name
      library_config_path: /show_collection.yml
      sync_mode: append
  token: ###################
  url: http://192.168.1.5:32400.

That'll work. Maybe something other than library_config_path though.

I think the library belongs in the collection file.

If you want the plex attribute in each file thats fine but we should still have a list of files in the config like this

plex:
  libraries:
      - /movie_collection.yml
      - /4kmovie_collection.yml
      - /show_collection.yml
  token: ###################
  url: http://192.168.1.5:32400

No need for this any more.

@meisnate12
Copy link

we could probably just go with config path

@burkasaurusrex
Copy link
Author

What about something like this? https://www.home-assistant.io/docs/configuration/splitting_configuration/

I'm sure there's lot of other projects that had to figure out how to nest YAML as well. Just trying to avoid reinventing the wheel if possible.

@mza921
Copy link
Owner

mza921 commented Nov 14, 2020

Closing this. For config split, we can use #78

@mza921 mza921 closed this as completed Nov 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants