Exemple #1
0
        /// <summary>
        /// Fetch the epub file for the given book and write it to the standard RoseGarden/Downloads folder.
        /// If desired, fetch the image file and also write it to the standard RoseGarden/Downloads folder.
        /// Also write the catalog entry as a single-entry OPDS catalog file.
        /// </summary>
        private void FetchBook(XmlElement entry)
        {
            var pathEpub  = ComputeEpubPathFromEntryTitle(entry);
            var pathImage = Path.ChangeExtension(pathEpub, "jpg");
            var pathOpds  = Path.ChangeExtension(pathEpub, "opds");
            var obsolete  = false;

            if (File.Exists(pathOpds))
            {
                obsolete = IsExistingDownloadObsolete(entry, pathOpds);
            }
            if (!File.Exists(pathEpub) || !File.Exists(pathOpds) || obsolete)
            {
                _opdsClient.DownloadBook(entry, "epub", _feedTitle, pathEpub);
                if (_options.DownloadImage)
                {
                    _opdsClient.DownloadImage(entry, pathImage);
                }
                _opdsClient.WriteCatalogEntryFile(_rootCatalog, entry, pathOpds);
            }
            else
            {
                Console.WriteLine("INFO: skipping download of \"{0}\" because we apparently already have it.", Path.GetFileName(pathEpub));
            }
        }
        private int FetchAndSaveBook()
        {
            var catalog = (String.IsNullOrWhiteSpace(_options.LanguageName) && String.IsNullOrWhiteSpace(_options.Publisher)) ? _rootCatalog : _filteredCatalog;
            var entries = catalog.DocumentElement.SelectNodes($"/a:feed/a:entry/a:title[text()='{_options.BookTitle}']", _nsmgr);

            if (entries.Count == 0)
            {
                Console.WriteLine("WARNING: Could not find a book with the title \"{0}\"", _options.BookTitle);
                return(2);
            }
            XmlElement bookEntry = null;

            if (entries.Count > 1 && !String.IsNullOrWhiteSpace(_options.Author))
            {
                foreach (var entry in entries.Cast <XmlElement>())
                {
                    var parent = entry.ParentNode;
                    var author = parent.SelectSingleNode($"./a:author/a:name[text()='{_options.Author}'");
                    if (author != null)
                    {
                        bookEntry = parent as XmlElement;
                        break;
                    }
                }
                if (bookEntry == null)
                {
                    Console.WriteLine("WARNING: Could not find a book written by {0} with the title \"{1}\"", _options.Author, _options.BookTitle);
                    return(2);
                }
            }
            else
            {
                bookEntry = entries[0].ParentNode as XmlElement;
            }
            var ret = DownloadBook(bookEntry, "epub");

            if (ret != 0)
            {
                return(ret);
            }
            if (_options.DownloadPDF)
            {
                ret = DownloadBook(bookEntry, "pdf");
                if (ret != 0)
                {
                    return(ret);
                }
            }
            if (_options.DownloadThumbnail)
            {
                // The actual returned file may well be jpeg even if advertised as image/png, so assume jpeg until we
                // see the data.
                string path;
                if (String.IsNullOrEmpty(_options.OutputFile))
                {
                    path = Path.Combine(OpdsClient.DownloadFolder, Program.SanitizeNameForFileSystem(_options.BookTitle) + ".thumb.jpg");
                }
                else
                {
                    path = Path.ChangeExtension(_options.OutputFile, "thumb.jpg");
                }
                ret = _opdsClient.DownloadThumbnail(bookEntry, path);
                if (ret != 0)
                {
                    return(ret);
                }
            }
            if (_options.DownloadImage)
            {
                // The actual returned file may well be jpeg even if advertised as image/png, so assume jpeg until we
                // see the data.
                string path;
                if (String.IsNullOrEmpty(_options.OutputFile))
                {
                    path = Path.Combine(OpdsClient.DownloadFolder, Program.SanitizeNameForFileSystem(_options.BookTitle) + ".jpg");
                }
                else
                {
                    path = Path.ChangeExtension(_options.OutputFile, "jpg");
                }
                ret = _opdsClient.DownloadImage(bookEntry, path);
                if (ret != 0)
                {
                    return(ret);
                }
            }
            WriteCatalogEntryFile(bookEntry);
            return(ret);
        }