|
12 | 12 | using System.Security.Cryptography;
|
13 | 13 | using System.Text;
|
14 | 14 | using System.IO;
|
| 15 | +using System.Linq; |
| 16 | +using System.Xml.Linq; |
15 | 17 |
|
16 | 18 | namespace CustomMetadataDB.Helpers;
|
17 | 19 |
|
@@ -233,7 +235,7 @@ public static EpisodeInfo FileToInfo(string file)
|
233 | 235 | }
|
234 | 236 |
|
235 | 237 |
|
236 |
| - public static MetadataResult<Episode> ToEpisode(EpisodeInfo data) |
| 238 | + public static MetadataResult<Episode> ToEpisode(EpisodeInfo data, string file = null) |
237 | 239 | {
|
238 | 240 | if (data.Path == "")
|
239 | 241 | {
|
@@ -272,9 +274,105 @@ public static MetadataResult<Episode> ToEpisode(EpisodeInfo data)
|
272 | 274 | item.SetProviderId(Constants.PLUGIN_EXTERNAL_ID, id);
|
273 | 275 | }
|
274 | 276 |
|
| 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 | + |
275 | 297 | return new() { HasMetadata = true, Item = item };
|
276 | 298 | }
|
277 | 299 |
|
278 | 300 | private static MetadataResult<Series> ErrorOut() => new() { HasMetadata = false, Item = new Series() };
|
279 | 301 | 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]+;)", "&"); |
| 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 | + } |
280 | 378 | }
|
0 commit comments