Exemplo n.º 1
0
        public void AddFormat(BookFile file, CalibreSettings settings)
        {
            var format   = Path.GetExtension(file.Path);
            var bookData = Convert.ToBase64String(File.ReadAllBytes(file.Path));

            var payload = new CalibreChangesPayload
            {
                LoadedBookIds = new List <int> {
                    file.CalibreId
                },
                Changes = new CalibreChanges
                {
                    AddedFormats = new List <CalibreAddFormat>
                    {
                        new CalibreAddFormat
                        {
                            Ext  = format,
                            Data = bookData
                        }
                    }
                }
            };

            ExecuteSetFields(file.CalibreId, payload, settings);
        }
Exemplo n.º 2
0
        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;

            if (cover != null)
            {
                var imageFile = _mediaCoverService.GetCoverPath(edition.BookId, MediaCoverEntity.Book, cover.CoverType, cover.Extension, null);

                if (File.Exists(imageFile))
                {
                    var imageData = File.ReadAllBytes(imageFile);
                    image = Convert.ToBase64String(imageData);
                }
            }

            var payload = new CalibreChangesPayload
            {
                LoadedBookIds = new List <int> {
                    file.CalibreId
                },
                Changes = new CalibreChanges
                {
                    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 }
                    },
                    Series      = series?.Title,
                    SeriesIndex = seriesIndex
                }
            };

            ExecuteSetFields(file.CalibreId, payload, settings);
        }
Exemplo n.º 3
0
        private void ExecuteSetFields(int id, CalibreChangesPayload payload, CalibreSettings settings)
        {
            var builder = GetBuilder($"cdb/set-fields/{id}", settings)
                          .Post()
                          .SetHeader("Content-Type", "application/json");

            var request = builder.Build();

            request.SetContent(payload.ToJson());

            _httpClient.Execute(request);
        }
Exemplo n.º 4
0
        public void RemoveFormats(int calibreId, IEnumerable <string> formats, CalibreSettings settings)
        {
            var payload = new CalibreChangesPayload
            {
                LoadedBookIds = new List <int> {
                    calibreId
                },
                Changes = new CalibreChanges
                {
                    RemovedFormats = formats.ToList()
                }
            };

            ExecuteSetFields(calibreId, payload, settings);
        }
Exemplo n.º 5
0
        public void SetFields(BookFile file, CalibreSettings settings, bool updateCover = true, bool embed = false)
        {
            var edition    = file.Edition.Value;
            var book       = edition.Book.Value;
            var serieslink = book.SeriesLinks.Value.FirstOrDefault(x => x.Series.Value.Title.IsNotNullOrWhiteSpace());

            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;

            if (cover != null)
            {
                var imageFile = _mediaCoverService.GetCoverPath(edition.BookId, MediaCoverEntity.Book, cover.CoverType, cover.Extension, null);

                if (File.Exists(imageFile))
                {
                    var imageData = File.ReadAllBytes(imageFile);
                    image = Convert.ToBase64String(imageData);
                }
            }

            var payload = new CalibreChangesPayload
            {
                LoadedBookIds = new List <int> {
                    file.CalibreId
                },
                Changes = new CalibreChanges
                {
                    Title   = edition.Title,
                    Authors = new List <string> {
                        file.Author.Value.Name
                    },
                    Cover       = updateCover ? image : null,
                    PubDate     = book.ReleaseDate,
                    Publisher   = edition.Publisher,
                    Languages   = edition.Language.CanonicalizeLanguage(),
                    Comments    = edition.Overview,
                    Rating      = (int)(edition.Ratings.Value * 2),
                    Identifiers = new Dictionary <string, string>
                    {
                        { "isbn", edition.Isbn13 },
                        { "asin", edition.Asin },
                        { "goodreads", edition.ForeignEditionId }
                    },
                    Series      = series?.Title,
                    SeriesIndex = seriesIndex
                }
            };

            ExecuteSetFields(file.CalibreId, payload, settings);

            if (embed)
            {
                _rootFolderWatchingService.ReportFileSystemChangeBeginning(file.Path);
                EmbedMetadata(file.CalibreId, settings);
            }
        }
Exemplo n.º 6
0
        public void SetFields(BookFile file, CalibreSettings settings, bool updateCover = true, bool embed = false)
        {
            var edition    = file.Edition.Value;
            var book       = edition.Book.Value;
            var serieslink = book.SeriesLinks.Value.OrderBy(x => x.SeriesPosition).FirstOrDefault(x => x.Series.Value.Title.IsNotNullOrWhiteSpace());

            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;

            if (cover != null)
            {
                var imageFile = _mediaCoverService.GetCoverPath(edition.BookId, MediaCoverEntity.Book, cover.CoverType, cover.Extension, null);

                if (File.Exists(imageFile))
                {
                    var imageData = File.ReadAllBytes(imageFile);
                    if (CalibreImageValidator.IsValidImage(imageData))
                    {
                        image = Convert.ToBase64String(imageData);
                    }
                }
            }

            var textInfo = CultureInfo.InvariantCulture.TextInfo;
            var genres   = book.Genres.Select(x => textInfo.ToTitleCase(x.Replace('-', ' '))).ToList();

            var payload = new CalibreChangesPayload
            {
                LoadedBookIds = new List <int> {
                    file.CalibreId
                },
                Changes = new CalibreChanges
                {
                    Title   = edition.Title,
                    Authors = new List <string> {
                        file.Author.Value.Name
                    },
                    Cover       = updateCover ? image : null,
                    PubDate     = book.ReleaseDate,
                    Publisher   = edition.Publisher,
                    Languages   = edition.Language.CanonicalizeLanguage(),
                    Tags        = genres,
                    Comments    = edition.Overview,
                    Rating      = (int)(edition.Ratings.Value * 2),
                    Identifiers = new Dictionary <string, string>
                    {
                        { "isbn", edition.Isbn13 },
                        { "asin", edition.Asin },
                        { "goodreads", edition.ForeignEditionId }
                    },
                    Series      = series?.Title,
                    SeriesIndex = seriesIndex
                }
            };

            ExecuteSetFields(file.CalibreId, payload, settings);

            // updating the calibre metadata may have renamed the file, so track that
            var updated = GetBook(file.CalibreId, settings);

            var updatedPath = GetOriginalFormat(updated.Formats);

            if (updatedPath != file.Path)
            {
                _rootFolderWatchingService.ReportFileSystemChangeBeginning(updatedPath);
                file.Path = updatedPath;
            }

            var fileInfo = new FileInfo(file.Path);

            file.Size     = fileInfo.Length;
            file.Modified = fileInfo.LastWriteTimeUtc;

            if (file.Id > 0)
            {
                _mediaFileService.Update(file);
            }

            if (embed)
            {
                EmbedMetadata(file, settings);
            }
        }