static void Main(string[] args) { string mobi_unpack = ""; string python = ""; string shelfariURL = ""; string outDir = ""; string tmpDir = ""; string databaseName = ""; string uniqid = ""; string asin = ""; string aliaspath = ""; int offset = 0; bool saveRaw = false; bool spoilers = false; bool unattended = false; List <string> fileList = new List <string>(); Version dd = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; string xrayversion = dd.Major.ToString() + "." + dd.Minor.ToString() + dd.Build.ToString(); Console.WriteLine("X-Ray Builder Version {0}\n", xrayversion); if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { if (args[i] == "-m" || args[i] == "--mobi2mobi") { Console.WriteLine("Mobi2mobi is no longer required."); i++; } else if (args[i] == "-o" || args[i] == "--outdir") { outDir = args[++i]; } else if (args[i] == "--offset") { int.TryParse(args[++i], out offset); } else if (args[i] == "-p" || args[i] == "--python") { python = args[++i]; } else if (args[i] == "-r" || args[i] == "--saveraw") { saveRaw = true; } else if (args[i] == "-s" || args[i] == "--shelfari") { shelfariURL = args[++i]; } else if (args[i] == "--tmp") { tmpDir = args[++i]; if (!Directory.Exists(tmpDir)) { Console.WriteLine("The temporary directory you specified does not exist, creating it."); Directory.CreateDirectory(tmpDir); } tmpDir = Path.GetFullPath(tmpDir); Console.WriteLine("Using temporary directory: {0}", tmpDir); } else if (args[i] == "--spoilers") { spoilers = true; } else if (args[i] == "-u" || args[i] == "--unpack") { mobi_unpack = args[++i]; if (!File.Exists(mobi_unpack)) { Exit("Specified mobi_unpack.py script not found."); } } else if (args[i] == "-a") { aliaspath = args[++i]; } else if (args[i] == "--unattended") { unattended = true; } else if (File.Exists(args[i])) { fileList.Add(Path.GetFullPath(args[i])); } else if (args[0] == "--usage" || args[0] == "--help" || args[0] == "-?") { ShowUsage(); } else { Console.Error.WriteLine("Unknown arg {0}: {1}. Continuing anyway...", i, args[i]); } } //if (fileList.Count == 0) // Exit("No mobi files specified."); //else if (shelfariURL == "") // Exit("Shelfari URL not found."); if (outDir != "" && !Directory.Exists(outDir)) { Exit("Out directory does not exist. (" + outDir + ")"); } else if (outDir == "") { outDir = Environment.CurrentDirectory + "\\out"; if (!Directory.Exists(outDir)) { Directory.CreateDirectory(outDir); } } } else { ShowUsage(); } Console.WriteLine("Using {0} as an output directory.", outDir); //TODO: Make a function to handle these instead of copy/pasting! if (mobi_unpack != "" && mobi_unpack != XRayBuilder.Properties.Settings.Default.mobi_unpack) { XRayBuilder.Properties.Settings.Default.mobi_unpack = mobi_unpack; XRayBuilder.Properties.Settings.Default.Save(); Console.WriteLine("Saving mobi_unpack directory as default. If not specified in the future, this one will be used."); } else if (mobi_unpack == "" && XRayBuilder.Properties.Settings.Default.mobi_unpack != "") { mobi_unpack = XRayBuilder.Properties.Settings.Default.mobi_unpack; Console.WriteLine("Using saved mobi_unpack path ({0}).", mobi_unpack); } else if (mobi_unpack == "") { mobi_unpack = "dist/mobi_unpack.exe"; } if (!File.Exists(mobi_unpack)) { mobi_unpack = "dist/kindleunpack.exe"; } if (!File.Exists(mobi_unpack)) { Exit("Mobi_unpack not found."); } if (python != "" && python != XRayBuilder.Properties.Settings.Default.python) { XRayBuilder.Properties.Settings.Default.python = python; XRayBuilder.Properties.Settings.Default.Save(); Console.WriteLine("Saving python directory as default. If not specified in the future, this one will be used."); } else if (python == "" && XRayBuilder.Properties.Settings.Default.python != "") { python = XRayBuilder.Properties.Settings.Default.python; Console.WriteLine("Using saved python path ({0}).", python); } else if (python == "") { python = "python"; if (Path.GetExtension(mobi_unpack) == ".py") { Console.WriteLine("Using default Python command. Ensure Python's directory is included in your PATH environment variable."); } } foreach (string mobiFile in fileList) { Console.WriteLine("Processing {0}...", Path.GetFileName(mobiFile)); if (shelfariURL == "") { Console.WriteLine("Enter Shelfari URL for {0} (Enter to skip): ", Path.GetFileNameWithoutExtension(mobiFile)); shelfariURL = Console.ReadLine().Trim(); if (shelfariURL == "") { Console.WriteLine("No Shelfari URL specified! Skipping this book."); continue; } } string rawML = ""; string randomFile = (tmpDir != "" ? tmpDir : GetTempDirectory()); //Sanity check if (!Directory.Exists(randomFile)) { Console.WriteLine("Temporary path not accessible for some reason. Skipping book..."); continue; } if (Path.GetExtension(mobiFile) == ".rawml") { rawML = mobiFile; } else { Console.WriteLine("Running mobi_unpack to get book data..."); //Create a temp folder and use mobi_unpack from command line to unpack mobi file to that folder ProcessStartInfo startInfo = new ProcessStartInfo(); if (Path.GetExtension(mobi_unpack) == ".py") { startInfo.FileName = python; startInfo.Arguments = mobi_unpack + " -r -d \"" + mobiFile + @""" """ + randomFile + @""""; } else { startInfo.FileName = mobi_unpack; startInfo.Arguments = "-r -d \"" + mobiFile + @""" """ + randomFile + @""""; } startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; string unpackInfo = ""; try { using (Process process = Process.Start(startInfo)) { process.BeginErrorReadLine(); using (StreamReader reader1 = process.StandardOutput) { unpackInfo = reader1.ReadToEnd(); //Console.WriteLine(unpackInfo); } } } catch (Exception e) { Console.WriteLine("Error trying to launch mobi_unpack, skipping this book. ({0} | {1})", e.Message, e.Data); continue; } rawML = Path.GetFileNameWithoutExtension(mobiFile) + ".rawml"; //Was the unpack successful? if (!unpackInfo.Contains("Write opf\r\n") && !unpackInfo.Contains("\r\nCompleted")) { Exit("Error unpacking mobi file: " + unpackInfo); } //Console.WriteLine(unpackInfo); Console.WriteLine("Mobi unpacked..."); //Attempt to find the .rawml unpacked from the mobi rawML = randomFile + @"/mobi8/" + rawML; if (!File.Exists(rawML)) { rawML = randomFile + @"/mobi7/" + Path.GetFileNameWithoutExtension(mobiFile) + ".rawml"; } if (!File.Exists(rawML)) { Exit("Error finding rawml file. Path: " + rawML); } Console.WriteLine("RawML found at {0}. Grabbing metadata...", rawML); if (saveRaw) { Console.WriteLine("Saving rawML to output directory."); File.Copy(rawML, Path.Combine(outDir, Path.GetFileName(rawML)), true); } //Match match = Regex.Match(unpackInfo, @"'ASIN': \['([-|\w]*)']"); //changed for kindleunpack Match match = Regex.Match(unpackInfo, @"ASIN\s*(.*)"); if (match.Success && match.Groups.Count > 1) { asin = match.Groups[1].Value.Replace("\r", ""); } //match = Regex.Match(unpackInfo, @"'UniqueID': \['(\d*)']"); //changed for kindleunpack match = Regex.Match(unpackInfo, @"(\d*) unique_id"); if (match.Success && match.Groups.Count > 1) { uniqid = match.Groups[1].Value; } //string test = unpackInfo.Substring(dsf + //Attempt to get database name from the mobi file. //If mobi_unpack ran successfully, then hopefully this will always be valid? byte[] dbinput = new byte[32]; FileStream stream = File.Open(mobiFile, FileMode.Open, FileAccess.Read); if (stream == null) { Console.WriteLine("Error opening mobi file (stream error). Skipping book..."); continue; } int bytesRead = stream.Read(dbinput, 0, 32); if (bytesRead != 32) { Console.WriteLine("Error reading from mobi file. Skipping book..."); continue; } databaseName = Encoding.Default.GetString(dbinput).Trim('\0'); } if (asin == "") { Console.WriteLine("No ASIN found.\nManually enter ASIN for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); asin = Console.ReadLine().Trim(); if (asin == "") { Console.WriteLine("No ASIN specified! Skipping this book."); continue; } } if (databaseName == "") { Console.WriteLine("No database name found.\nManually enter for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); databaseName = Console.ReadLine().Trim(); if (databaseName == "") { Console.WriteLine("No database name specified! Skipping this book."); continue; } } if (uniqid == "") { Console.WriteLine("No unique ID found.\nManually enter for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); uniqid = Console.ReadLine().Trim(); if (uniqid == "") { Console.WriteLine("No unique ID specified! Skipping this book."); continue; } } if (databaseName == "" || uniqid == "" || asin == "") { Console.WriteLine("Error: Missing metadata.\nDatabase Name: {0}\nASIN: {1}\nUniqueID: {2}", databaseName, asin, uniqid); continue; } else if (databaseName.Length == 31) { Console.WriteLine("\nWARNING: Database Name is the maximum length. If \"{0}\" is the full book title, this should not be an issue.\n" + "If the title is supposed to be longer than that, you may get an error WG on your Kindle.\n" + "This can be resolved by either shortening the title in Calibre or manually changing the database name.\n", databaseName); } Console.WriteLine("Got metadata!\nDatabase Name: {0}\nASIN: {1}\nUniqueID: {2}\nAttempting to build X-Ray...", databaseName, asin, uniqid); Console.WriteLine("Spoilers: {0}", spoilers ? "Enabled" : "Disabled"); Console.WriteLine("Location Offset: {0}", offset); //Create X-Ray and attempt to create the base file (essentially the same as the site) XRay ss = new XRay(shelfariURL, databaseName, uniqid, asin, spoilers, offset, aliaspath, unattended); if (ss.createXRAY() > 0) { Console.WriteLine("Error while processing. Skipping to next file."); continue; } Console.WriteLine("\n\nInitial X-Ray built, adding locs and chapters..."); //Expand the X-Ray file from the unpacked mobi if (ss.expandFromRawML(rawML) > 0) { Console.WriteLine("Skipping to next file."); continue; } using (StreamWriter streamWriter = new StreamWriter(outDir + "\\" + ss.getXRayName(), false, Encoding.Default)) { streamWriter.Write(ss.ToString()); } Console.Error.WriteLine("XRay file created successfully!\nSaved to " + outDir + "\\" + ss.getXRayName()); Console.WriteLine("*****************************************************"); Directory.Delete(randomFile, true); shelfariURL = ""; } if (!unattended) { Console.WriteLine("All files processed! Press Enter to exit."); Console.ReadLine(); } else { Console.WriteLine("All files processed!"); } }
static void Main(string[] args) { string mobi_unpack = ""; string python = ""; string shelfariURL = ""; string outDir = ""; string tmpDir = ""; string databaseName = ""; string uniqid = ""; string asin = ""; string aliaspath = ""; int offset = 0; bool saveRaw = false; bool spoilers = false; bool unattended = false; List<string> fileList = new List<string>(); Version dd = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version; string xrayversion = dd.Major.ToString() + "." + dd.Minor.ToString() + dd.Build.ToString(); Console.WriteLine("X-Ray Builder Version {0}\n", xrayversion); if (args.Length > 0) { for (int i = 0; i < args.Length; i++) { if (args[i] == "-m" || args[i] == "--mobi2mobi") { Console.WriteLine("Mobi2mobi is no longer required."); i++; } else if (args[i] == "-o" || args[i] == "--outdir") outDir = args[++i]; else if (args[i] == "--offset") int.TryParse(args[++i], out offset); else if (args[i] == "-p" || args[i] == "--python") python = args[++i]; else if (args[i] == "-r" || args[i] == "--saveraw") saveRaw = true; else if (args[i] == "-s" || args[i] == "--shelfari") shelfariURL = args[++i]; else if (args[i] == "--tmp") { tmpDir = args[++i]; if (!Directory.Exists(tmpDir)) { Console.WriteLine("The temporary directory you specified does not exist, creating it."); Directory.CreateDirectory(tmpDir); } tmpDir = Path.GetFullPath(tmpDir); Console.WriteLine("Using temporary directory: {0}", tmpDir); } else if (args[i] == "--spoilers") spoilers = true; else if (args[i] == "-u" || args[i] == "--unpack") { mobi_unpack = args[++i]; if (!File.Exists(mobi_unpack)) Exit("Specified mobi_unpack.py script not found."); } else if (args[i] == "-a") { aliaspath = args[++i]; } else if (args[i] == "--unattended") { unattended = true; } else if (File.Exists(args[i])) { fileList.Add(Path.GetFullPath(args[i])); } else if (args[0] == "--usage" || args[0] == "--help" || args[0] == "-?") ShowUsage(); else Console.Error.WriteLine("Unknown arg {0}: {1}. Continuing anyway...", i, args[i]); } //if (fileList.Count == 0) // Exit("No mobi files specified."); //else if (shelfariURL == "") // Exit("Shelfari URL not found."); if (outDir != "" && !Directory.Exists(outDir)) Exit("Out directory does not exist. (" + outDir + ")"); else if (outDir == "") { outDir = Environment.CurrentDirectory + "\\out"; if (!Directory.Exists(outDir)) Directory.CreateDirectory(outDir); } } else ShowUsage(); Console.WriteLine("Using {0} as an output directory.", outDir); //TODO: Make a function to handle these instead of copy/pasting! if (mobi_unpack != "" && mobi_unpack != XRayBuilder.Properties.Settings.Default.mobi_unpack) { XRayBuilder.Properties.Settings.Default.mobi_unpack = mobi_unpack; XRayBuilder.Properties.Settings.Default.Save(); Console.WriteLine("Saving mobi_unpack directory as default. If not specified in the future, this one will be used."); } else if (mobi_unpack == "" && XRayBuilder.Properties.Settings.Default.mobi_unpack != "") { mobi_unpack = XRayBuilder.Properties.Settings.Default.mobi_unpack; Console.WriteLine("Using saved mobi_unpack path ({0}).", mobi_unpack); } else if (mobi_unpack == "") mobi_unpack = "dist/mobi_unpack.exe"; if (!File.Exists(mobi_unpack)) mobi_unpack = "dist/kindleunpack.exe"; if(!File.Exists(mobi_unpack)) Exit("Mobi_unpack not found."); if (python != "" && python != XRayBuilder.Properties.Settings.Default.python) { XRayBuilder.Properties.Settings.Default.python = python; XRayBuilder.Properties.Settings.Default.Save(); Console.WriteLine("Saving python directory as default. If not specified in the future, this one will be used."); } else if (python == "" && XRayBuilder.Properties.Settings.Default.python != "") { python = XRayBuilder.Properties.Settings.Default.python; Console.WriteLine("Using saved python path ({0}).", python); } else if (python == "") { python = "python"; if(Path.GetExtension(mobi_unpack) == ".py") Console.WriteLine("Using default Python command. Ensure Python's directory is included in your PATH environment variable."); } foreach (string mobiFile in fileList) { Console.WriteLine("Processing {0}...", Path.GetFileName(mobiFile)); if (shelfariURL == "") { Console.WriteLine("Enter Shelfari URL for {0} (Enter to skip): ", Path.GetFileNameWithoutExtension(mobiFile)); shelfariURL = Console.ReadLine().Trim(); if (shelfariURL == "") { Console.WriteLine("No Shelfari URL specified! Skipping this book."); continue; } } string rawML = ""; string randomFile = (tmpDir != "" ? tmpDir : GetTempDirectory()); //Sanity check if (!Directory.Exists(randomFile)) { Console.WriteLine("Temporary path not accessible for some reason. Skipping book..."); continue; } if (Path.GetExtension(mobiFile) == ".rawml") { rawML = mobiFile; } else { Console.WriteLine("Running mobi_unpack to get book data..."); //Create a temp folder and use mobi_unpack from command line to unpack mobi file to that folder ProcessStartInfo startInfo = new ProcessStartInfo(); if (Path.GetExtension(mobi_unpack) == ".py") { startInfo.FileName = python; startInfo.Arguments = mobi_unpack + " -r -d \"" + mobiFile + @""" """ + randomFile + @""""; } else { startInfo.FileName = mobi_unpack; startInfo.Arguments = "-r -d \"" + mobiFile + @""" """ + randomFile + @""""; } startInfo.RedirectStandardOutput = true; startInfo.RedirectStandardError = true; startInfo.UseShellExecute = false; string unpackInfo = ""; try { using (Process process = Process.Start(startInfo)) { process.BeginErrorReadLine(); using (StreamReader reader1 = process.StandardOutput) { unpackInfo = reader1.ReadToEnd(); //Console.WriteLine(unpackInfo); } } } catch (Exception e) { Console.WriteLine("Error trying to launch mobi_unpack, skipping this book. ({0} | {1})", e.Message, e.Data); continue; } rawML = Path.GetFileNameWithoutExtension(mobiFile) + ".rawml"; //Was the unpack successful? if (!unpackInfo.Contains("Write opf\r\n") && !unpackInfo.Contains("\r\nCompleted")) Exit("Error unpacking mobi file: " + unpackInfo); //Console.WriteLine(unpackInfo); Console.WriteLine("Mobi unpacked..."); //Attempt to find the .rawml unpacked from the mobi rawML = randomFile + @"/mobi8/" + rawML; if (!File.Exists(rawML)) rawML = randomFile + @"/mobi7/" + Path.GetFileNameWithoutExtension(mobiFile) + ".rawml"; if (!File.Exists(rawML)) Exit("Error finding rawml file. Path: " + rawML); Console.WriteLine("RawML found at {0}. Grabbing metadata...", rawML); if (saveRaw) { Console.WriteLine("Saving rawML to output directory."); File.Copy(rawML, Path.Combine(outDir, Path.GetFileName(rawML)), true); } //Match match = Regex.Match(unpackInfo, @"'ASIN': \['([-|\w]*)']"); //changed for kindleunpack Match match = Regex.Match(unpackInfo, @"ASIN\s*(.*)"); if (match.Success && match.Groups.Count > 1) asin = match.Groups[1].Value.Replace("\r", ""); //match = Regex.Match(unpackInfo, @"'UniqueID': \['(\d*)']"); //changed for kindleunpack match = Regex.Match(unpackInfo, @"(\d*) unique_id"); if (match.Success && match.Groups.Count > 1) uniqid = match.Groups[1].Value; //string test = unpackInfo.Substring(dsf + //Attempt to get database name from the mobi file. //If mobi_unpack ran successfully, then hopefully this will always be valid? byte[] dbinput = new byte[32]; FileStream stream = File.Open(mobiFile, FileMode.Open, FileAccess.Read); if (stream == null) { Console.WriteLine("Error opening mobi file (stream error). Skipping book..."); continue; } int bytesRead = stream.Read(dbinput, 0, 32); if (bytesRead != 32) { Console.WriteLine("Error reading from mobi file. Skipping book..."); continue; } databaseName = Encoding.Default.GetString(dbinput).Trim('\0'); } if (asin == "") { Console.WriteLine("No ASIN found.\nManually enter ASIN for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); asin = Console.ReadLine().Trim(); if (asin == "") { Console.WriteLine("No ASIN specified! Skipping this book."); continue; } } if (databaseName == "") { Console.WriteLine("No database name found.\nManually enter for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); databaseName = Console.ReadLine().Trim(); if (databaseName == "") { Console.WriteLine("No database name specified! Skipping this book."); continue; } } if (uniqid == "") { Console.WriteLine("No unique ID found.\nManually enter for {0} (Enter to skip book): ", Path.GetFileNameWithoutExtension(mobiFile)); uniqid = Console.ReadLine().Trim(); if (uniqid == "") { Console.WriteLine("No unique ID specified! Skipping this book."); continue; } } if (databaseName == "" || uniqid == "" || asin == "") { Console.WriteLine("Error: Missing metadata.\nDatabase Name: {0}\nASIN: {1}\nUniqueID: {2}", databaseName, asin, uniqid); continue; } else if (databaseName.Length == 31) { Console.WriteLine("\nWARNING: Database Name is the maximum length. If \"{0}\" is the full book title, this should not be an issue.\n" + "If the title is supposed to be longer than that, you may get an error WG on your Kindle.\n" + "This can be resolved by either shortening the title in Calibre or manually changing the database name.\n", databaseName); } Console.WriteLine("Got metadata!\nDatabase Name: {0}\nASIN: {1}\nUniqueID: {2}\nAttempting to build X-Ray...", databaseName, asin, uniqid); Console.WriteLine("Spoilers: {0}", spoilers ? "Enabled" : "Disabled"); Console.WriteLine("Location Offset: {0}", offset); //Create X-Ray and attempt to create the base file (essentially the same as the site) XRay ss = new XRay(shelfariURL, databaseName, uniqid, asin, spoilers, offset, aliaspath, unattended); if (ss.createXRAY() > 0) { Console.WriteLine("Error while processing. Skipping to next file."); continue; } Console.WriteLine("\n\nInitial X-Ray built, adding locs and chapters..."); //Expand the X-Ray file from the unpacked mobi if (ss.expandFromRawML(rawML) > 0) { Console.WriteLine("Skipping to next file."); continue; } using (StreamWriter streamWriter = new StreamWriter(outDir + "\\" + ss.getXRayName(), false, Encoding.Default)) { streamWriter.Write(ss.ToString()); } Console.Error.WriteLine("XRay file created successfully!\nSaved to " + outDir + "\\" + ss.getXRayName()); Console.WriteLine("*****************************************************"); Directory.Delete(randomFile, true); shelfariURL = ""; } if (!unattended) { Console.WriteLine("All files processed! Press Enter to exit."); Console.ReadLine(); } else { Console.WriteLine("All files processed!"); } }