Skip to content

Commit

Permalink
Merge pull request #243 from glensc/trakt-login-command
Browse files Browse the repository at this point in the history
  • Loading branch information
glensc authored Apr 16, 2021
2 parents 6049ecd + 7fbd3b4 commit 1b34061
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
2 changes: 2 additions & 0 deletions plex_trakt_sync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from plex_trakt_sync.commands.clear_collections import clear_collections
from plex_trakt_sync.commands.inspect import inspect
from plex_trakt_sync.commands.sync import sync
from plex_trakt_sync.commands.trakt_login import trakt_login
from plex_trakt_sync.commands.watch import watch


Expand All @@ -19,4 +20,5 @@ def cli(ctx):
cli.add_command(clear_collections)
cli.add_command(inspect)
cli.add_command(sync)
cli.add_command(trakt_login)
cli.add_command(watch)
50 changes: 50 additions & 0 deletions plex_trakt_sync/commands/trakt_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from json import JSONDecodeError

import click
import trakt.core
from trakt.errors import ForbiddenException

from plex_trakt_sync.config import CONFIG
from plex_trakt_sync.style import title, success, error, prompt
from plex_trakt_sync.trakt_api import TraktApi

PROMPT_TRAKT_CLIENT_ID = prompt("Please enter your client id")
PROMPT_TRAKT_CLIENT_SECRET = prompt("Please enter your client secret")
TRAKT_LOGIN_SUCCESS = success(
"You are now logged into Trakt. "
"Your Trakt credentials have been added in .env and .pytrakt.json files."
)


def trakt_authenticate():
click.echo(title("Sign in to Trakt"))
trakt.core.AUTH_METHOD = trakt.core.DEVICE_AUTH

click.echo("If you do not have a client ID and secret. Please visit the following url to create them.")
click.echo(" https://trakt.tv/oauth/applications")
click.echo("")

while True:
client_id = click.prompt(PROMPT_TRAKT_CLIENT_ID, type=str)
client_secret = click.prompt(PROMPT_TRAKT_CLIENT_SECRET, type=str)

try:
return trakt.init(client_id=client_id, client_secret=client_secret, store=True)
except (ForbiddenException, JSONDecodeError) as e:
click.echo(error(f"Log in to Trakt failed: {e}, Try again."))


@click.command()
def trakt_login():
"""
Log in to Trakt Account to obtain Access Token.
"""

trakt_authenticate()
api = TraktApi()
user = api.me.username

CONFIG["TRAKT_USERNAME"] = user
CONFIG.save()

click.echo(TRAKT_LOGIN_SUCCESS)

0 comments on commit 1b34061

Please sign in to comment.