Merge pull request #18 from Yoruio/experimental
Store plex tokens instead of auth details
This commit is contained in:
@@ -6,6 +6,7 @@ from discord.ext import commands
|
||||
from discord import app_commands
|
||||
import asyncio
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
from plexapi.server import PlexServer
|
||||
import app.bot.helper.db as db
|
||||
import app.bot.helper.plexhelper as plexhelper
|
||||
import app.bot.helper.jellyfinhelper as jelly
|
||||
@@ -22,12 +23,23 @@ jellyfin_configured = True
|
||||
config = configparser.ConfigParser()
|
||||
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
|
||||
try:
|
||||
PLEXUSER = config.get(BOT_SECTION, 'plex_user')
|
||||
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
|
||||
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
|
||||
except:
|
||||
print("No Plex login info found")
|
||||
if not plex_token_configured:
|
||||
print("Could not load plex config")
|
||||
plex_configured = False
|
||||
|
||||
# Get Plex roles config
|
||||
@@ -101,12 +113,17 @@ except:
|
||||
if USE_PLEX and plex_configured:
|
||||
try:
|
||||
print("Connecting to Plex......")
|
||||
if plex_token_configured and PLEX_TOKEN and PLEX_BASE_URL:
|
||||
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!')
|
||||
except Exception as e:
|
||||
# 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}')
|
||||
else:
|
||||
print(f"Plex {'disabled' if not USE_PLEX else 'not configured'}. Skipping Plex login.")
|
||||
|
||||
@@ -9,8 +9,8 @@ MEMBARR_VERSION = 1.1
|
||||
|
||||
config = configparser.ConfigParser()
|
||||
|
||||
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass',
|
||||
'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
|
||||
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', 'plex_token',
|
||||
'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',
|
||||
'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url']
|
||||
|
||||
@@ -20,6 +20,8 @@ plex_roles = None
|
||||
PLEXUSER = ""
|
||||
PLEXPASS = ""
|
||||
PLEX_SERVER_NAME = ""
|
||||
PLEX_TOKEN = ""
|
||||
PLEX_BASE_URL = ""
|
||||
Plex_LIBS = None
|
||||
JELLYFIN_SERVER_URL = ""
|
||||
JELLYFIN_API_KEY = ""
|
||||
@@ -57,12 +59,22 @@ if not (path.exists(CONFIG_PATH)):
|
||||
config = configparser.ConfigParser()
|
||||
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
|
||||
try:
|
||||
PLEXUSER = config.get(BOT_SECTION, 'plex_user')
|
||||
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
|
||||
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
|
||||
except:
|
||||
print("No Plex login info found")
|
||||
if not plex_token_configured:
|
||||
print("Could not load plex config")
|
||||
plex_configured = False
|
||||
|
||||
|
||||
44
run.py
44
run.py
@@ -12,6 +12,7 @@ import app.bot.helper.confighelper as confighelper
|
||||
import app.bot.helper.jellyfinhelper as jelly
|
||||
from app.bot.helper.message import *
|
||||
from requests import ConnectTimeout
|
||||
from plexapi.myplex import MyPlexAccount
|
||||
|
||||
maxroles = 10
|
||||
|
||||
@@ -104,13 +105,42 @@ async def plexrolels(interaction: discord.Interaction):
|
||||
|
||||
@plex_commands.command(name="setup", description="Setup Plex integration")
|
||||
@app_commands.checks.has_permissions(administrator=True)
|
||||
async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str):
|
||||
confighelper.change_config("plex_user", str(username))
|
||||
confighelper.change_config("plex_pass", str(password))
|
||||
confighelper.change_config("plex_server_name", str(server_name))
|
||||
print("Plex username, password, and plex server name updated. Restarting bot.")
|
||||
await interaction.response.send_message(
|
||||
"Plex username, password, and plex server name updated. Restarting bot. Please wait.\n" +
|
||||
async def setupplex(interaction: discord.Interaction, username: str, password: str, server_name: str, base_url: str = "", save_token: bool = True):
|
||||
await interaction.response.defer()
|
||||
try:
|
||||
account = MyPlexAccount(username, password)
|
||||
plex = account.resource(server_name).connect()
|
||||
except Exception as e:
|
||||
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.",
|
||||
ephemeral=True
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user