-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #243 from glensc/trakt-login-command
- Loading branch information
Showing
2 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |