Esempio n. 1
0
        public static CatalogFolderModel ToFolder(this CatalitFb2BooksDto booksDto, string authorizationString)
        {
            var folderModel = new CatalogFolderModel {Items = new List<CatalogItemModel>()};

            if (booksDto.Books == null || !booksDto.Books.Any())
            {
                return folderModel;
            }

            foreach (var fb2BookDto in booksDto.Books)
            {
                var bookCatalogItem = new CatalogBookItemModel();

                //TODO: change buy url and download links
                //bookCatalogItem.AcquisitionLink = new BookAcquisitionLinkModel
                //    {
                //        Type = ".fb2",
                //        Prices = new List<BookPriceModel> {new BookPriceModel {CurrencyCode = "RUB", Price = fb2BookDto.Price}},
                //        Url = "http://www.someurl.com/"
                //    };
                bookCatalogItem.Links = new List<BookDownloadLinkModel>
                    {
                        new BookDownloadLinkModel
                            {
                                Type = ".fb2.zip", 
                                Url = CreateDownloadUrl(fb2BookDto, authorizationString)
                            }
                    };

                bookCatalogItem.Id = fb2BookDto.Id.ToString(CultureInfo.InvariantCulture);
                //bookCatalogItem.Id = string.Concat(fb2BookDto.Id.ToString(CultureInfo.InvariantCulture), "|",
                //                                   fb2BookDto.Description.Hidden.DocumentInfo.Id);

                // title
                bookCatalogItem.Title = fb2BookDto.Description.Hidden.TitleInfo.BookTitle;

                // author
                bookCatalogItem.Author = CreateAuthorFullName(fb2BookDto.Description.Hidden.TitleInfo.Author);

                // cover image
                bookCatalogItem.ImageUrl = new Uri(fb2BookDto.ImageCover);

                folderModel.Items.Add(bookCatalogItem);
            }

            return folderModel;
        }
Esempio n. 2
0
        public OpdsCatalogReader(CatalogModel catalogModel, IStorageStateSaver storageStateSaver, 
            IWebClient webClient)
        {
            CatalogModel = catalogModel;
            _storageStateSaver = storageStateSaver;
            WebClient = webClient;

            _opdsFilters = new IOpdsBadFormatFilter[]
                               {
                                   new UnescapedAmpersandsFilter(), 
                                   new UnescapedCdataFilter(),
                                   new UnescapedQuotesFilter(), 
                                   new UnescapedSignsFilter(),
                                   new UnescapedHtmlDescriptionFilter(),
                                   new AcquisitionBuyFilter(),
                                   new UrlWithoutProtocolFilter(), 
                               };

            CurrentFolder = new CatalogFolderModel
                                 {
                                     BaseUrl = catalogModel.Url,
                                     Items = new List<CatalogItemModel>()
                                 };
        }
Esempio n. 3
0
        public void LoadState(string ownerKey = null)
        {
            var navigationStack = _storageStateSaver.Restore<List<CatalogFolderModel>>(CreateStorageKey(NAVIGATION_STACK_KEY, ownerKey));
            if (navigationStack != null && navigationStack.Count > 0)
            {
                NavigationStack.Clear();
                for (var i = navigationStack.Count - 1; i >= 0; --i)
                {
                    NavigationStack.Push(navigationStack[i]);
                }
            }

            CurrentFolder = _storageStateSaver.Restore<CatalogFolderModel>(CreateStorageKey(CURRENT_FOLDER_KEY, ownerKey));
        }
Esempio n. 4
0
 protected void UpdateCurrentFolder(CatalogFolderModel folder)
 {
     if (folder.BaseUrl != null && folder.BaseUrl != CurrentFolder.BaseUrl)
     {
         return;
     }
     CurrentFolder.Items = folder.Items;
     CurrentFolder.NextPageUrl = folder.NextPageUrl;
 }
Esempio n. 5
0
        public void GoBack()
        {
            if (!NavigationStack.Any())
            {
                throw new ReadCatalogException("Unable go back");
            }

            //cancel active requests
            WebClient.Cancel();
            
            CurrentFolder = NavigationStack.Pop();
        }
Esempio n. 6
0
        public void GoTo(CatalogItemModel catalogItem)
        {
            //cancel active requests
            //WebClient.Cancel();
            WebClient.Cancel();

            NavigationStack.Push(CurrentFolder);
            CurrentFolder = new CatalogFolderModel
                                    {
                                        Items = new List<CatalogItemModel>(),
                                        BaseUrl = catalogItem.OpdsUrl,
                                        CurrentRepresentationItem =  catalogItem
                                    };
        }
