// Search Goodreads for possible kindle edition of book and return ASIN. public async Task <string> SearchBookASINById(string id, CancellationToken cancellationToken = default) { try { var bookHtmlDoc = await _httpClient.GetPageAsync(BookUrl(id), cancellationToken); var link = bookHtmlDoc.DocumentNode.SelectSingleNode("//div[@class='otherEditionsActions']/a"); var match = Regex.Match(link.GetAttributeValue("href", ""), @"editions/([0-9]*)-"); if (match.Success) { var kindleEditionsUrl = string.Format("https://www.goodreads.com/work/editions/{0}?utf8=%E2%9C%93&sort=num_ratings&filter_by_format=Kindle+Edition", match.Groups[1].Value); bookHtmlDoc = await _httpClient.GetPageAsync(kindleEditionsUrl, cancellationToken); var bookNodes = bookHtmlDoc.DocumentNode.SelectNodes("//div[@class='elementList clearFix']"); if (bookNodes != null) { foreach (var book in bookNodes) { return(_amazonClient.ParseAsin(book.InnerHtml)); } } } return(""); } catch (Exception ex) { _logger.Log($"An error occurred while searching for {id}'s ASIN.\r\n{ex.Message}\r\n{ex.StackTrace}"); return(""); } }
private void btnOpenXml_Click(object sender, EventArgs e) { var openFile = new OpenFileDialog { Title = "Open XML or TXT file", Filter = "XML files (*.xml)|*.xml|TXT files (*.txt)|*.txt", InitialDirectory = $@"{Environment.CurrentDirectory}\xml\" }; if (openFile.ShowDialog() != DialogResult.OK) { return; } var filetype = Path.GetExtension(openFile.FileName); txtAsin.Text = _amazonClient.ParseAsin(openFile.FileName) ?? ""; try { switch (filetype) { case ".xml": _terms = XmlUtil.DeserializeFile <List <Term> >(openFile.FileName); break; case ".txt": _terms = _termsService.ReadTermsFromTxt(openFile.FileName).ToList(); break; default: MessageBox.Show($"Error: Bad file type \"{filetype}\""); break; } ReloadTerms(); } catch (Exception ex) { MessageBox.Show($"Error:\r\n{ex.Message}\r\n{ex.StackTrace}"); } }