Esempio n. 1
0
        private async Task CheckAndFixIncorrectAsinOrThrowAsync(IMetadata metadata, string bookPath, CancellationToken cancellationToken)
        {
            if (AmazonClient.IsAsin(metadata.Asin))
            {
                return;
            }

            // todo add check for fixasin flag
            if (!metadata.CanModify)
            {
                throw new Exception($"Invalid Amazon ASIN detected: {metadata.Asin}!\r\nKindle may not display an X-Ray for this book.\r\nYou must either use Calibre's Quality Check plugin (Fix ASIN for Kindle Fire) or a MOBI editor (exth 113 and optionally 504) to change this.");
            }

            _logger.Log($"Invalid Amazon ASIN detected: {metadata.Asin}!\nKindle may not display an X-Ray for this book.\r\nAttempting to fix it automatically...");
            _logger.Log($"Searching Amazon for {metadata.Title} by {metadata.Author}...");
            // todo config tld
            var amazonSearchResult = await _amazonClient.SearchBook(metadata.Title, metadata.Author, "com", cancellationToken);

            if (amazonSearchResult != null)
            {
                metadata.SetAsin(amazonSearchResult.Asin);
#if NETCOREAPP3_1
                await using var fs = new FileStream(bookPath, FileMode.Create);
#else
                using var fs = new FileStream(bookPath, FileMode.Create);
#endif
                metadata.Save(fs);
                _logger.Log($"Successfully updated the ASIN to {metadata.Asin}! Be sure to copy this new version of the book to your Kindle device.");
            }
            else
            {
                _logger.Log("Unable to automatically find a matching ASIN for this book on Amazon :(");
            }
        }
Esempio n. 2
0
        public Task <Term[]> DownloadTermsAsync(string asin, string regionTld, CancellationToken cancellationToken)
        {
            return(HandleDownloadExceptionsAsync(async() =>
            {
                if (!AmazonClient.IsAsin(asin))
                {
                    return null;
                }

                var requestContent = new StringContent(JsonUtil.Serialize(new DownloadRequest
                {
                    Asin = asin,
                    Type = DownloadRequest.TypeEnum.Terms,
                    RegionTld = regionTld
                }), Encoding.UTF8, "application/json");
                var request = new HttpRequestMessage(HttpMethod.Post, $"{BaseUrl}{DownloadEndpoint}")
                {
                    Content = requestContent
                };
                // Increase timeout for terms requests
                request.SetTimeout(TimeSpan.FromSeconds(30));
                var response = await _httpClient.SendAsync(request, cancellationToken);
                var responseStream = await response.Content.ReadAsStreamAsync();
                var responseString = await new StreamReader(responseStream, Encoding.UTF8).ReadToEndAsync();
                return XmlUtil.Deserialize <Term[]>(responseString);
            }));
        }
Esempio n. 3
0
        private bool CheckAsin()
        {
            if (AmazonClient.IsAsin(tbAsin.Text))
            {
                return(true);
            }

            MessageBox.Show("This does not appear to be a valid ASIN.\r\nAre you sure it is correct?", "Invalid ASIN", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
            return(false);
        }
        private async void btnDownloadTerms_Click(object sender, EventArgs e)
        {
            if (!AmazonClient.IsAsin(txtAsin.Text))
            {
                MessageBox.Show($"'{txtAsin.Text} is not a valid ASIN.\r\nRoentgen requires one!");
                return;
            }

            ToggleInterface(false);
            await DownloadTermsAsync(txtAsin.Text);

            ToggleInterface(true);
        }
Esempio n. 5
0
 public static void IncorrectAsinPromptOrThrow(string asin)
 {
     if (!AmazonClient.IsAsin(asin) &&
         DialogResult.No == MessageBox.Show($"Incorrect ASIN detected: {asin}!\n" +
                                            "Kindle may not display an X-Ray for this book.\n" +
                                            "Do you wish to continue?", "Incorrect ASIN", MessageBoxButtons.YesNo))
     {
         throw new Exception($"Incorrect ASIN detected: {asin}!\r\n" +
                             "Kindle may not display an X-Ray for this book.\r\n" +
                             "You must either use Calibre's Quality Check plugin (Fix ASIN for Kindle Fire) " +
                             "or a MOBI editor (exth 113 and optionally 504) to change this.");
     }
 }
Esempio n. 6
0
        private async Task <T> DownloadArtifactAsync <T>(string asin, string regionTld, DownloadRequest.TypeEnum type, CancellationToken cancellationToken) where T : class
        {
            if (!AmazonClient.IsAsin(asin))
            {
                return(null);
            }

            var request = new StringContent(JsonUtil.Serialize(new DownloadRequest
            {
                Asin      = asin,
                Type      = type,
                RegionTld = regionTld
            }), Encoding.UTF8, "application/json");
            var response = await _httpClient.PostAsync($"{BaseUrl}{DownloadEndpoint}", request, cancellationToken);

            var responseStream = await response.Content.ReadAsStreamAsync();

            var responseString = await new StreamReader(responseStream, Encoding.UTF8).ReadToEndAsync();

            return(JsonUtil.Deserialize <T>(responseString, false));
        }