New: Send series info to calibre when importing

pull/395/head
ta264 4 years ago
parent 6ea1a56b50
commit 515751b1e1

@ -80,20 +80,20 @@ namespace NzbDrone.Core.Books.Calibre
var format = Path.GetExtension(file.Path); var format = Path.GetExtension(file.Path);
var bookData = Convert.ToBase64String(File.ReadAllBytes(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, Ext = format,
data_url = bookData Data = bookData
} }
} }
}, }
loaded_book_ids = new[] { file.CalibreId }
}; };
ExecuteSetFields(file.CalibreId, payload, settings); ExecuteSetFields(file.CalibreId, payload, settings);
@ -101,14 +101,13 @@ namespace NzbDrone.Core.Books.Calibre
public void RemoveFormats(int calibreId, IEnumerable<string> formats, 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 RemovedFormats = formats.ToList()
}, }
loaded_book_ids = new[] { calibreId }
}; };
ExecuteSetFields(calibreId, payload, settings); ExecuteSetFields(calibreId, payload, settings);
@ -117,6 +116,18 @@ namespace NzbDrone.Core.Books.Calibre
public void SetFields(BookFile file, CalibreSettings settings) public void SetFields(BookFile file, CalibreSettings settings)
{ {
var edition = file.Edition.Value; 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); var cover = edition.Images.FirstOrDefault(x => x.CoverType == MediaCoverTypes.Cover);
string image = null; string image = null;
@ -131,30 +142,32 @@ namespace NzbDrone.Core.Books.Calibre
} }
} }
var payload = new var payload = new CalibreChangesPayload
{ {
changes = new LoadedBookIds = new List<int> { file.CalibreId },
Changes = new CalibreChanges
{ {
title = edition.Title, Title = edition.Title,
authors = new[] { file.Author.Value.Name }, Authors = new List<string> { file.Author.Value.Name },
cover = image, Cover = image,
pubdate = edition.Book.Value.ReleaseDate, PubDate = book.ReleaseDate,
comments = edition.Overview, Comments = edition.Overview,
rating = edition.Ratings.Value * 2, Rating = edition.Ratings.Value * 2,
identifiers = new Dictionary<string, string> Identifiers = new Dictionary<string, string>
{ {
{ "isbn", edition.Isbn13 }, { "isbn", edition.Isbn13 },
{ "asin", edition.Asin }, { "asin", edition.Asin },
{ "goodreads", edition.ForeignEditionId } { "goodreads", edition.ForeignEditionId }
} },
}, Series = series?.Title,
loaded_book_ids = new[] { file.CalibreId } SeriesIndex = seriesIndex
}
}; };
ExecuteSetFields(file.CalibreId, payload, settings); 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) var builder = GetBuilder($"cdb/set-fields/{id}", settings)
.Post() .Post()

@ -32,7 +32,7 @@ namespace NzbDrone.Core.Books.Calibre
} }
public string Host { get; set; } public string Host { get; set; }
public int Port { get; set; } = 8080; public int Port { get; set; }
public string UrlBase { get; set; } public string UrlBase { get; set; }
public string Username { get; set; } public string Username { get; set; }
public string Password { get; set; } public string Password { get; set; }

@ -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 @@ namespace NzbDrone.Core.Datastore
.Where<Series>(s => s.Id == series.Id)).ToList(), .Where<Series>(s => s.Id == series.Id)).ToList(),
s => s.Id > 0); 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(); Mapper.Entity<AuthorMetadata>("AuthorMetadata").RegisterModel();
@ -135,7 +137,10 @@ namespace NzbDrone.Core.Datastore
new SqlBuilder() new SqlBuilder()
.Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id) .Join<Author, AuthorMetadata>((a, m) => a.AuthorMetadataId == m.Id)
.Where<Author>(a => a.AuthorMetadataId == book.AuthorMetadataId)).SingleOrDefault(), .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() Mapper.Entity<Edition>("Editions").RegisterModel()
.HasOne(r => r.Book, r => r.BookId) .HasOne(r => r.Book, r => r.BookId)

Loading…
Cancel
Save