added Emby plugin

This commit is contained in:
John Andrews
2022-04-21 17:59:30 +12:00
parent e3ac0a0de1
commit 6904a9e1a8
22 changed files with 254 additions and 1 deletions

2
.gitignore vendored
View File

@@ -13,3 +13,5 @@ deploy/**
DiscordNodes/settings.json DiscordNodes/settings.json
Gotify/settings.json Gotify/settings.json
Plex/settings.json Plex/settings.json
Emby/settings.invalid.json
Emby/settings.json

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
Emby/Emby.csproj Normal file

Binary file not shown.

25
Emby/Emby.en.json Normal file
View File

@@ -0,0 +1,25 @@
{
"Plugins": {
"Emby": {
"Description": "A plugin that allows you to communicate with Emby.",
"Fields": {
"ServerUrl": "Server",
"ServerUrl-Placeholder": "http://localhost:8096/",
"ServerUrl-Help": "The URL of the Emby server",
"AccessToken": "Access Token",
"AccessToken-Help": "An Emby API access token"
}
}
},
"Flow": {
"Parts": {
"EmbyUpdater": {
"Outputs": {
"1": "Emby update request sent",
"2": "Emby update request failed to send"
},
"Description": "Sends a message to a Emby server to update the library."
}
}
}
}

6
Emby/ExtensionMethods.cs Normal file
View File

@@ -0,0 +1,6 @@
namespace FileFlows.Emby;
internal static class ExtensionMethods
{
public static string? EmptyAsNull(this string str) => str == string.Empty ? null : str;
}

3
Emby/GlobalUsings.cs Normal file
View File

@@ -0,0 +1,3 @@
global using System;
global using System.Text;
global using FileFlows.Plugin;

View File

@@ -0,0 +1,86 @@
using System.Text.RegularExpressions;
namespace FileFlows.Emby.MediaManagement;
public class EmbyUpdater: Node
{
public override int Inputs => 1;
public override int Outputs => 2;
public override FlowElementType Type => FlowElementType.Process;
public override string Icon => "fas fa-paper-plane";
public override int Execute(NodeParameters args)
{
var settings = args.GetPluginSettings<PluginSettings>();
if (string.IsNullOrWhiteSpace(settings?.AccessToken))
{
args.Logger?.WLog("No access token set");
return 2;
}
if (string.IsNullOrWhiteSpace(settings?.ServerUrl))
{
args.Logger?.WLog("No server URL set");
return 2;
}
// get the path
string path = args.WorkingFile;
path = args.UnMapPath(path);
if (args.IsDirectory == false)
{
bool windows = path.StartsWith("\\") || Regex.IsMatch(path, @"^[a-zA-Z]:\\");
string pathSeparator = windows ? "\\" : "/";
path = path.Substring(0, path.LastIndexOf(pathSeparator));
}
string url = settings.ServerUrl;
if (url.EndsWith("/") == false)
url += "/";
url += "Library/Media/Updated";
string body = System.Text.Json.JsonSerializer.Serialize(new {
Updates = new object [] { new { Path = path } }
});
using var httpClient = new HttpClient();
var updateResponse = GetWebRequest(httpClient, url, settings.AccessToken, body);
if (updateResponse.success == false)
{
if(string.IsNullOrWhiteSpace(updateResponse.body) == false)
args.Logger?.WLog("Failed to update Emby:" + updateResponse.body);
return 2;
}
return 1;
}
private Func<HttpClient, string, string, string, (bool success, string body)> _GetWebRequest;
internal Func<HttpClient, string, string, string, (bool success, string body)> GetWebRequest
{
get
{
if(_GetWebRequest == null)
{
_GetWebRequest = (HttpClient client, string url, string accessToken, string json) =>
{
try
{
var content = new StringContent(json, Encoding.UTF8, "application/json");
client.DefaultRequestHeaders.Add("X-MediaBrowser-Token", accessToken);
var response = client.PostAsync(url, content).Result;
string respnoseBody = response.Content.ReadAsStringAsync().Result;
if(response.IsSuccessStatusCode)
return (response.IsSuccessStatusCode, respnoseBody);
return (response.IsSuccessStatusCode, respnoseBody?.EmptyAsNull() ?? response.ReasonPhrase);
}
catch(Exception ex)
{
return (false, ex.Message);
}
};
}
return _GetWebRequest;
}
}
}

11
Emby/Plugin.cs Normal file
View File

@@ -0,0 +1,11 @@
namespace FileFlows.Emby;
public class Plugin : FileFlows.Plugin.IPlugin
{
public string Name => "Emby";
public string MinimumVersion => "0.5.2.690";
public void Init()
{
}
}

17
Emby/PluginSettings.cs Normal file
View File

@@ -0,0 +1,17 @@
namespace FileFlows.Emby
{
using FileFlows.Plugin;
using FileFlows.Plugin.Attributes;
using System.ComponentModel.DataAnnotations;
public class PluginSettings:IPluginSettings
{
[Text(1)]
[Required]
public string ServerUrl { get; set; }
[Text(2)]
[Required]
public string AccessToken { get; set; }
}
}

38
Emby/Tests/EmbyTests.cs Normal file
View File

@@ -0,0 +1,38 @@
#if(DEBUG)
using FileFlows.Emby.MediaManagement;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace FileFlows.Emby.Tests;
[TestClass]
public class EmbyTests
{
[TestMethod]
public void Emby_Basic()
{
var args = new NodeParameters(@"/media/movies/Citizen Kane (1941)/Citizen Kane (1941).mp4", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.json");
};
var node = new EmbyUpdater();
Assert.AreEqual(1, node.Execute(args));
}
[TestMethod]
public void Emby_Fail()
{
var args = new NodeParameters(@"/media/unknownmovies/The Batman (2022)/The Batman.mkv", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) =>
{
return File.ReadAllText("../../../settings.invalid.json");
};
var node = new EmbyUpdater();
Assert.AreEqual(2, node.Execute(args));
}
}
#endif

59
Emby/Tests/TestLogger.cs Normal file
View File

@@ -0,0 +1,59 @@
#if(DEBUG)
namespace FileFlows.Emby.Tests;
using FileFlows.Plugin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
internal class TestLogger : ILogger
{
private List<string> Messages = new List<string>();
public void DLog(params object[] args) => Log("DBUG", args);
public void ELog(params object[] args) => Log("ERRR", args);
public void ILog(params object[] args) => Log("INFO", args);
public void WLog(params object[] args) => Log("WARN", args);
private void Log(string type, object[] args)
{
if (args == null || args.Length == 0)
return;
string message = type + " -> " +
string.Join(", ", args.Select(x =>
x == null ? "null" :
x.GetType().IsPrimitive || x is string ? x.ToString() :
System.Text.Json.JsonSerializer.Serialize(x)));
Messages.Add(message);
}
public bool Contains(string message)
{
if (string.IsNullOrWhiteSpace(message))
return false;
string log = string.Join(Environment.NewLine, Messages);
return log.Contains(message);
}
public override string ToString()
{
return String.Join(Environment.NewLine, this.Messages.ToArray());
}
public string GetTail(int length = 50)
{
if (length <= 0)
length = 50;
if (Messages.Count <= length)
return string.Join(Environment.NewLine, Messages);
return string.Join(Environment.NewLine, Messages.TakeLast(length));
}
}
#endif

View File

@@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordNodes", "DiscordNode
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plex", "Plex\Plex.csproj", "{4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Plex", "Plex\Plex.csproj", "{4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}"
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Emby", "Emby\Emby.csproj", "{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
{4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Debug|Any CPU.Build.0 = Debug|Any CPU {4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Release|Any CPU.ActiveCfg = Release|Any CPU {4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Release|Any CPU.Build.0 = Release|Any CPU {4127A3CC-8BCE-41AE-BDE4-6FA75EF2D8A1}.Release|Any CPU.Build.0 = Release|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C73A4E1F-E1CD-4803-BE6D-886DCBE18727}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -11,7 +11,7 @@ public class PlexTests
[TestMethod] [TestMethod]
public void Plex_Basic() public void Plex_Basic()
{ {
var args = new NodeParameters(@"/media/movies/The Batman (2022)/The Batman.mkv", new TestLogger(), false, string.Empty); var args = new NodeParameters(@"/media/movies/Citizen Kane (1941)/Citizen Kane (1941).mp4", new TestLogger(), false, string.Empty);
args.GetPluginSettingsJson = (string input) => args.GetPluginSettingsJson = (string input) =>
{ {
return File.ReadAllText("../../../settings.json"); return File.ReadAllText("../../../settings.json");

Binary file not shown.