feat: gh-16 store plex token instead of login

- Adds ability to store plex token and server base url instead of login
 username / password
 - Sets plex token storage as default plex storage / auth method
(This also fixes ratelimit, as no login occurs with auth token)

task: gh-16
This commit is contained in:
Yoruio
2022-07-20 15:11:24 -07:00
parent f3f585d13d
commit 1006341a53
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
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,13 +23,24 @@ 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:
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
try:
@@ -101,12 +113,17 @@ except:
if USE_PLEX and plex_configured:
try:
print("Connecting to Plex......")
account = MyPlexAccount(PLEXUSER, PLEXPASS)
plex = account.resource(PLEX_SERVER_NAME).connect() # returns a PlexServer instance
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.")

View File

@@ -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,14 +59,24 @@ 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("Could not load plex config")
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
try: