Пример #1
0
        public override void Parse(XElement element)
        {
            var pairs = element.Descendants("series_work");

            if (pairs.Any())
            {
                var dict = new Dictionary <long, SeriesResource>();

                foreach (var pair in pairs)
                {
                    var series = new SeriesResource();
                    series.Parse(pair.Element("series"));

                    if (!dict.TryGetValue(series.Id, out var cached))
                    {
                        dict[series.Id] = series;
                        cached          = series;
                    }

                    var work = new WorkResource();
                    work.Parse(pair.Element("work"));
                    work.SetSeriesInfo(pair);

                    cached.Works.Add(work);
                }

                List = dict.Values.ToList();
            }
            else
            {
                List = new List <SeriesResource>();
            }
        }
Пример #2
0
        private Book MapSearchResult(WorkResource resource)
        {
            var book = _bookService.FindById(resource.Id.ToString());

            if (resource.BestBook != null)
            {
                var edition = _editionService.GetEditionByForeignEditionId(resource.BestBook.Id.ToString());

                if (edition == null)
                {
                    edition = new Edition
                    {
                        ForeignEditionId = resource.BestBook.Id.ToString(),
                        Title            = resource.BestBook.Title,
                        TitleSlug        = resource.BestBook.Id.ToString(),
                        Ratings          = new Ratings {
                            Votes = resource.RatingsCount, Value = resource.AverageRating
                        },
                    };
                }

                edition.Monitored = true;
                edition.ManualAdd = true;

                if (resource.BestBook.ImageUrl.IsNotNullOrWhiteSpace() && !NoPhotoRegex.IsMatch(resource.BestBook.ImageUrl))
                {
                    edition.Images.Add(new MediaCover.MediaCover
                    {
                        Url       = FullSizeImageRegex.Replace(resource.BestBook.ImageUrl),
                        CoverType = MediaCoverTypes.Cover
                    });
                }

                if (book == null)
                {
                    book = new Book
                    {
                        ForeignBookId = resource.Id.ToString(),
                        Title         = resource.BestBook.Title,
                        TitleSlug     = resource.Id.ToString(),
                        ReleaseDate   = resource.OriginalPublicationDate,
                        Ratings       = new Ratings {
                            Votes = resource.RatingsCount, Value = resource.AverageRating
                        },
                        AnyEditionOk = true
                    };
                }

                book.Editions = new List <Edition> {
                    edition
                };

                var authorId = resource.BestBook.AuthorId.ToString();
                var author   = _authorService.FindById(authorId);

                if (author == null)
                {
                    author = new Author
                    {
                        CleanName = Parser.Parser.CleanAuthorName(resource.BestBook.AuthorName),
                        Metadata  = new AuthorMetadata()
                        {
                            ForeignAuthorId = resource.BestBook.AuthorId.ToString(),
                            Name            = resource.BestBook.AuthorName,
                            TitleSlug       = resource.BestBook.AuthorId.ToString()
                        }
                    };
                }

                book.Author         = author;
                book.AuthorMetadata = book.Author.Value.Metadata.Value;
                book.CleanTitle     = book.Title.CleanAuthorName();
            }

            return(book);
        }
Пример #3
0
        // TODO: parse series information once I get a better sense
        // of what series are from the other API calls.
        //// public List<Series> Series { get; private set; }

        public override void Parse(XElement element)
        {
            Id    = element.ElementAsLong("id");
            Title = element.ElementAsString("title");
            TitleWithoutSeries = element.ElementAsString("title_without_series");
            Isbn          = element.ElementAsString("isbn");
            Isbn13        = element.ElementAsString("isbn13");
            Asin          = element.ElementAsString("asin");
            KindleAsin    = element.ElementAsString("kindle_asin");
            MarketplaceId = element.ElementAsString("marketplace_id");
            CountryCode   = element.ElementAsString("country_code");
            ImageUrl      = element.ElementAsString("large_image_url") ??
                            element.ElementAsString("image_url") ??
                            element.ElementAsString("small_image_url");
            PublicationDate    = element.ElementAsMultiDateField("publication");
            Publisher          = element.ElementAsString("publisher");
            LanguageCode       = element.ElementAsString("language_code");
            IsEbook            = element.ElementAsBool("is_ebook");
            Description        = element.ElementAsString("description");
            AverageRating      = element.ElementAsDecimal("average_rating");
            Pages              = element.ElementAsInt("num_pages");
            Format             = element.ElementAsString("format");
            EditionInformation = element.ElementAsString("edition_information");
            RatingsCount       = element.ElementAsInt("ratings_count");
            TextReviewsCount   = element.ElementAsInt("text_reviews_count");
            Url           = element.ElementAsString("link");
            EditionsUrl   = element.ElementAsString("editions_url");
            ReviewsWidget = element.ElementAsString("reviews_widget");

            var workElement = element.Element("work");

            if (workElement != null)
            {
                Work = new WorkResource();
                Work.Parse(workElement);
            }

            Authors      = element.ParseChildren <AuthorSummaryResource>("authors", "author");
            SimilarBooks = element.ParseChildren <BookSummaryResource>("similar_books", "book");

            var bookLinks = element.ParseChildren <BookLinkResource>("book_links", "book_link");

            if (bookLinks != null)
            {
                bookLinks.ForEach(x => x.FixBookLink(Id));
                BookLinks = bookLinks;
            }

            var buyLinks = element.ParseChildren <BookLinkResource>("buy_links", "buy_link");

            if (buyLinks != null)
            {
                buyLinks.ForEach(x => x.FixBookLink(Id));
                BuyLinks = buyLinks;
            }

            var shelves = element.ParseChildren(
                "popular_shelves",
                "shelf",
                (shelfElement) =>
            {
                var shelfName       = shelfElement?.Attribute("name")?.Value;
                var shelfCountValue = shelfElement?.Attribute("count")?.Value;

                int shelfCount = 0;
                int.TryParse(shelfCountValue, out shelfCount);
                return(new KeyValuePair <string, int>(shelfName, shelfCount));
            });

            if (shelves != null)
            {
                PopularShelves = shelves.GroupBy(obj => obj.Key).ToDictionary(shelf => shelf.Key, shelf => shelf.Sum(x => x.Value));
            }
        }