Esempio n. 7
0
        public async Task<IEnumerable<CatalogItemModel>> SearchAsync(string query)
        {
            NavigationStack.Clear();
            CurrentFolder = new CatalogFolderModel
                {
                    BaseUrl = string.Format(SearchUrl, HttpUtility.HtmlEncode(query)),
                    Items = new List<CatalogItemModel>()
                };

            if (string.IsNullOrEmpty(SearchUrl))
            {
                return Enumerable.Empty<CatalogItemModel>();
            }

            return await LoadItemsAsync(string.Format(SearchUrl, HttpUtility.HtmlEncode(query)));
        }
        public void GoBack()
        {
            if (!_navigationStack.Any())
            {
                throw new ReadCatalogException("Unable go back");
            }

            //cancel active requests
            _cancelSource.Cancel();

            _currentFolder = _navigationStack.Pop();
        }
        public void GoTo(CatalogItemModel model)
        {

            //cancel active requests
            _cancelSource.Cancel();

            _navigationStack.Push(_currentFolder);
            _currentFolder = new CatalogFolderModel
            {
                Items = new List<CatalogItemModel>(),
                BaseUrl = model.OpdsUrl + "/files"
            };
        }
Esempio n. 10
0
        public static CatalogFolderModel ToFolder(this CatalogContentDto catalogContentDto, string authorityUrl, CatalogType type, int catalogId)
        {
            var folderModel = new CatalogFolderModel();
            var folderItems = new List<CatalogItemModel>();

            if (catalogContentDto.Links != null)
            {
                // pagination, next page
                var nextPageLink = catalogContentDto.Links.SingleOrDefault(l => !string.IsNullOrEmpty(l.Rel) && l.Rel.Equals("next")
                                                                            && !string.IsNullOrEmpty(l.Type) &&
                                                                            (l.Type.Contains(CATALOG_LINK_TYPE_PREFIX) || (l.Type.Contains(CATALOG_LINK_TYPE))));
                if (nextPageLink != null)
                {
                    folderModel.NextPageUrl = GetValidUrl(nextPageLink.Href, authorityUrl);
                }
            }

            if (catalogContentDto.Entries != null)
            {
                foreach (var entryDto in catalogContentDto.Entries)
                {
                    CatalogItemModel model = null;

                    // book or just folder
                    var links = entryDto.Links.Where(e =>
                        {
                            if (string.IsNullOrEmpty(e.Rel) || (!e.Rel.StartsWith(REL_ACQUISITION_PREFIX)))
                            {
                                if (string.IsNullOrEmpty(e.Type) 
                                    || !(e.Type.StartsWith(APPLICATION_PREFIX) && FormatConstants.Any(fc => e.Type.Contains(fc)) && !ApparentlyIgnoredFormatConstants.Any(fc => e.Type.Contains(fc))))
                                {
                                    return false;
                                }
                                return true;
                            }

                            return !string.IsNullOrEmpty(e.Type); // && FormatConstants.Any(formatConstant => e.Type.Contains(formatConstant));
                        });

                    if (links.Count() != 0)
                    {
                        // links with price
                        var priceLink = links.SingleOrDefault(l => REL_ACQUISITION_BUY_PREFIX.Equals(l.Rel) && !string.IsNullOrEmpty(l.Href)
                                                                   && l.Prices != null && l.Prices.Any(p => !p.Price.Equals("0.00")));
                        
                        // download links for diff. formats
                        var downloadLinks = (from linkDto in links
                                             where !string.IsNullOrEmpty(linkDto.Type) && !string.IsNullOrEmpty(linkDto.Href) && !REL_ACQUISITION_BUY_PREFIX.Equals(linkDto.Rel)
                                             select new BookDownloadLinkModel
                                                 {
                                                     Type = linkDto.Type, Url = GetValidUrl(linkDto.Href, authorityUrl, true)
                                                 }).Where(dl => FormatConstants.Any(fc => dl.Type.Contains(fc))).ToList();

                        // if there are now supported formats in downloadLinks, but there were some acquisition links with another formats => skip this book.
                        if (!downloadLinks.Any() && priceLink == null)
                        {
                            var htmlBuyLink = links.SingleOrDefault(l => REL_ACQUISITION_BUY_PREFIX.Equals(l.Rel));
                            if (htmlBuyLink == null)
                            {
                                continue;
                            }
                            model = new CatalogItemModel {HtmlUrl = GetValidUrl(htmlBuyLink.Href, authorityUrl)};
                        }
                        else
                        {
                            // this is book
                            BookAcquisitionLinkModel acquisitionLink = null;
                            if (priceLink != null && priceLink.Prices.Any(p => !p.Price.Equals("0.00")))
                            {
                                acquisitionLink = new BookAcquisitionLinkModel
                                {
                                    Type = !string.IsNullOrEmpty(priceLink.DcFormat) ? priceLink.DcFormat : priceLink.Type,
                                    Prices = priceLink.Prices != null ? priceLink.Prices.Select(p => new BookPriceModel { CurrencyCode = p.CurrencyCode, Price = p.Price })
                                                                                        .ToList() : null,
                                    Url = GetValidUrl(priceLink.Href, authorityUrl)
                                };
                            }

                            var id = string.IsNullOrEmpty(entryDto.Id) ? string.Concat(catalogId, "-", entryDto.Title) : entryDto.Id;

                            model = new CatalogBookItemModel
                            {
                                AcquisitionLink = acquisitionLink,
                                Links = downloadLinks,
                                Id = id,
                                TrialLink = type == CatalogType.Litres ? CreateTrialLink(entryDto.Id) : null
                            };
                        }
                    }
                    // check for Litres bookshelf
                    else if (entryDto.Links.Any(l => LITRES_REL_BOOKSHELF_PREFIX.Equals(l.Rel)))
                    {
                        model = new LitresBookshelfCatalogItemModel();
                    }
                    // check for Litres topup
                    else if (entryDto.Links.Any(l => LITRES_REL_TOPUP_PREFIX.Equals(l.Rel)))
                    {
                        model = new LitresTopupCatalogItemModel
                            {
                                HtmlUrl = LITRES_PUT_MONEY_LINK_FORMAT,
                                Title = LITRES_REL_TOPUP_TITLE
                            };
                    }
                    else
                    {
                        // this is default folder
                        if (model == null)
                        {
                            model = new CatalogItemModel();
                        }
                    }

                    // title
                    if (string.IsNullOrEmpty(model.Title))
                    {
                        if (entryDto.Title == null)
                        {
                            continue;
                        }
                        if (!string.IsNullOrEmpty(entryDto.Title.Text))
                        {
                            model.Title = entryDto.Title.Text;
                        }
                        else if (!string.IsNullOrEmpty(entryDto.Title.DivValue))
                        {
                            model.Title = entryDto.Title.DivValue;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    // description
                    model.Description = entryDto.Content != null && !string.IsNullOrEmpty(entryDto.Content.Value)
                                            ? entryDto.Content.Value
                                            : string.Empty;

                    // author
                    model.Author = entryDto.Author != null && !string.IsNullOrEmpty(entryDto.Author.Name)
                                        ? entryDto.Author.Name
                                        : string.Empty;

                    // opds catalog url
                    if (!(model is LitresTopupCatalogItemModel) && string.IsNullOrEmpty(model.HtmlUrl))
                    {
                        var catalogLink = entryDto.Links.FirstOrDefault(l => !string.IsNullOrEmpty(l.Type) && (l.Type.Contains(CATALOG_LINK_TYPE_PREFIX) || l.Type.Contains(CATALOG_LINK_TYPE) || l.Type.Contains(LITRES_CATALOG_LINK_TYPE_)));
                        if (catalogLink != null)
                        {
                            model.OpdsUrl = GetValidUrl(catalogLink.Href, authorityUrl);
                        }
                    }

                    // html url, to open in browser
                    var htmlLink = entryDto.Links.FirstOrDefault(l => HTML_TEXT_LINK_TYPE.Equals(l.Type) && !string.IsNullOrEmpty(l.Href));
                    if (htmlLink != null)
                    {
                        if (string.IsNullOrEmpty(model.HtmlUrl))
                        {
                            model.HtmlUrl = GetValidUrl(htmlLink.Href, authorityUrl);
                        }
                    }

                    //image
                    var imageLinks = entryDto.Links.Where(l => !string.IsNullOrEmpty(l.Type) && l.Type.Equals(IMAGE_LINK_TYPE));
                    if (imageLinks.Any())
                    {
                        if (imageLinks.Count() > 1)
                        {
                            var imageLink = imageLinks.SingleOrDefault(l => l.Rel.Equals(REL_IMAGE_PREFIX));
                            if (imageLink != null)
                            {
                                model.ImageUrl = new Uri(GetValidUrl(imageLink.Href, authorityUrl, true));
                            }
                        }
                        else
                        {
                            model.ImageUrl = new Uri(GetValidUrl(imageLinks.First().Href, authorityUrl, true));
                        }
                    }

                    // decoding of description & title
                    model.Description = HttpUtility.HtmlDecode(model.Description);
                    model.Title = HttpUtility.HtmlDecode(model.Title);
                    model.Author = HttpUtility.HtmlDecode(model.Author);

                    if (model.Description.Contains("<") && model.Description.Contains(">"))
                    {
                        model.Description = HtmlToText.Convert(model.Description);
                    }
                    model.Description = model.Description.Trim();
                    if (model.Author.Contains("<") && model.Author.Contains(">"))
                    {
                        model.Author = HtmlToText.Convert(model.Author);
                    }

                    folderItems.Add(model);
                }
            }

            folderModel.Items = folderItems;
            return folderModel;
        }