Skip to content

Commit

Permalink
Merge pull request #2126 from glensc/reuse-token
Browse files Browse the repository at this point in the history
Feature: Re-use token for plex-login with --use-token flag
  • Loading branch information
glensc authored Jan 1, 2025
2 parents 1c7a057 + 46e3485 commit 59c3f9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
5 changes: 5 additions & 0 deletions plextraktsync/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ def env_plex_username():
help="Plex password",
default=lambda: environ.get("PLEX_PASSWORD", None),
)
@click.option(
"--use-token",
is_flag=True,
help="Use saved plex token if available",
)
def plex_login():
"""
Log in to Plex Account to obtain Access Token. Optionally can use managed user on servers that you own.
Expand Down
28 changes: 19 additions & 9 deletions plextraktsync/commands/plex_login.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,19 @@ def server_urls(server: MyPlexResource):
yield local_url()


def myplex_login(username, password):
def myplex_login(username: str, password: str, token=None, code=None):
while True:
username = Prompt.ask(PROMPT_PLEX_USERNAME, default=username)
password = Prompt.ask(PROMPT_PLEX_PASSWORD, password=True, default=password, show_default=False)
code = Prompt.ask(PROMPT_PLEX_CODE)
if token:
print("Using existing token to login to Plex")
if not token:
username = Prompt.ask(PROMPT_PLEX_USERNAME, default=username)
password = Prompt.ask(PROMPT_PLEX_PASSWORD, password=True, default=password, show_default=False)
code = Prompt.ask(PROMPT_PLEX_CODE)
try:
return MyPlexAccount(username=username, password=password, code=code)
return MyPlexAccount(username=username, password=password, code=code, token=token)
except Unauthorized as e:
if token:
raise e
print(error(f"Log in to Plex failed: '{e}', Try again."), highlight=False)
except BadRequest as e:
raise ClickException(f"Log in to Plex failed: {e}")
Expand Down Expand Up @@ -185,16 +190,21 @@ def plex_login_autoconfig():
login(username, password)


def plex_login(username, password):
login(username, password)
def plex_login(username, password, use_token: bool):
token = None
if use_token:
config = factory.config
token = config["PLEX_ACCOUNT_TOKEN"]

login(username, password, token)


def login(username: str, password: str):
def login(username: str, password: str, token=None):
if factory.has_plex_token:
if not Confirm.ask(PROMPT_PLEX_RELOGIN, default=True):
return

account = myplex_login(username, password)
account = myplex_login(username, password, token)
print(
Panel.fit(
"Login to MyPlex was successful",
Expand Down

0 comments on commit 59c3f9e

Please sign in to comment.