Ejemplo n.º 1
0
        private static Author MapAuthor(AuthorResource resource)
        {
            var metadata = MapAuthorMetadata(resource);

            var books = resource.Works
                        .Where(x => x.ForeignId > 0 && GetAuthorId(x) == resource.ForeignId)
                        .Select(MapBook)
                        .ToList();

            books.ForEach(x => x.AuthorMetadata = metadata);

            var series = resource.Series.Select(MapSeries).ToList();

            MapSeriesLinks(series, books, resource.Series);

            var result = new Author
            {
                Metadata  = metadata,
                CleanName = Parser.Parser.CleanAuthorName(metadata.Name),
                Books     = books,
                Series    = series
            };

            return(result);
        }
Ejemplo n.º 2
0
        private Author PollAuthorUncached(string foreignAuthorId)
        {
            AuthorResource resource = null;

            var useCache = true;

            for (var i = 0; i < 60; i++)
            {
                var httpRequest = _requestBuilder.GetRequestBuilder().Create()
                                  .SetSegment("route", $"author/{foreignAuthorId}")
                                  .Build();

                httpRequest.AllowAutoRedirect = true;
                httpRequest.SuppressHttpError = true;

                var httpResponse = _cachedHttpClient.Get(httpRequest, useCache, TimeSpan.FromMinutes(30));

                if (httpResponse.HasHttpError)
                {
                    if (httpResponse.StatusCode == HttpStatusCode.NotFound)
                    {
                        throw new AuthorNotFoundException(foreignAuthorId);
                    }
                    else if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
                    {
                        throw new BadRequestException(foreignAuthorId);
                    }
                    else
                    {
                        throw new BookInfoException("Unexpected error fetching author data");
                    }
                }

                resource = JsonSerializer.Deserialize <AuthorResource>(httpResponse.Content, SerializerSettings);

                if (resource.Works != null)
                {
                    resource.Works ??= new List <WorkResource>();
                    resource.Series ??= new List <SeriesResource>();
                    break;
                }

                useCache = false;
                Thread.Sleep(2000);
            }

            if (resource?.Works == null)
            {
                throw new BookInfoException($"Failed to get works for {foreignAuthorId}");
            }

            return(MapAuthor(resource));
        }
Ejemplo n.º 3
0
        private static AuthorMetadata MapAuthorMetadata(AuthorResource resource)
        {
            var metadata = new AuthorMetadata
            {
                ForeignAuthorId = resource.ForeignId.ToString(),
                TitleSlug       = resource.ForeignId.ToString(),
                Name            = resource.Name.CleanSpaces(),
                Overview        = resource.Description,
                Ratings         = new Ratings {
                    Votes = resource.RatingCount, Value = (decimal)resource.AverageRating
                },
                Status = AuthorStatusType.Continuing
            };

            metadata.SortName          = metadata.Name.ToLower();
            metadata.NameLastFirst     = metadata.Name.ToLastFirst();
            metadata.SortNameLastFirst = metadata.NameLastFirst.ToLower();

            if (resource.ImageUrl.IsNotNullOrWhiteSpace())
            {
                metadata.Images.Add(new MediaCover.MediaCover
                {
                    Url       = resource.ImageUrl,
                    CoverType = MediaCoverTypes.Poster
                });
            }

            if (resource.Url.IsNotNullOrWhiteSpace())
            {
                metadata.Links.Add(new Links {
                    Url = resource.Url, Name = "Goodreads"
                });
            }

            return(metadata);
        }