Merge pull request #17 from Yoruio/16-store-plex-api-token-instead-of-login-info

feat: gh-16 store plex token instead of login
This commit is contained in:
Roy Du
2022-07-20 15:33:07 -07:00
committed by GitHub
3 changed files with 74 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ from discord.ext import commands
from discord import app_commands from discord import app_commands
import asyncio import asyncio
from plexapi.myplex import MyPlexAccount from plexapi.myplex import MyPlexAccount
from plexapi.server import PlexServer
import app.bot.helper.db as db import app.bot.helper.db as db
import app.bot.helper.plexhelper as plexhelper import app.bot.helper.plexhelper as plexhelper
import app.bot.helper.jellyfinhelper as jelly import app.bot.helper.jellyfinhelper as jelly
@@ -22,13 +23,24 @@ jellyfin_configured = True
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
plex_token_configured = True
try:
PLEX_TOKEN = config.get(BOT_SECTION, 'plex_token')
PLEX_BASE_URL = config.get(BOT_SECTION, 'plex_base_url')
except:
print("No Plex auth token details found")
plex_token_configured = False
# Get Plex config # Get Plex config
try: try:
PLEXUSER = config.get(BOT_SECTION, 'plex_user') PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass') PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name') PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
except: except:
plex_configured = False print("No Plex login info found")
if not plex_token_configured:
print("Could not load plex config")
plex_configured = False
# Get Plex roles config # Get Plex roles config
try: try:
@@ -101,12 +113,17 @@ except:
if USE_PLEX and plex_configured: if USE_PLEX and plex_configured:
try: try:
print("Connecting to Plex......") print("Connecting to Plex......")
account = MyPlexAccount(PLEXUSER, PLEXPASS) if plex_token_configured and PLEX_TOKEN and PLEX_BASE_URL:
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance print("Using Plex auth token")
plex = PlexServer(PLEX_BASE_URL, PLEX_TOKEN)
else:
print("Using Plex login info")
account = MyPlexAccount(PLEXUSER, PLEXPASS)
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance
print('Logged into plex!') print('Logged into plex!')
except Exception as e: except Exception as e:
# probably rate limited. # probably rate limited.
print('Error with plex login. Please check username and password and Plex server name or setup plex in the bot. If you have restarted the bot multiple times recently, this is most likely due to being ratelimited on the Plex API. Try again in 10 minutes.') print('Error with plex login. Please check Plex authentication details. If you have restarted the bot multiple times recently, this is most likely due to being ratelimited on the Plex API. Try again in 10 minutes.')
print(f'Error: {e}') print(f'Error: {e}')
else: else:
print(f"Plex {'disabled' if not USE_PLEX else 'not configured'}. Skipping Plex login.") print(f"Plex {'disabled' if not USE_PLEX else 'not configured'}. Skipping Plex login.")

View File

@@ -9,8 +9,8 @@ MEMBARR_VERSION = 1.1
config = configparser.ConfigParser() config = configparser.ConfigParser()
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', 'plex_token',
'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id', 'plex_base_url', 'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles', 'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles',
'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url'] 'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url']
@@ -20,6 +20,8 @@ plex_roles = None
PLEXUSER = "" PLEXUSER = ""
PLEXPASS = "" PLEXPASS = ""
PLEX_SERVER_NAME = "" PLEX_SERVER_NAME = ""
PLEX_TOKEN = ""
PLEX_BASE_URL = ""
Plex_LIBS = None Plex_LIBS = None
JELLYFIN_SERVER_URL = "" JELLYFIN_SERVER_URL = ""
JELLYFIN_API_KEY = "" JELLYFIN_API_KEY = ""
@@ -57,14 +59,24 @@ if not (path.exists(CONFIG_PATH)):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(CONFIG_PATH) config.read(CONFIG_PATH)
plex_token_configured = True
try:
PLEX_TOKEN = config.get(BOT_SECTION, 'plex_token')
PLEX_BASE_URL = config.get(BOT_SECTION, 'plex_base_url')
except:
print("No Plex auth token details found")
plex_token_configured = False
# Get Plex config # Get Plex config
try: try:
PLEXUSER = config.get(BOT_SECTION, 'plex_user') PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass') PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name') PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
except: except:
print("Could not load plex config") print("No Plex login info found")
plex_configured = False if not plex_token_configured:
print("Could not load plex config")
plex_configured = False
# Get Plex roles config # Get Plex roles config
try: try:

44
run.py
View File

@@ -12,6 +12,7 @@ import app.bot.helper.confighelper as confighelper
import app.bot.helper.jellyfinhelper as jelly import app.bot.helper.jellyfinhelper as jelly
from app.bot.helper.message import * from app.bot.helper.message import *
from requests import ConnectTimeout from requests import ConnectTimeout
from plexapi.myplex import MyPlexAccount
maxroles = 10 maxroles = 10
@@ -104,13 +105,42 @@ async def plexrolels(interaction: discord.Interaction):
@plex_commands.command(name="setup", description="Setup Plex integration") @plex_commands.command(name="setup", description="Setup Plex integration")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str): async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str, base_url: str = "", save_token: bool = True):
confighelper.change_config("plex_user", str(username)) await interaction.response.defer()
confighelper.change_config("plex_pass", str(password)) try:
confighelper.change_config("plex_server_name", str(server_name)) account = MyPlexAccount(username, password)
print("Plex username, password, and plex server name updated. Restarting bot.") plex = account.resource(server_name).connect()
await interaction.response.send_message( except Exception as e:
"Plex username, password, and plex server name updated. Restarting bot. Please wait.\n" + if str(e).startswith("(429)"):
await embederror(interaction.followup, "Too many requests. Please try again later.")
return
await embederror(interaction.followup, "Could not connect to Plex server. Please check your credentials.")
return
if (save_token):
# Save new config entries
confighelper.change_config("plex_base_url", plex._baseurl if base_url == "" else base_url)
confighelper.change_config("plex_token", plex._token)
# Delete old config entries
confighelper.change_config("plex_user", "")
confighelper.change_config("plex_pass", "")
confighelper.change_config("plex_server_name", "")
else:
# Save new config entries
confighelper.change_config("plex_user", username)
confighelper.change_config("plex_pass", password)
confighelper.change_config("plex_server_name", server_name)
# Delete old config entries
confighelper.change_config("plex_base_url", "")
confighelper.change_config("plex_token", "")
print("Plex authentication details updated. Restarting bot.")
await interaction.followup.send(
"Plex authentication details updated. Restarting bot. Please wait.\n" +
"Please check logs and make sure you see the line: `Logged into plex`. If not run this command again and make sure you enter the right values.", "Please check logs and make sure you see the line: `Logged into plex`. If not run this command again and make sure you enter the right values.",
ephemeral=True ephemeral=True
) )