Skip to content

Commit

Permalink
Some small changes in PDF parser
Browse files Browse the repository at this point in the history
  • Loading branch information
majora2007 committed Feb 17, 2025
1 parent 494a8cb commit eff13bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
54 changes: 27 additions & 27 deletions API/Helpers/PdfComicInfoExtractor.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
/// Translate PDF metadata (See PdfMetadataExtractor.cs) into ComicInfo structure.

// Contributed by https://github.com/microtherion

// All references to the "PDF Spec" (section numbers, etc) refer to the
// PDF 1.7 Specification a.k.a. PDF32000-1:2008
// https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf

/**
* Contributed by https://github.com/microtherion
*
* All references to the "PDF Spec" (section numbers, etc) refer to the
* PDF 1.7 Specification a.k.a. PDF32000-1:2008
* https://opensource.adobe.com/dc-acrobat-sdk-docs/pdfstandards/PDF32000_2008.pdf
*/
//
using System;
using System.Xml;
using System.Text;
using System.IO;
using System.Diagnostics;
using API.Data.Metadata;
using API.Entities.Enums;
using API.Services;
using API.Services.Tasks.Scanner.Parser;
using Microsoft.Extensions.Logging;
using Nager.ArticleNumber;
using System.Collections.Generic;
using System.Globalization;

namespace API.Helpers;
#nullable enable
Expand All @@ -27,6 +24,9 @@ public interface IPdfComicInfoExtractor
ComicInfo? GetComicInfo(string filePath);
}

/// <summary>
/// Translate PDF metadata (See PdfMetadataExtractor.cs) into ComicInfo structure.
/// </summary>
public class PdfComicInfoExtractor : IPdfComicInfoExtractor
{
private readonly ILogger<BookService> _logger;
Expand All @@ -44,7 +44,7 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med
_mediaErrorService = mediaErrorService;
}

private float? GetFloatFromText(string? text)
private static float? GetFloatFromText(string? text)
{
if (string.IsNullOrEmpty(text)) return null;

Expand Down Expand Up @@ -78,9 +78,9 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med
return null;
}

private string? MaybeGetMetadata(Dictionary<string, string> metadata, string key)
private static string? MaybeGetMetadata(Dictionary<string, string> metadata, string key)
{
return metadata.ContainsKey(key) ? metadata[key] : null;
return metadata.TryGetValue(key, out var value) ? value : null;
}

private ComicInfo? GetComicInfoFromMetadata(Dictionary<string, string> metadata, string filePath)
Expand All @@ -100,6 +100,7 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med
info.Publisher = MaybeGetMetadata(metadata, "Publisher") ?? string.Empty;
info.Writer = MaybeGetMetadata(metadata, "Author") ?? string.Empty;
info.Title = MaybeGetMetadata(metadata, "Title") ?? string.Empty;
info.TitleSort = MaybeGetMetadata(metadata, "TitleSort") ?? string.Empty;
info.Genre = MaybeGetMetadata(metadata, "Subject") ?? string.Empty;
info.LanguageISO = BookService.ValidateLanguage(MaybeGetMetadata(metadata, "Language"));
info.Isbn = MaybeGetMetadata(metadata, "ISBN") ?? string.Empty;
Expand All @@ -111,10 +112,9 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med
}

info.UserRating = GetFloatFromText(MaybeGetMetadata(metadata, "UserRating")) ?? 0.0f;
info.TitleSort = MaybeGetMetadata(metadata, "TitleSort") ?? string.Empty;
info.Series = MaybeGetMetadata(metadata, "Series") ?? info.TitleSort;
info.Series = MaybeGetMetadata(metadata, "Series") ?? info.Title;
info.SeriesSort = info.Series;
info.Volume = (GetFloatFromText(MaybeGetMetadata(metadata, "Volume")) ?? 0.0f).ToString();
info.Volume = (GetFloatFromText(MaybeGetMetadata(metadata, "Volume")) ?? 0.0f).ToString(CultureInfo.InvariantCulture);

// If this is a single book and not a collection, set publication status to Completed
if (string.IsNullOrEmpty(info.Volume) && Parser.ParseVolume(filePath, LibraryType.Manga).Equals(Parser.LooseLeafVolume))
Expand All @@ -124,15 +124,15 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med

// Removed as probably unneeded per discussion in https://github.com/Kareadita/Kavita/pull/3108#discussion_r1956747782
//
// var hasVolumeInSeries = !Parser.ParseVolume(info.Title, LibraryType.Manga)
// .Equals(Parser.LooseLeafVolume);
var hasVolumeInSeries = !Parser.ParseVolume(info.Title, LibraryType.Manga)
.Equals(Parser.LooseLeafVolume);

// if (string.IsNullOrEmpty(info.Volume) && hasVolumeInSeries && (!info.Series.Equals(info.Title) || string.IsNullOrEmpty(info.Series)))
// {
// // This is likely a light novel for which we can set series from parsed title
// info.Series = Parser.ParseSeries(info.Title, LibraryType.Manga);
// info.Volume = Parser.ParseVolume(info.Title, LibraryType.Manga);
// }
if (string.IsNullOrEmpty(info.Volume) && hasVolumeInSeries && (!info.Series.Equals(info.Title) || string.IsNullOrEmpty(info.Series)))
{
// This is likely a light novel for which we can set series from parsed title
// info.Series = Parser.ParseSeries(info.Title, LibraryType.Manga);
// info.Volume = Parser.ParseVolume(info.Title, LibraryType.Manga);
}

ComicInfo.CleanComicInfo(info);

Expand All @@ -156,4 +156,4 @@ public PdfComicInfoExtractor(ILogger<BookService> logger, IMediaErrorService med

return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class ManageUserPreferencesComponent implements OnInit {

get Locale() {
if (!this.settingsForm.get('locale')) return 'English';
console.log(this.locales.filter(l => l.isoCode === this.settingsForm.get('locale')!.value)[0])

return this.locales.filter(l => l.isoCode === this.settingsForm.get('locale')!.value)[0].title;
}

Expand Down

0 comments on commit eff13bd

Please sign in to comment.