Merge pull request #14 from Yoruio/13-implement-external-url-config-setting-for-jellyfin

feat: Adds jellyfin external URL and timeout gh-13
This commit is contained in:
Roy Du
2022-07-16 14:30:36 -06:00
committed by GitHub
5 changed files with 39 additions and 13 deletions

View File

@@ -90,6 +90,14 @@ try:
except: except:
USE_PLEX = False USE_PLEX = False
try:
JELLYFIN_EXTERNAL_URL = config.get(BOT_SECTION, "jellyfin_external_url")
if not JELLYFIN_EXTERNAL_URL:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
except:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
print("Could not get Jellyfin external url. Defaulting to server url.")
if USE_PLEX and plex_configured: if USE_PLEX and plex_configured:
try: try:
print("Connecting to Plex......") print("Connecting to Plex......")
@@ -296,7 +304,7 @@ class app(commands.Cog):
db.save_user_jellyfin(str(after.id), username) db.save_user_jellyfin(str(after.id), username)
await asyncio.sleep(5) await asyncio.sleep(5)
await embedcustom(after, "You have been added to Jellyfin!", {'Username': username, 'Password': f"||{password}||"}) await embedcustom(after, "You have been added to Jellyfin!", {'Username': username, 'Password': f"||{password}||"})
await embedinfo(after, f"Go to {JELLYFIN_SERVER_URL} to log in!") await embedinfo(after, f"Go to {JELLYFIN_EXTERNAL_URL} to log in!")
else: else:
await embedinfo(after, 'There was an error adding this user to Jellyfin. Message Server Admin.') await embedinfo(after, 'There was an error adding this user to Jellyfin. Message Server Admin.')
jellyfin_processed = True jellyfin_processed = True

View File

@@ -12,7 +12,7 @@ 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_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id', '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_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url']
# settings # settings
Discord_bot_token = "" Discord_bot_token = ""
@@ -96,6 +96,14 @@ except:
print("Could not load Jellyfin config") print("Could not load Jellyfin config")
jellyfin_configured = False jellyfin_configured = False
try:
JELLYFIN_EXTERNAL_URL = config.get(BOT_SECTION, "jellyfin_external_url")
if not JELLYFIN_EXTERNAL_URL:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
except:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
print("Could not get Jellyfin external url. Defaulting to server url.")
# Get Jellyfin roles config # Get Jellyfin roles config
try: try:
jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles') jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles')

View File

@@ -164,12 +164,12 @@ def get_config(jellyfin_url, jellyfin_api_key):
url = f"{jellyfin_url}/System/Configuration" url = f"{jellyfin_url}/System/Configuration"
querystring = {"api_key":jellyfin_api_key} querystring = {"api_key":jellyfin_api_key}
response = requests.request("GET", url, params=querystring) response = requests.request("GET", url, params=querystring, timeout=5)
return response.json() return response.json()
def get_status(jellyfin_url, jellyfin_api_key): def get_status(jellyfin_url, jellyfin_api_key):
url = f"{jellyfin_url}/System/Configuration" url = f"{jellyfin_url}/System/Configuration"
querystring = {"api_key":jellyfin_api_key} querystring = {"api_key":jellyfin_api_key}
response = requests.request("GET", url, params=querystring) response = requests.request("GET", url, params=querystring, timeout=5)
return response.status_code return response.status_code

View File

@@ -18,11 +18,11 @@ async def embedcustom(recipient, title, fields, ephemeral=True):
async def send_info(recipient, message, ephemeral=True): async def send_info(recipient, message, ephemeral=True):
if isinstance(recipient, discord.InteractionResponse): if isinstance(recipient, discord.InteractionResponse):
await recipient.send_message(message, ephemeral=ephemeral) await recipient.send_message(message, ephemeral=ephemeral)
elif isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member): elif isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member) or isinstance(recipient, discord.Webhook):
await recipient.send(message) await recipient.send(message)
async def send_embed(recipient, embed, ephemeral=True): async def send_embed(recipient, embed, ephemeral=True):
if isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member): if isinstance(recipient, discord.User) or isinstance(recipient, discord.member.Member) or isinstance(recipient, discord.Webhook):
await recipient.send(embed=embed) await recipient.send(embed=embed)
elif isinstance(recipient, discord.InteractionResponse): elif isinstance(recipient, discord.InteractionResponse):
await recipient.send_message(embed=embed, ephemeral = ephemeral) await recipient.send_message(embed=embed, ephemeral = ephemeral)

24
run.py
View File

@@ -11,6 +11,7 @@ from app.bot.helper.confighelper import MEMBARR_VERSION, switch, Discord_bot_tok
import app.bot.helper.confighelper as confighelper 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
maxroles = 10 maxroles = 10
@@ -153,7 +154,8 @@ async def jellyrolels(interaction: discord.Interaction):
@jellyfin_commands.command(name="setup", description="Setup Jellyfin integration") @jellyfin_commands.command(name="setup", description="Setup Jellyfin integration")
@app_commands.checks.has_permissions(administrator=True) @app_commands.checks.has_permissions(administrator=True)
async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str): async def setupjelly(interaction: discord.Interaction, server_url: str, api_key: str, external_url: str = None):
await interaction.response.defer()
# get rid of training slashes # get rid of training slashes
server_url = server_url.rstrip('/') server_url = server_url.rstrip('/')
@@ -163,28 +165,36 @@ async def setupjelly(interaction: discord.Interaction, server_url: str, api_key:
pass pass
elif server_status == 401: elif server_status == 401:
# Unauthorized # Unauthorized
await embederror(interaction.response, "API key provided is invalid") await embederror(interaction.followup, "API key provided is invalid")
return return
elif server_status == 403: elif server_status == 403:
# Forbidden # Forbidden
await embederror(interaction.response, "API key provided does not have permissions") await embederror(interaction.followup, "API key provided does not have permissions")
return return
elif server_status == 404: elif server_status == 404:
# page not found # page not found
await embederror(interaction.response, "Server endpoint provided was not found") await embederror(interaction.followup, "Server endpoint provided was not found")
return return
else: else:
await embederror(interaction.response, "Unknown error occurred while connecting to server. Check Membarr logs.") await embederror(interaction.followup, "Unknown error occurred while connecting to Jellyfin. Check Membarr logs.")
except ConnectTimeout as e:
await embederror(interaction.followup, "Connection to server timed out. Check that Jellyfin is online and reachable.")
return
except Exception as e: except Exception as e:
print("Exception while testing Jellyfin connection") print("Exception while testing Jellyfin connection")
print(type(e).__name__)
print(e) print(e)
await embederror(interaction.response, "Could not connect to server. Check logs for more details.") await embederror(interaction.followup, "Unknown exception while connecting to Jellyfin. Check Membarr logs")
return return
confighelper.change_config("jellyfin_server_url", str(server_url)) confighelper.change_config("jellyfin_server_url", str(server_url))
confighelper.change_config("jellyfin_api_key", str(api_key)) confighelper.change_config("jellyfin_api_key", str(api_key))
if external_url is not None:
confighelper.change_config("jellyfin_external_url", str(external_url))
else:
confighelper.change_config("jellyfin_external_url", "")
print("Jellyfin server URL and API key updated. Restarting bot.") print("Jellyfin server URL and API key updated. Restarting bot.")
await interaction.response.send_message("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True) await interaction.followup.send("Jellyfin server URL and API key updated. Restarting bot.", ephemeral=True)
await reload() await reload()
print("Bot has been restarted. Give it a few seconds.") print("Bot has been restarted. Give it a few seconds.")