private void btnExtractTerms_Click(object sender, EventArgs e) { string selPath = UIFunctions.GetFile("Open a Kindle X-Ray file...", "", "ASC files|*.asc", _settings.outDir); if (selPath == "" || !selPath.Contains("XRAY.entities")) { Logger.Log("Invalid or no file selected."); return; } var newVer = XRayUtil.CheckXRayVersion(selPath); if (newVer == XRayUtil.XRayVersion.Invalid) { Logger.Log("Invalid X-Ray file."); return; } try { var terms = newVer == XRayUtil.XRayVersion.New ? XRayUtil.ExtractTermsNew(new SQLiteConnection($"Data Source={selPath}; Version=3;"), true) : XRayUtil.ExtractTermsOld(selPath); if (!Directory.Exists(Environment.CurrentDirectory + @"\xml\")) { Directory.CreateDirectory(Environment.CurrentDirectory + @"\xml\"); } string outfile = Environment.CurrentDirectory + @"\xml\" + Path.GetFileNameWithoutExtension(selPath) + ".xml"; Functions.Save(terms.ToList(), outfile); Logger.Log("Character data has been successfully extracted and saved to: " + outfile); } catch (Exception ex) { Logger.Log("Error:\r\n" + ex.Message + "\r\n" + ex.StackTrace); } }
private async void txtMobi_TextChanged(object sender, EventArgs e) { if (txtMobi.Text == "" || !File.Exists(txtMobi.Text)) { return; } txtGoodreads.Text = ""; prgBar.Value = 0; var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.outDir, false)); if (metadata == null) { txtMobi.Text = ""; return; } metadata.CheckDRM(); pbCover.Image = (Image)metadata.coverImage?.Clone(); lblTitle.Visible = true; lblAuthor.Visible = true; lblAsin.Visible = true; txtTitle.Visible = true; txtAuthor.Visible = true; txtAsin.Visible = true; txtAuthor.Text = metadata.Author; txtTitle.Text = metadata.Title; txtAsin.Text = metadata.ASIN; _tooltip.SetToolTip(txtAsin, AmazonUrl(txtAsin.Text)); openBook.Clear(); openBook.Add(metadata.Author); openBook.Add(metadata.Title); openBook.Add(metadata.ASIN); checkFiles(metadata.Author, metadata.Title, metadata.ASIN); try { // Directory.Delete(randomFile, true); } catch (Exception ex) { Logger.Log($"An error occurred while trying to delete temporary files: {ex.Message}\r\n{ex.StackTrace}\r\n" + "Try deleting these files manually."); } }
private void btnBrowseOut_Click(object sender, EventArgs e) { txtOut.Text = UIFunctions.GetDir(txtOut.Text); }
private void btnBrowseMobi_Click(object sender, EventArgs e) { txtMobi.Text = ""; txtMobi.Text = UIFunctions.GetBook(txtMobi.Text); }
private async void tmiXray_Click(object sender, EventArgs e) { await UIFunctions.ShowPreview(Filetype.XRay, ApPath, _settings.outDir); }
private async void tmiAuthorProfile_Click(object sender, EventArgs e) { await UIFunctions.ShowPreview(Filetype.AuthorProfile, ApPath, _settings.outDir); }
private async Task btnSearchGoodreads_Run() { if (!File.Exists(txtMobi.Text)) { MessageBox.Show("Specified book was not found.", "Book Not Found"); return; } if (!Directory.Exists(_settings.outDir)) { MessageBox.Show("Specified output directory does not exist. Please review the settings page.", "Output Directory Not found"); return; } //this.TopMost = true; using (var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.outDir, false))) { if (metadata == null) { return; } try { List <BookInfo> books = await _dataSource.SearchBook(metadata.Author, metadata.Title); string bookUrl; if (books?.Count > 1) { Logger.Log($"Warning: Multiple results returned from {_dataSource.Name}..."); var frmG = new frmGR { BookList = books }; frmG.ShowDialog(); bookUrl = books[frmG.cbResults.SelectedIndex].dataUrl; } else if (books?.Count == 1) { bookUrl = books[0].dataUrl; } else { Logger.Log($"Unable to find this book on {_dataSource.Name}!"); return; } if (!string.IsNullOrEmpty(bookUrl)) { txtGoodreads.Text = bookUrl; txtGoodreads.Refresh(); Logger.Log( $"Book found on {_dataSource.Name}!\r\n{metadata.Title} by {metadata.Author}\r\n{_dataSource.Name} URL: {bookUrl}\r\n" + "You may want to visit the URL to ensure it is correct."); } } catch (Exception ex) { Logger.Log("An error occurred while searching: " + ex.Message + "\r\n" + ex.StackTrace); } } }
private async Task btnKindleExtras_Run() { //Check current settings if (!File.Exists(txtMobi.Text)) { MessageBox.Show("Specified book was not found.", "Book Not Found"); return; } if (rdoGoodreads.Checked) { if (txtGoodreads.Text == "") { MessageBox.Show($"No {_dataSource.Name} link was specified.", $"Missing {_dataSource.Name} Link"); return; } if (!txtGoodreads.Text.ToLower().Contains(_settings.dataSource.ToLower())) { MessageBox.Show($"Invalid {_dataSource.Name} link was specified.\r\n" + $"If you do not want to use {_dataSource.Name}, you can change the data source in Settings." , $"Invalid {_dataSource.Name} Link"); return; } } if (_settings.realName.Trim().Length == 0 || _settings.penName.Trim().Length == 0) { MessageBox.Show( "Both Real and Pen names are required for End Action\r\n" + "file creation. This information allows you to rate this\r\n" + "book on Amazon. Please review the settings page.", "Amazon Customer Details Not found"); return; } var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.outDir, _settings.saverawml)); if (metadata == null) { return; } SetDatasourceLabels(); // Reset the dataSource for the new build process Logger.Log($"Book's {_dataSource.Name} URL: {txtGoodreads.Text}"); try { BookInfo bookInfo = new BookInfo(metadata, txtGoodreads.Text); string outputDir = OutputDirectory(bookInfo.author, bookInfo.sidecarName, true); Logger.Log("Attempting to build Author Profile..."); AuthorProfile ap = new AuthorProfile(bookInfo, new AuthorProfile.Settings { AmazonTld = _settings.amazonTLD, Android = _settings.android, OutDir = _settings.outDir, SaveBio = _settings.saveBio, UseNewVersion = _settings.useNewVersion, UseSubDirectories = _settings.useSubDirectories }); if (!await ap.Generate()) { return; } SaPath = $@"{outputDir}\StartActions.data.{bookInfo.asin}.asc"; ApPath = $@"{outputDir}\AuthorProfile.profile.{bookInfo.asin}.asc"; Logger.Log("Attempting to build Start Actions and End Actions..."); EndActions ea = new EndActions(ap, bookInfo, metadata.RawMlSize, _dataSource, new EndActions.Settings { AmazonTld = _settings.amazonTLD, Android = _settings.android, OutDir = _settings.outDir, PenName = _settings.penName, RealName = _settings.realName, UseNewVersion = _settings.useNewVersion, UseSubDirectories = _settings.useSubDirectories }); if (!await ea.Generate()) { return; } if (_settings.useNewVersion) { await ea.GenerateNewFormatData(_progress, _cancelTokens.Token); // TODO: Do the templates differently Model.EndActions eaBase; try { var template = File.ReadAllText(Environment.CurrentDirectory + @"\dist\BaseEndActions.json", Encoding.UTF8); eaBase = JsonConvert.DeserializeObject <Model.EndActions>(template); } catch (FileNotFoundException) { Logger.Log(@"Unable to find dist\BaseEndActions.json, make sure it has been extracted!"); return; } catch (Exception e) { Logger.Log($@"An error occurred while loading dist\BaseEndActions.json (make sure any new versions have been extracted!)\r\n{e.Message}\r\n{e.StackTrace}"); return; } await ea.GenerateEndActionsFromBase(eaBase, _progress, _cancelTokens.Token); StartActions sa; try { var template = File.ReadAllText(Environment.CurrentDirectory + @"\dist\BaseStartActions.json", Encoding.UTF8); sa = JsonConvert.DeserializeObject <StartActions>(template); } catch (FileNotFoundException) { Logger.Log(@"Unable to find dist\BaseStartActions.json, make sure it has been extracted!"); return; } catch (Exception e) { Logger.Log($@"An error occurred while loading dist\BaseStartActions.json (make sure any new versions have been extracted!)\r\n{e.Message}\r\n{e.StackTrace}"); return; } // TODO: Separate out SA logic string saContent = null; if (_settings.downloadSA) { Logger.Log("Attempting to download Start Actions..."); try { saContent = await Amazon.DownloadStartActions(metadata.ASIN); } catch { Logger.Log("No pre-made Start Actions available, building..."); } } if (string.IsNullOrEmpty(saContent)) { saContent = ea.GenerateStartActionsFromBase(sa); } ea.WriteStartActions(saContent); cmsPreview.Items[3].Enabled = true; EaPath = $@"{outputDir}\EndActions.data.{bookInfo.asin}.asc"; } else { ea.GenerateOld(); } cmsPreview.Items[1].Enabled = true; checkFiles(bookInfo.author, bookInfo.title, bookInfo.asin); if (_settings.playSound) { System.Media.SoundPlayer player = new System.Media.SoundPlayer(Environment.CurrentDirectory + @"\done.wav"); player.Play(); } } catch (Exception ex) { Logger.Log("An error occurred while creating the new Author Profile, Start Actions, and/or End Actions files:\r\n" + ex.Message + "\r\n" + ex.StackTrace); } finally { metadata.Dispose(); } }
private async Task btnBuild_Run() { //Check current settings if (!File.Exists(txtMobi.Text)) { MessageBox.Show(@"Specified book was not found.", @"Book Not Found"); return; } if (rdoGoodreads.Checked && txtGoodreads.Text == "") { MessageBox.Show("No " + _dataSource.Name + " link was specified.", "Missing " + _dataSource.Name + " Link"); return; } if (!Directory.Exists(_settings.outDir)) { MessageBox.Show(@"Specified output directory does not exist.\r\nPlease review the settings page.", @"Output Directory Not found"); return; } if (_settings.realName.Trim().Length == 0 || _settings.penName.Trim().Length == 0) { MessageBox.Show( @"Both Real and Pen names are required for End Action\r\n" + @"file creation. This information allows you to rate this\r\n" + "book on Amazon. Please review the settings page.", "Amazon Customer Details Not found"); return; } prgBar.Value = 0; var metadata = await Task.Run(() => UIFunctions.GetAndValidateMetadata(txtMobi.Text, _settings.outDir, _settings.saverawml)); if (metadata == null) { return; } // Added author name to log output Logger.Log($"Book's {_dataSource.Name} URL: {txtGoodreads.Text}"); if (_cancelTokens.IsCancellationRequested) { return; } Logger.Log("Attempting to build X-Ray..."); //If AZW3 file use AZW3 offset, if checked. Checked by default. bool AZW3 = Path.GetExtension(txtMobi.Text) == ".azw3" && _settings.overrideOffset; Logger.Log("Offset: " + (AZW3 ? $"{_settings.offsetAZW3} (AZW3)" : _settings.offset.ToString())); //Create X-Ray and attempt to create the base file (essentially the same as the site) XRay xray; SetDatasourceLabels(); // Reset the dataSource for the new build process try { if (rdoGoodreads.Checked) { xray = new XRay(txtGoodreads.Text, metadata.DBName, metadata.UniqueID, metadata.ASIN, _dataSource, AZW3 ? _settings.offsetAZW3 : _settings.offset, "", false); } else { xray = new XRay(txtXMLFile.Text, metadata.DBName, metadata.UniqueID, metadata.ASIN, _dataSource, AZW3 ? _settings.offsetAZW3 : _settings.offset, ""); } await Task.Run(() => xray.CreateXray(_progress, _cancelTokens.Token)).ConfigureAwait(false); xray.ExportAndDisplayTerms(); if (_settings.enableEdit && DialogResult.Yes == MessageBox.Show( "Terms have been exported to an alias file or already exist in that file. Would you like to open the file in notepad for editing?\r\n" + "See the MobileRead forum thread (link in Settings) for more information on building aliases.", "Aliases", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)) { Functions.RunNotepad(xray.AliasPath); } if (!File.Exists(xray.AliasPath)) { Logger.Log("Aliases file not found."); } else { xray.LoadAliases(); Logger.Log($"Character aliases read from {xray.AliasPath}."); } Logger.Log("Initial X-Ray built, adding locations and chapters..."); //Expand the X-Ray file from the unpacked mobi if (await Task.Run(() => xray.ExpandFromRawMl(metadata.GetRawMlStream(), SafeShow, _progress, _cancelTokens.Token, _settings.ignoresofthyphen, !_settings.useNewVersion)).ConfigureAwait(false) > 0) { Logger.Log("Build canceled or error occurred while processing locations and chapters."); return; } } catch (OperationCanceledException) { Logger.Log("Build canceled."); return; } catch (Exception ex) { Logger.Log("An error occurred while building the X-Ray:\r\n" + ex.Message + "\r\n" + ex.StackTrace); return; } Logger.Log("Saving X-Ray to file..."); string outFolder; try { if (_settings.android) { outFolder = _settings.outDir + @"\Android\" + metadata.ASIN; Directory.CreateDirectory(outFolder); } else { outFolder = OutputDirectory(metadata.Author, metadata.Title, true); } } catch (Exception ex) { Logger.Log("Failed to create output directory: " + ex.Message + "\r\n" + ex.StackTrace + "\r\nFiles will be placed in the default output directory."); outFolder = _settings.outDir; } var newPath = outFolder + "\\" + xray.XRayName(_settings.android); if (_settings.useNewVersion) { try { xray.SaveToFileNew(newPath, _progress, _cancelTokens.Token); } catch (OperationCanceledException) { Logger.Log("Building canceled."); return; } catch (Exception ex) { // TODO: Add option to retry maybe? Logger.Log($"An error occurred while creating the new X-Ray database. Is it opened in another program?\r\n{ex.Message}"); return; } XrPath = outFolder + @"\XRAY.entities." + metadata.ASIN; //Save the new XRAY.ASIN.previewData file try { string PdPath = outFolder + @"\XRAY." + metadata.ASIN + ".previewData"; xray.SavePreviewToFile(PdPath); Logger.Log($"X-Ray previewData file created successfully!\r\nSaved to {PdPath}"); } catch (Exception ex) { Logger.Log(String.Format("An error occurred saving the previewData file: {0}\r\n{1}", ex.Message, ex.StackTrace)); } } else { xray.SaveToFileOld(newPath); } Logger.Log($"X-Ray file created successfully!\r\nSaved to {newPath}"); checkFiles(metadata.Author, metadata.Title, metadata.ASIN); if (_settings.playSound) { System.Media.SoundPlayer player = new System.Media.SoundPlayer(Environment.CurrentDirectory + @"\done.wav"); player.Play(); } metadata.Dispose(); }
private void btnBrowseXML_Click(object sender, EventArgs e) { txtXMLFile.Text = UIFunctions.GetFile(txtXMLFile.Text, "XML files (*.xml)|*.xml|TXT files (*.txt)|*.txt"); }