Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Folded DailyExtender into cmdb #4

Merged
merged 4 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CustomMetadataDB.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
@@ -72,6 +72,34 @@ public void Test_get_series_id_from_json(string dir, string expected)
Assert.Equal(expected, result);
}

[Fact]
public void Test_file_to_info()
{
var path = "/home/media/test/201012 foobar ep24 - ahmed [foo].mkv";
var item = Utils.FileToInfo(path, new DateTime(2021, 1, 1, 01, 02, 03, DateTimeKind.Utc));
Assert.Equal(110120203, item.IndexNumber);
Assert.Equal(202010, item.ParentIndexNumber);
Assert.Equal(2020, item.Year);
Assert.Equal("ep24 - ahmed", item.Name);
Assert.Equal($"{item.IndexNumber}", item.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
}

[Fact]
public void Test_ToEpisode()
{
var path = "/home/media/test/201012 foobar ep24 - ahmed [foo].mkv";
var result = Utils.ToEpisode(Utils.FileToInfo(path, new DateTime(2021, 1, 1, 01, 02, 03, DateTimeKind.Utc)));

Assert.True(result.HasMetadata);

var item = result.Item;

Assert.Equal(110120203, item.IndexNumber);
Assert.Equal(202010, item.ParentIndexNumber);
Assert.Equal("ep24 - ahmed", item.Name);
Assert.Equal($"{item.IndexNumber}", item.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
}

private static ILogger<UtilsTest> SetLogger()
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
2 changes: 1 addition & 1 deletion CustomMetadataDB/CustomMetadataDB.csproj
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>CustomMetadataDB</RootNamespace>
<Version>0.0.0.2</Version>
<Version>1.0.0.0</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>
9 changes: 9 additions & 0 deletions CustomMetadataDB/ExternalId.cs
Original file line number Diff line number Diff line change
@@ -14,4 +14,13 @@ public class SeriesExternalId : IExternalId
public ExternalIdMediaType? Type => ExternalIdMediaType.Series;
public string UrlFormatString => Plugin.Instance.Configuration.ApiRefUrl;
}

public class EpisodeExternalId : IExternalId
{
public bool Supports(IHasProviderIds item) => item is Episode;
public string ProviderName => Constants.PLUGIN_NAME;
public string Key => Constants.PLUGIN_EXTERNAL_ID;
public ExternalIdMediaType? Type => ExternalIdMediaType.Episode;
public string UrlFormatString => Plugin.Instance.Configuration.ApiRefUrl;
}
}
15 changes: 14 additions & 1 deletion CustomMetadataDB/Helpers/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
namespace CustomMetadataDB.Helpers
using System.Text.RegularExpressions;

