Skip to content

Commit 09d57ab

Browse files
authored
Merge pull request #6 from arabcoders/dev
Folded DailyExtender into cmdb
2 parents d29ca45 + f172770 commit 09d57ab

File tree

10 files changed

+400
-187
lines changed

10 files changed

+400
-187
lines changed

CustomMetadataDB.Tests/UtilsTests.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,33 @@ public void Test_get_series_id_from_json(string dir, string expected)
6666

6767
Assert.Equal(expected, result);
6868
}
69+
70+
[Fact]
71+
public void Test_file_to_info()
72+
{
73+
var path = "/home/media/test/201012 foobar ep24 - ahmed [foo].mkv";
74+
var item = Utils.FileToInfo(path, new DateTime(2021, 1, 1, 01, 02, 03, DateTimeKind.Utc));
75+
Assert.Equal(110120203, item.IndexNumber);
76+
Assert.Equal(202010, item.ParentIndexNumber);
77+
Assert.Equal(2020, item.Year);
78+
Assert.Equal("ep24 - ahmed", item.Name);
79+
Assert.Equal($"{item.IndexNumber}", item.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
80+
}
81+
82+
[Fact]
83+
public void Test_ToEpisode()
84+
{
85+
var path = "/home/media/test/201012 foobar ep24 - ahmed [foo].mkv";
86+
var result = Utils.ToEpisode(Utils.FileToInfo(path, new DateTime(2021, 1, 1, 01, 02, 03, DateTimeKind.Utc)));
87+
88+
Assert.True(result.HasMetadata);
89+
90+
var item = result.Item;
91+
92+
Assert.Equal(110120203, item.IndexNumber);
93+
Assert.Equal(202010, item.ParentIndexNumber);
94+
Assert.Equal("ep24 - ahmed", item.Name);
95+
Assert.Equal($"{item.IndexNumber}", item.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
96+
}
6997
}
7098
}

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>net6.0</TargetFramework>
44
<RootNamespace>CustomMetadataDB</RootNamespace>
5-
<Version>0.0.0.2</Version>
5+
<Version>1.0.0.0</Version>
66
<FileVersion>$(Version)</FileVersion>
77
<AssemblyVersion>$(Version)</AssemblyVersion>
88
<RestorePackagesWithLockFile>true</RestorePackagesWithLockFile>

CustomMetadataDB/ExternalId.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,30 @@
44
using CustomMetadataDB.Helpers;
55
using MediaBrowser.Controller.Configuration;
66

7-
namespace CustomMetadataDB
7+
namespace CustomMetadataDB;
8+
9+
public class SeriesExternalId : IExternalId
810
{
9-
public class SeriesExternalId : IExternalId
11+
public bool Supports(IHasProviderIds item) => item is Series;
12+
public string Name => Constants.PLUGIN_NAME;
13+
public string Key => Constants.PLUGIN_EXTERNAL_ID;
14+
public string UrlFormatString { get; set; }
15+
16+
public SeriesExternalId(IServerConfigurationManager config)
1017
{
11-
public bool Supports(IHasProviderIds item) => item is Series;
12-
public string Name => Constants.PLUGIN_NAME;
13-
public string Key => Constants.PLUGIN_EXTERNAL_ID;
14-
public string UrlFormatString { get; set; }
18+
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
19+
}
20+
}
1521

