Example #1
0
        public async Task <AuthorProfileGenerator.Response> ConvertAsync(Artifacts.EndActions endActions, CancellationToken cancellationToken)
        {
            var authorBio      = endActions.Data.AuthorBios?.Authors.FirstOrDefault();
            var firstAuthorRec = endActions.Data.AuthorRecs?.Recommendations.FirstOrDefault();

            var asin     = authorBio?.Asin ?? firstAuthorRec?.Asin;
            var bio      = authorBio?.Bio;
            var imageUrl = authorBio?.ImageUrl ?? firstAuthorRec?.ImageUrl;
            var name     = authorBio?.Name ?? firstAuthorRec?.Authors.FirstOrDefault();

            var otherBooks = Enumerable.Empty <BookInfo>();

            if (firstAuthorRec != null)
            {
                otherBooks = endActions.Data.AuthorRecs.Recommendations
                             .Select(rec => new BookInfo(rec.Title, rec.Authors.FirstOrDefault(), rec.Asin)
                {
                    Description  = rec.Description,
                    ImageUrl     = rec.ImageUrl,
                    AmazonRating = rec.AmazonRating,
                    Reviews      = rec.NumberOfReviews ?? 0
                });
            }

            Bitmap authorImage = null;

            if (!string.IsNullOrEmpty(imageUrl))
            {
                try
                {
                    _logger.Log("Downloading author image...");
                    authorImage = await _httpClient.GetImageAsync(imageUrl, cancellationToken : cancellationToken);
                }
                catch (Exception ex)
                {
                    _logger.Log($"An error occurred downloading the author image: {ex.Message}");
                }
            }

            return(new AuthorProfileGenerator.Response
            {
                Asin = !string.IsNullOrEmpty(asin) ? asin : null,
                Name = !string.IsNullOrEmpty(name) ? name : null,
                Biography = !string.IsNullOrEmpty(bio) ? bio : null,
                ImageUrl = !string.IsNullOrEmpty(imageUrl) ? imageUrl : null,
                OtherBooks = otherBooks.ToArray(),
                Image = authorImage
            });
        }
Example #2
0
        public EndActionsArtifactService(ILogger logger)
        {
            _logger = logger;

            try
            {
                var template = File.ReadAllText($"{AppDomain.CurrentDomain.BaseDirectory}dist/BaseEndActions.json", Encoding.UTF8);
                _baseEndActions = JsonConvert.DeserializeObject <Artifacts.EndActions>(template);
            }
            catch (FileNotFoundException)
            {
                throw new InitializationException("Unable to find dist/BaseEndActions.json, make sure it has been extracted!");
            }
            catch (Exception e)
            {
                throw new InitializationException($"An error occurred while loading dist/BaseEndActions.json (make sure any new versions have been extracted!)\r\n{e.Message}\r\n{e.StackTrace}", e);
            }
        }