namespace CustomMetadataDB.Helpers
{
public class Constants
{
public const string PLUGIN_NAME = "CMetadataDB";
public const string PLUGIN_EXTERNAL_ID = "cmdb";
public const string PLUGIN_DESCRIPTION = "Custom metadata agent db.";
public const string PLUGIN_GUID = "83b77e24-9fce-4ee0-a794-73fdfa972e66";

public static readonly Regex[] EPISODE_MATCHERS = {
// YY?YY(-._)MM(-._)DD -? series -? epNumber -? title
new(@"^(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})\s-?(?<series>.+?)(?<epNumber>\#(\d+)|ep(\d+)|DVD[0-9.-]+|DISC[0-9.-]+|SP[0-9.-]+|Episode\s(\d+)) -?(?<title>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
// YY?YY(-._)MM(-._)DD -? title
new(@"^(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})\s?-?(?<title>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
// title YY?YY(-._)MM(-._)DD at end of filename.
new(@"(?<title>.+?)(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
// series - YY?YY(-._)MM(-._)DD -? title
new(@"(?<series>.+?)(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})\s?-?(?<title>.+)?", RegexOptions.Compiled | RegexOptions.IgnoreCase)
};
}
}
151 changes: 149 additions & 2 deletions CustomMetadataDB/Helpers/Utils.cs
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@
using System.Text.Json.Serialization;
using Microsoft.Extensions.Logging;
using System;
using System.Text.RegularExpressions;

namespace CustomMetadataDB.Helpers
{
@@ -21,6 +22,7 @@ public class Utils
public static ILogger Logger { get; set; } = null;

public static PersonInfo CreatePerson(string name, string provider_id)

{
return new PersonInfo
{
@@ -31,7 +33,7 @@ public static PersonInfo CreatePerson(string name, string provider_id)
}
public static MetadataResult<Series> ToSeries(DTO data)
{
Logger?.LogInformation($"Processing {data}.");
Logger?.LogDebug($"Processing {data}.");

var item = new Series();

@@ -84,13 +86,158 @@ public static MetadataResult<Series> ToSeries(DTO data)
};
}

public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)
{

//-- get only the file stem
string filename = System.IO.Path.GetFileNameWithoutExtension(file);

Match matcher = null;

for (int i = 0; i < Constants.EPISODE_MATCHERS.Length; i++)
{
matcher = Constants.EPISODE_MATCHERS[i].Match(filename);
if (!matcher.Success)
{
continue;
}
break;
}

if (!matcher.Success)
{
Logger?.LogInformation($"No match found for {file}.");
return new EpisodeInfo();
}

string series = matcher.Groups["series"].Success ? matcher.Groups["series"].Value : "";
string year = matcher.Groups["year"].Success ? matcher.Groups["year"].Value : "";
year = (year.Length == 2) ? "20" + year : year;
string month = matcher.Groups["month"].Success ? matcher.Groups["month"].Value : "";
string day = matcher.Groups["day"].Success ? matcher.Groups["day"].Value : "";
string episode = matcher.Groups["episode"].Success ? matcher.Groups["episode"].Value : "";

string season = matcher.Groups["season"].Success ? matcher.Groups["season"].Value : "";
season = season == "" ? year + month : season;

string broadcastDate = (year != "" && month != "" && day != "") ? year + "-" + month + "-" + day : "";
if (broadcastDate == "" && file_date != null)
{
broadcastDate = file_date?.ToString("yyyy-MM-dd") ?? "";
}

string title = matcher.Groups["title"].Success ? matcher.Groups["title"].Value : "";
if (title != "")
{
if (!string.IsNullOrEmpty(series) && title != series && title.ToLower().Contains(series.ToLower()))
{
title = title.Replace(series, "", StringComparison.OrdinalIgnoreCase).Trim();
}

if (title == "" && title == series && broadcastDate != "")
{
title = broadcastDate;
}

// -- replace double spaces with single space
title = Regex.Replace(title, @"\[.+?\]", " ").Trim('-').Trim();
title = Regex.Replace(title, @"\s+", " ");
title = title.Trim().Trim('-').Trim();

if (matcher.Groups["epNumber"].Success)
{
title = matcher.Groups["epNumber"].Value + " - " + title;
}
else if (title != "" && broadcastDate != "" && broadcastDate != title)
{
title = $"{broadcastDate.Replace("-", "")} ~ {title}";
}
}

if (episode == "")
{
episode = "1" + month + day;

// get the modified date of the file
if (System.IO.File.Exists(file) || file_date != null)
{
episode += (file_date ?? System.IO.File.GetLastWriteTimeUtc(file)).ToString("mmss");
}
}

episode = (episode == "") ? int.Parse('1' + month + day).ToString() : episode;

EpisodeInfo item = new()
{
IndexNumber = int.Parse(episode),
Name = title,
Path = file,
Year = int.Parse(year),
ParentIndexNumber = int.Parse(season)
};

item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, item.IndexNumber.ToString());

// -- Set the PremiereDate if we have a year, month and day
if (year != "" && month != "" && day != "")
{
item.PremiereDate = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
}

return item;
}


public static MetadataResult<Episode> ToEpisode(EpisodeInfo data)
{
if (data.Path == "")
{
Logger?.LogInformation($"No metadata found for '{data.Path}'.");
return ErrorOutEpisode();
}

Logger?.LogDebug($"Processing {data}.");

Episode item = new()
{
Name = data.Name,
IndexNumber = data.IndexNumber,
ParentIndexNumber = data.ParentIndexNumber,
Path = data.Path,
};

if (data.PremiereDate is DateTime time)
{
item.PremiereDate = time;
item.ProductionYear = time.Year;
item.ForcedSortName = time.ToString("yyyyMMdd") + '-' + item.Name;
}

item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, data.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);

return new MetadataResult<Episode>
{
HasMetadata = true,
Item = item
};
}

private static MetadataResult<Series> ErrorOut()
{
return new MetadataResult<Series>
{
HasMetadata = true,
HasMetadata = false,
Item = new Series()
};
}

private static MetadataResult<Episode> ErrorOutEpisode()
{
return new MetadataResult<Episode>
{
HasMetadata = false,
Item = new Episode()
};
}
}
}
107 changes: 0 additions & 107 deletions CustomMetadataDB/Provider/AbstractProvider.cs

This file was deleted.

Loading