16-
public SeriesExternalId(IServerConfigurationManager config)
17-
{
18-
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
19-
}
22+
public class EpisodeExternalId : IExternalId
23+
{
24+
public bool Supports(IHasProviderIds item) => item is Episode;
25+
public string Name => Constants.PLUGIN_NAME;
26+
public string Key => Constants.PLUGIN_EXTERNAL_ID;
27+
public string UrlFormatString { get; set; }
28+
29+
public EpisodeExternalId(IServerConfigurationManager config)
30+
{
31+
UrlFormatString = Utils.GetConfiguration(config).ApiRefUrl;
2032
}
2133
}
Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
1-
namespace CustomMetadataDB.Helpers
1+
using System.Text.RegularExpressions;
2+
3+
namespace CustomMetadataDB.Helpers
24
{
35
public class Constants
46
{
57
public const string PLUGIN_NAME = "CMetadataDB";
68
public const string PLUGIN_EXTERNAL_ID = "cmdb";
79
public const string PLUGIN_DESCRIPTION = "Custom metadata agent db.";
810
public const string PLUGIN_GUID = "83b77e24-9fce-4ee0-a794-73fdfa972e66";
11+
public static readonly Regex[] EPISODE_MATCHERS = {
12+
// YY?YY(-._)MM(-._)DD -? series -? epNumber -? title
13+
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),
14+
// YY?YY(-._)MM(-._)DD -? title
15+
new(@"^(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})\s?-?(?<title>.+)", RegexOptions.Compiled | RegexOptions.IgnoreCase),
16+
// title YY?YY(-._)MM(-._)DD at end of filename.
17+
new(@"(?<title>.+?)(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})$", RegexOptions.Compiled | RegexOptions.IgnoreCase),
18+
// series - YY?YY(-._)MM(-._)DD -? title
19+
new(@"(?<series>.+?)(?<year>\d{2,4})(\-|\.|_)?(?<month>\d{2})(\-|\.|_)?(?<day>\d{2})\s?-?(?<title>.+)?", RegexOptions.Compiled | RegexOptions.IgnoreCase)
20+
};
921
}
1022
}

CustomMetadataDB/Helpers/Utils.cs

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Text.Json.Serialization;
1111
using System;
1212
using CustomMetadataDB.Helpers.Configuration;
13+
using System.Text.RegularExpressions;
1314

