Skip to content

Commit

Permalink
Merge pull request #6 from arabcoders/dev
Browse files Browse the repository at this point in the history
Folded DailyExtender into cmdb
  • Loading branch information
arabcoders authored Mar 22, 2024
2 parents d29ca45 + f172770 commit 09d57ab
Show file tree
Hide file tree
Showing 10 changed files with 400 additions and 187 deletions.
28 changes: 28 additions & 0 deletions CustomMetadataDB.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,33 @@ 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]);
}
}
}
2 changes: 1 addition & 1 deletion CustomMetadataDB/CustomMetadataDB.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
32 changes: 22 additions & 10 deletions CustomMetadataDB/ExternalId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,30 @@
using CustomMetadataDB.Helpers;
using MediaBrowser.Controller.Configuration;

namespace CustomMetadataDB
namespace CustomMetadataDB;

public class SeriesExternalId : IExternalId
{
public class SeriesExternalId : IExternalId
public bool Supports(IHasProviderIds item) => item is Series;
public string Name => Constants.PLUGIN_NAME;
public string Key => Constants.PLUGIN_EXTERNAL_ID;
public string UrlFormatString { get; set; }

public SeriesExternalId(IServerConfigurationManager config)
{
public bool Supports(IHasProviderIds item) => item is Series;
public string Name => Constants.PLUGIN_NAME;
public string Key => Constants.PLUGIN_EXTERNAL_ID;
public string UrlFormatString { get; set; }
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
}
}

public SeriesExternalId(IServerConfigurationManager config)
{
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
}
public class EpisodeExternalId : IExternalId
{
public bool Supports(IHasProviderIds item) => item is Episode;
public string Name => Constants.PLUGIN_NAME;
public string Key => Constants.PLUGIN_EXTERNAL_ID;
public string UrlFormatString { get; set; }

public EpisodeExternalId(IServerConfigurationManager config)
{
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
}
}
14 changes: 13 additions & 1 deletion CustomMetadataDB/Helpers/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
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)
};
}
}
140 changes: 136 additions & 4 deletions CustomMetadataDB/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.Json.Serialization;
using System;
using CustomMetadataDB.Helpers.Configuration;
using System.Text.RegularExpressions;

namespace CustomMetadataDB.Helpers
{
Expand All @@ -36,6 +37,7 @@ public static PersonInfo CreatePerson(string name, string provider_id)
ProviderIds = new ProviderIdDictionary(new Dictionary<string, string> { { Constants.PLUGIN_EXTERNAL_ID, provider_id } }),
};
}

public static MetadataResult<Series> ToSeries(DTO data)
{
Logger?.Info($"Processing {data}.");
Expand Down Expand Up @@ -90,13 +92,143 @@ public static MetadataResult<Series> ToSeries(DTO data)
Item = item
};
}
private static MetadataResult<Series> ErrorOut()

public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)
{
return new MetadataResult<Series>

//-- 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++)
{
HasMetadata = true,
Item = new Series()
matcher = Constants.EPISODE_MATCHERS[i].Match(filename);
if (!matcher.Success)
{
continue;
}
break;
}

if (!matcher.Success)
{
Logger?.Info($"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,
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.Name == "")
{
Logger?.Warn($"No metadata found for '{data}'.");
return ErrorOutEpisode();
}

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

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

if (data.PremiereDate is DateTimeOffset time)
{
item.PremiereDate = time;
item.ProductionYear = time.Year;
}

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 = false, Item = new Series() };
}

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

This file was deleted.

Loading

0 comments on commit 09d57ab

Please sign in to comment.