mirror of
https://github.com/Readarr/Readarr
synced 2026-01-07 16:13:05 +01:00
New: Send series info to calibre when importing
This commit is contained in:
parent
6ea1a56b50
commit
515751b1e1
7 changed files with 86 additions and 30 deletions
|
|
@ -80,20 +80,20 @@ public void AddFormat(BookFile file, CalibreSettings settings)
|
|||
var format = Path.GetExtension(file.Path);
|
||||
var bookData = Convert.ToBase64String(File.ReadAllBytes(file.Path));
|
||||
|
||||
var payload = new
|
||||
var payload = new CalibreChangesPayload
|
||||
{
|
||||
changes = new
|
||||
LoadedBookIds = new List<int> { file.CalibreId },
|
||||
Changes = new CalibreChanges
|
||||
{
|
||||
added_formats = new[]
|
||||
AddedFormats = new List<CalibreAddFormat>
|
||||
{
|
||||
new
|
||||
new CalibreAddFormat
|
||||
{
|
||||
ext = format,
|
||||
data_url = bookData
|
||||
Ext = format,
|
||||
Data = bookData
|
||||
}
|
||||
}
|
||||
},
|
||||
loaded_book_ids = new[] { file.CalibreId }
|
||||
}
|
||||
};
|
||||
|
||||
ExecuteSetFields(file.CalibreId, payload, settings);
|
||||
|
|
@ -101,14 +101,13 @@ public void AddFormat(BookFile file, CalibreSettings settings)
|
|||
|
||||
public void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSettings settings)
|
||||
{
|
||||
var payload = new
|
||||
var payload = new CalibreChangesPayload
|
||||
{
|
||||
changes = new
|
||||
LoadedBookIds = new List<int> { calibreId },
|
||||
Changes = new CalibreChanges
|
||||
{
|
||||
removed_formats = formats
|
||||
},
|
||||
|
||||
loaded_book_ids = new[] { calibreId }
|
||||
RemovedFormats = formats.ToList()
|
||||
}
|
||||
};
|
||||
|
||||
ExecuteSetFields(calibreId, payload, settings);
|
||||
|
|
@ -117,6 +116,18 @@ public void RemoveFormats(int calibreId, IEnumerable<string> formats, CalibreSet
|
|||
public void SetFields(BookFile file, CalibreSettings settings)
|
||||
{
|
||||
var edition = file.Edition.Value;
|
||||
var book = edition.Book.Value;
|
||||
var serieslink = book.SeriesLinks.Value.FirstOrDefault();
|
||||
|
||||
var series = serieslink?.Series.Value;
|
||||
double? seriesIndex = null;
|
||||
if (double.TryParse(serieslink?.Position, out var index))
|
||||
{
|
||||
_logger.Trace($"Parsed {serieslink?.Position} as {index}");
|
||||
seriesIndex = index;
|
||||
}
|
||||
|
||||
_logger.Trace($"Book: {book} Series: {series?.Title}, Position: {seriesIndex}");
|
||||
|
||||
var cover = edition.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover);
|
||||
string image = null;
|
||||
|
|
@ -131,30 +142,32 @@ public void SetFields(BookFile file, CalibreSettings settings)
|
|||
}
|
||||
}
|
||||
|
||||
var payload = new
|
||||
var payload = new CalibreChangesPayload
|
||||
{
|
||||
changes = new
|
||||
LoadedBookIds = new List<int> { file.CalibreId },
|
||||
Changes = new CalibreChanges
|
||||
{
|
||||
title = edition.Title,
|
||||
authors = new[] { file.Author.Value.Name },
|
||||
cover = image,
|
||||
pubdate = edition.Book.Value.ReleaseDate,
|
||||
comments = edition.Overview,
|
||||
rating = edition.Ratings.Value * 2,
|
||||
identifiers = new Dictionary<string, string>
|
||||
Title = edition.Title,
|
||||
Authors = new List<string> { file.Author.Value.Name },
|
||||
Cover = image,
|
||||
PubDate = book.ReleaseDate,
|
||||
Comments = edition.Overview,
|
||||
Rating = edition.Ratings.Value * 2,
|
||||
Identifiers = new Dictionary<string, string>
|
||||
{
|
||||
{ "isbn", edition.Isbn13 },
|
||||
{ "asin", edition.Asin },
|
||||
{ "goodreads", edition.ForeignEditionId }
|
||||
}
|
||||
},
|
||||
loaded_book_ids = new[] { file.CalibreId }
|
||||
},
|
||||
Series = series?.Title,
|
||||
SeriesIndex = seriesIndex
|
||||
}
|
||||
};
|
||||
|
||||
ExecuteSetFields(file.CalibreId, payload, settings);
|
||||
}
|
||||
|
||||
private void ExecuteSetFields(int id, object payload, CalibreSettings settings)
|
||||
private void ExecuteSetFields(int id, CalibreChangesPayload payload, CalibreSettings settings)
|
||||
{
|
||||
var builder = GetBuilder($"cdb/set-fields/{id}", settings)
|
||||
.Post()
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public CalibreSettings()
|
|||
}
|
||||
|
||||
public string Host { get; set; }
|
||||
public int Port { get; set; } = 8080;
|
||||
public int Port { get; set; }
|
||||
public string UrlBase { get; set; }
|
||||
public string Username { get; set; }
|
||||
public string Password { get; set; }
|
||||
|
|
|
|||
38
src/NzbDrone.Core/Books/Calibre/Resources/CalibreChanges.cs
Normal file
38
src/NzbDrone.Core/Books/Calibre/Resources/CalibreChanges.cs
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NzbDrone.Core.Books.Calibre
|
||||
{
|
||||
public class CalibreChangesPayload
|
||||
{
|
||||
public CalibreChanges Changes { get; set; }
|
||||
[JsonProperty("loaded_book_ids")]
|
||||
public List<int> LoadedBookIds { get; set; }
|
||||
}
|
||||
|
||||
public class CalibreChanges
|
||||
{
|
||||
public string Title { get; set; }
|
||||
public List<string> Authors { get; set; }
|
||||
public string Cover { get; set; }
|
||||
[JsonProperty("pubdate")]
|
||||
public DateTime? PubDate { get; set; }
|
||||
public string Comments { get; set; }
|
||||
public decimal Rating { get; set; }
|
||||
public Dictionary<string, string> Identifiers { get; set; }
|
||||
public string Series { get; set; }
|
||||
[JsonProperty("series_index")]
|
||||
public double? SeriesIndex { get; set; }
|
||||
|
||||
public List<CalibreAddFormat> AddedFormats { get; set; }
|
||||
public List<string> RemovedFormats { get; set; }
|
||||
}
|
||||
|
||||
public class CalibreAddFormat
|
||||
{
|
||||
public string Ext { get; set; }
|
||||
[JsonProperty("data_url")]
|
||||
public string Data { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
@ -115,7 +115,9 @@ public static void Map()
|
|||
.Where<Series>(s => s.Id == series.Id)).ToList(),
|
||||
s => s.Id > 0);
|
||||
|
||||
Mapper.Entity<SeriesBookLink>("SeriesBookLink").RegisterModel();
|
||||
Mapper.Entity<SeriesBookLink>("SeriesBookLink").RegisterModel()
|
||||
.HasOne(l => l.Book, l => l.BookId)
|
||||
.HasOne(l => l.Series, l => l.SeriesId);
|
||||
|
||||
Mapper.Entity<AuthorMetadata>("AuthorMetadata").RegisterModel();
|
||||
|
||||
|
|
@ -135,7 +137,10 @@ public static void Map()
|
|||
new SqlBuilder()
|
||||
.Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id)
|
||||
.Where<Author>(a => a.AuthorMetadataId == book.AuthorMetadataId)).SingleOrDefault(),
|
||||
a => a.AuthorMetadataId > 0);
|
||||
a => a.AuthorMetadataId > 0)
|
||||
.LazyLoad(b => b.SeriesLinks,
|
||||
(db, book) => db.Query<SeriesBookLink>(new SqlBuilder().Where<SeriesBookLink>(s => s.BookId == book.Id)).ToList(),
|
||||
b => b.Id > 0);
|
||||
|
||||
Mapper.Entity<Edition>("Editions").RegisterModel()
|
||||
.HasOne(r => r.Book, r => r.BookId)
|
||||
|
|
|
|||
Loading…
Reference in a new issue