1415
namespace CustomMetadataDB.Helpers
1516
{
@@ -36,6 +37,7 @@ public static PersonInfo CreatePerson(string name, string provider_id)
3637
ProviderIds = new ProviderIdDictionary(new Dictionary<string, string> { { Constants.PLUGIN_EXTERNAL_ID, provider_id } }),
3738
};
3839
}
40+
3941
public static MetadataResult<Series> ToSeries(DTO data)
4042
{
4143
Logger?.Info($"Processing {data}.");
@@ -90,13 +92,143 @@ public static MetadataResult<Series> ToSeries(DTO data)
9092
Item = item
9193
};
9294
}
93-
private static MetadataResult<Series> ErrorOut()
95+
96+
public static EpisodeInfo FileToInfo(string file, DateTime? file_date = null)
9497
{
95-
return new MetadataResult<Series>
98+
99+
//-- get only the file stem
100+
string filename = System.IO.Path.GetFileNameWithoutExtension(file);
101+
102+
Match matcher = null;
103+
104+
for (int i = 0; i < Constants.EPISODE_MATCHERS.Length; i++)
96105
{
97-
HasMetadata = true,
98-
Item = new Series()
106+
matcher = Constants.EPISODE_MATCHERS[i].Match(filename);
107+
if (!matcher.Success)
108+
{
109+
continue;
110+
}
111+
break;
112+
}
113+
114+
if (!matcher.Success)
115+
{
116+
Logger?.Info($"No match found for {file}.");
117+
return new EpisodeInfo();
118+
}
119+
120+
string series = matcher.Groups["series"].Success ? matcher.Groups["series"].Value : "";
121+
string year = matcher.Groups["year"].Success ? matcher.Groups["year"].Value : "";
122+
year = (year.Length == 2) ? "20" + year : year;
123+
string month = matcher.Groups["month"].Success ? matcher.Groups["month"].Value : "";
124+
string day = matcher.Groups["day"].Success ? matcher.Groups["day"].Value : "";
125+
string episode = matcher.Groups["episode"].Success ? matcher.Groups["episode"].Value : "";
126+
127+
string season = matcher.Groups["season"].Success ? matcher.Groups["season"].Value : "";
128+
season = season == "" ? year + month : season;
129+
130+
string broadcastDate = (year != "" && month != "" && day != "") ? year + "-" + month + "-" + day : "";
131+
if (broadcastDate == "" && file_date != null)
132+
{
133+
broadcastDate = file_date?.ToString("yyyy-MM-dd") ?? "";
134+
}
135+
136+
string title = matcher.Groups["title"].Success ? matcher.Groups["title"].Value : "";
137+
if (title != "")
138+
{
139+
if (!string.IsNullOrEmpty(series) && title != series && title.ToLower().Contains(series.ToLower()))
140+
{
141+
title = title.Replace(series, "", StringComparison.OrdinalIgnoreCase).Trim();
142+
}
143+
144+
if (title == "" && title == series && broadcastDate != "")
145+
{
146+
title = broadcastDate;
147+
}
148+
149+
// -- replace double spaces with single space
150+
title = Regex.Replace(title, @"\[.+?\]", " ").Trim('-').Trim();
151+
title = Regex.Replace(title, @"\s+", " ");
152+
title = title.Trim().Trim('-').Trim();
153+
154+
if (matcher.Groups["epNumber"].Success)
155+
{
156+
title = matcher.Groups["epNumber"].Value + " - " + title;
157+
}
158+
else if (title != "" && broadcastDate != "" && broadcastDate != title)
159+
{
160+
title = $"{broadcastDate.Replace("-", "")} ~ {title}";
161+
}
162+
}
163+
164+
if (episode == "")
165+
{
166+
episode = "1" + month + day;
167+
168+
// get the modified date of the file
169+
if (System.IO.File.Exists(file) || file_date != null)
170+
{
171+
episode += (file_date ?? System.IO.File.GetLastWriteTimeUtc(file)).ToString("mmss");
172+
}
173+
}
174+
175+
episode = (episode == "") ? int.Parse('1' + month + day).ToString() : episode;
176+
177+
EpisodeInfo item = new()
178+
{
179+
IndexNumber = int.Parse(episode),
180+
Name = title,
181+
Year = int.Parse(year),
182+
ParentIndexNumber = int.Parse(season)
183+
};
184+
185+
item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, item.IndexNumber.ToString());
186+
187+
// -- Set the PremiereDate if we have a year, month and day
188+
if (year != "" && month != "" && day != "")
189+
{
190+
item.PremiereDate = new DateTime(int.Parse(year), int.Parse(month), int.Parse(day));
191+
}
192+
193+
return item;
194+
}
195+
196+
public static MetadataResult<Episode> ToEpisode(EpisodeInfo data)
197+
{
198+
if (data.Name == "")
199+
{
200+
Logger?.Warn($"No metadata found for '{data}'.");
201+
return ErrorOutEpisode();
202+
}
203+
204+
Logger?.Debug($"Processing {data}.");
205+
206+
Episode item = new()
207+
{
208+
Name = data.Name,
209+
IndexNumber = data.IndexNumber,
210+
ParentIndexNumber = data.ParentIndexNumber,
99211
};
212+
213+
if (data.PremiereDate is DateTimeOffset time)
214+
{
215+
item.PremiereDate = time;
216+
item.ProductionYear = time.Year;
217+
}
218+
219+
item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, data.ProviderIds[Constants.PLUGIN_EXTERNAL_ID]);
220+
221+
return new MetadataResult<Episode> { HasMetadata = true, Item = item };
222+
}
223+
224+
private static MetadataResult<Series> ErrorOut()
225+
{
226+
return new MetadataResult<Series> { HasMetadata = false, Item = new Series() };
227+
}
228+
229+
private static MetadataResult<Episode> ErrorOutEpisode()
230+
{
231+
return new MetadataResult<Episode> { HasMetadata = false, Item = new Episode() };
100232
}
101233
}
102234
}

CustomMetadataDB/Provider/AbstractProvider.cs

Lines changed: 0 additions & 113 deletions
This file was deleted.

0 commit comments

Comments
 (0)