Skip to content

Commit

Permalink
no longer rely on mtime to pad episode number
Browse files Browse the repository at this point in the history
  • Loading branch information
arabcoders committed Jul 2, 2024
1 parent 1dc2dd1 commit 6171a9d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 24 deletions.
26 changes: 19 additions & 7 deletions CustomMetadataDB.Tests/UtilsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public class UtilsTest
private static readonly DTO[] _data = LoadFixtureData();
private static readonly ILogger<UtilsTest> _logger = SetLogger();

private static ILogger<UtilsTest> SetLogger()
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
return loggerFactory.CreateLogger<UtilsTest>();
}

private static DTO[] LoadFixtureData()
{
var assembly = Assembly.GetExecutingAssembly();
Expand Down Expand Up @@ -73,8 +79,8 @@ public void Test_get_series_id_from_json(string dir, string expected)
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);
var item = Utils.FileToInfo(path);
Assert.Equal(110125248, item.IndexNumber);
Assert.Equal(2020, item.ParentIndexNumber);
Assert.Equal(2020, item.Year);
Assert.Equal("ep24 - ahmed", item.Name);
Expand All @@ -85,13 +91,13 @@ public void Test_file_to_info()
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)));
var result = Utils.ToEpisode(Utils.FileToInfo(path));

Assert.True(result.HasMetadata);

var item = result.Item;

Assert.Equal(110120203, item.IndexNumber);
Assert.Equal(110125248, item.IndexNumber);
Assert.Equal(2020, item.ParentIndexNumber);
Assert.Equal("ep24 - ahmed", item.Name);
Assert.Equal($"{item.IndexNumber}", item.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
Expand Down Expand Up @@ -132,10 +138,16 @@ public void Test_toStandard_Naming_Multi(string filename, int indexEnd)
Assert.Equal(indexEnd, item.IndexNumberEnd);
}

private static ILogger<UtilsTest> SetLogger()
[Theory]
[InlineData("230629 Yoake no LOVE it! - BAT [720p h264 MP4 CM Cut].mkv", 4957)]
[InlineData("240629 Yoake no LOVE it! - FOO [720p h264 MP4 CM Cut].mkv", 4955)]
[InlineData("240629 Yoake no LOVE it! - BAR [820p h264 MP4 CM Cut].mkv", 5451)]
[InlineData("2406 NO [820p h264 MP4 CM Cut].mkv", 9852)]
[InlineData("2306 OK [820p h264 MP4 CM Cut].mkv", 1025)]
[InlineData("2206 TEST [820p h264 MP4 CM Cut].mkv", 5398)]
public void Test_ExtendId(string filename, int expected)
{
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
return loggerFactory.CreateLogger<UtilsTest>();
Assert.Equal(expected, Utils.ExtendId(filename));
}
}
}
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>net8.0</TargetFramework>
<RootNamespace>CustomMetadataDB</RootNamespace>
<Version>1.1.0.7</Version>
<Version>1.1.0.8</Version>
<FileVersion>$(Version)</FileVersion>
<AssemblyVersion>$(Version)</AssemblyVersion>
<IsPackable>true</IsPackable>
Expand Down
50 changes: 35 additions & 15 deletions CustomMetadataDB/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
using System;
using System.Text.RegularExpressions;
using Jellyfin.Data.Enums;
using System.Security.Cryptography;
using System.Text;
using System.IO;

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

public static int ExtendId(string path)
{
// 1. Get the basename, remove the extension, and convert to lowercase
string basename = Path.GetFileNameWithoutExtension(path).ToLower();

// 2. Hash the basename using SHA-256
using SHA256 sha256 = SHA256.Create();
byte[] hashBytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(basename));

// 3. Convert the SHA-256 hash to a hexadecimal string
string hex = BitConverter.ToString(hashBytes).Replace("-", "").ToLower();

// 4. Convert hexadecimal characters to ASCII values
StringBuilder asciiValues = new StringBuilder();
foreach (char c in hex)
{
asciiValues.Append(((int)c).ToString());
}

// 5. Get the final 4 digits, ensure it's a 4-digit integer
string asciiString = asciiValues.ToString();
string fourDigitString = asciiString.Length >= 4 ? asciiString.Substring(0, 4) : asciiString.PadRight(4, '9');
int fourDigitNumber = int.Parse(fourDigitString);

return fourDigitNumber;
}

public static MetadataResult<Series> ToSeries(DTO data)
{
Logger?.LogDebug($"Processing {data}.");
Expand Down Expand Up @@ -82,11 +113,10 @@ public static MetadataResult<Series> ToSeries(DTO data)
return new() { HasMetadata = true, Item = item };
}

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

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

Match matcher = null;

Expand Down Expand Up @@ -117,10 +147,6 @@ public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)
season = season == "" ? year : season;

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

string title = matcher.Groups["title"].Success ? matcher.Groups["title"].Value : "";
if (title != "")
Expand Down Expand Up @@ -152,13 +178,7 @@ public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)

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.GetCreationTimeUtc(file)).ToString("mmss");
}
episode = $"1{month}{day}{ExtendId(filename)}";
}

episode = (episode == "") ? int.Parse('1' + month + day).ToString() : episode;
Expand Down Expand Up @@ -205,7 +225,7 @@ public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)
}

var indexEnd = item.IndexNumberEnd.HasValue ? $"-E{item.IndexNumberEnd}" : "";
Logger?.LogInformation($"Parsed '{System.IO.Path.GetFileName(file)}' as 'S{item.ParentIndexNumber}E{item.IndexNumber}{indexEnd}' - {item.Name}'.");
Logger?.LogInformation($"Parsed '{Path.GetFileName(file)}' as 'S{item.ParentIndexNumber}E{item.IndexNumber}{indexEnd}' - {item.Name}'.");

return item;
}
Expand Down
2 changes: 1 addition & 1 deletion build.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
name: "Custom metadata agent db"
guid: "83b77e24-9fce-4ee0-a794-73fdfa972e66"
version: "1.0.0.7"
version: "1.0.0.8"
targetAbi: "10.7.0.0"
owner: "arabcoders"
overview: "A Custom metadata agent."
Expand Down

0 comments on commit 6171a9d

Please sign in to comment.