Skip to content

Commit 58c1d92

Browse files
committed
Parse nfo file for title, overview and air date. if available.
1 parent 003c1c5 commit 58c1d92

File tree

4 files changed

+102
-4
lines changed

4 files changed

+102
-4
lines changed

CustomMetadataDB/CustomMetadataDB.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>net8.0</TargetFramework>
44
<RootNamespace>CustomMetadataDB</RootNamespace>
5-
<Version>1.2.0.2</Version>
5+
<Version>1.2.1.0</Version>
66
<FileVersion>$(Version)</FileVersion>
77
<AssemblyVersion>$(Version)</AssemblyVersion>
88
<IsPackable>true</IsPackable>

CustomMetadataDB/Helpers/Utils.cs

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
using System.Security.Cryptography;
1313
using System.Text;
1414
using System.IO;
15+
using System.Linq;
16+
using System.Xml.Linq;
1517

1618
namespace CustomMetadataDB.Helpers;
1719

@@ -233,7 +235,7 @@ public static EpisodeInfo FileToInfo(string file)
233235
}
234236

235237

236-
public static MetadataResult<Episode> ToEpisode(EpisodeInfo data)
238+
public static MetadataResult<Episode> ToEpisode(EpisodeInfo data, string file = null)
237239
{
238240
if (data.Path == "")
239241
{
@@ -272,9 +274,105 @@ public static MetadataResult<Episode> ToEpisode(EpisodeInfo data)
272274
item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, id);
273275
}
274276

277+
if (!string.IsNullOrEmpty(file))
278+
{
279+
var nfoData = GetEpisodeNfo(file);
280+
if (nfoData.TryGetValue("title", out var nfo_title))
281+
{
282+
item.Name = nfo_title;
283+
}
284+
285+
if (nfoData.TryGetValue("overview", out var nfo_overview))
286+
{
287+
item.Overview = nfo_overview;
288+
}
289+
290+
if (nfoData.TryGetValue("aired", out var aired) && DateTime.TryParse(aired, out var date))
291+
{
292+
item.PremiereDate = date;
293+
item.ProductionYear = date.Year;
294+
}
295+
}
296+
275297
return new() { HasMetadata = true, Item = item };
276298
}
277299

278300
private static MetadataResult<Series> ErrorOut() => new() { HasMetadata = false, Item = new Series() };
279301
private static MetadataResult<Episode> ErrorOutEpisode() => new() { HasMetadata = false, Item = new Episode() };
302+
303+
public static Dictionary<string, string> GetEpisodeNfo(string path)
304+
{
305+
var info = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
306+
307+
if (string.IsNullOrWhiteSpace(path))
308+
{
309+
return info;
310+
}
311+
312+
try
313+
{
314+
string nfoPath = Path.ChangeExtension(path, ".nfo");
315+
316+
if (!File.Exists(nfoPath))
317+
{
318+
return info;
319+
}
320+
321+
Logger?.LogInformation($"GetEpisodeNfo() - nfoFile: {nfoPath}");
322+
323+
string rawXml = File.ReadAllText(nfoPath);
324+
325+
// Fix invalid ampersands
326+
rawXml = Regex.Replace(rawXml, @"&(?![A-Za-z]+[0-9]*;|#[0-9]+;|#x[0-9a-fA-F]+;)", "&amp;");
327+
// Remove junk self-closing tags on their own lines
328+
rawXml = Regex.Replace(rawXml, @"^\s*<.*/>\s*$", "", RegexOptions.Multiline);
329+
330+
XElement root;
331+
try
332+
{
333+
var doc = XDocument.Parse(rawXml);
334+
root = doc.Descendants("episodedetails").FirstOrDefault();
335+
if (root == null)
336+
{
337+
Logger?.LogError($"GetEpisodeNfo() - No <episodedetails> found in: {nfoPath}");
338+
return info;
339+
}
340+
}
341+
catch (Exception ex)
342+
{
343+
Logger?.LogError($"GetEpisodeNfo() - Failed to parse XML: {ex.Message}");
344+
return info;
345+
}
346+
347+
var keys = new List<(string xmlKey, string infoKey)>
348+
{
349+
("title", "title"),
350+
("season", "season"),
351+
("episode", "episode"),
352+
("aired", "aired"),
353+
("plot", "overview")
354+
};
355+
356+
foreach (var (xmlKey, infoKey) in keys)
357+
{
358+
var element = root.Element(xmlKey);
359+
if (element != null)
360+
{
361+
string value = element.Value.Trim();
362+
if (!string.IsNullOrEmpty(value))
363+
{
364+
info[infoKey] = value;
365+
}
366+
}
367+
}
368+
369+
return info;
370+
}
371+
catch (Exception ex)
372+
{
373+
Logger?.LogError($"GetEpisodeNfo() - Exception: {ex.Message}");
374+
Logger?.LogError(ex.ToString());
375+
return info;
376+
}
377+
}
280378
}

CustomMetadataDB/Provider/EpisodeProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Task<MetadataResult<Episode>> GetMetadata(EpisodeInfo info, CancellationT
6565
return Task.FromResult(result);
6666
}
6767

68-
return Task.FromResult(Utils.ToEpisode(item));
68+
return Task.FromResult(Utils.ToEpisode(item, info.Path));
6969
}
7070

7171
public Task<IEnumerable<RemoteSearchResult>> GetSearchResults(EpisodeInfo searchInfo, CancellationToken cancellationToken) => throw new NotImplementedException();

build.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
name: "Custom metadata agent db"
33
guid: "83b77e24-9fce-4ee0-a794-73fdfa972e66"
4-
version: "1.2.0.2"
4+
version: "1.2.1.0"
55
targetAbi: "10.7.0.0"
66
owner: "arabcoders"
77
overview: "A Custom metadata agent."

0 commit comments

Comments
 (0)