__main__.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from Commands import Commands
  2. from Globals import Globals
  3. from Kobo import Kobo, KoboException
  4. from Settings import Settings
  5. import argparse
  6. def InitializeGlobals() -> None:
  7. Globals.Kobo = Kobo()
  8. Globals.Settings = Settings()
  9. def InitializeKoboApi() -> None:
  10. if not Globals.Settings.AreAuthenticationSettingsSet():
  11. Globals.Kobo.AuthenticateDevice()
  12. Globals.Kobo.LoadInitializationSettings()
  13. if not Globals.Settings.IsLoggedIn():
  14. email = input( "Waiting for your input. You can use Shift+Insert to paste from the clipboard. Ctrl+C aborts the program.\n\nKobo e-mail: " )
  15. password = input( "Kobo password: " )
  16. print( """
  17. Open https://authorize.kobo.com/signin in a private/incognito window in your browser, wait till the page
  18. loads (do not login!) then open the developer tools (use F12 in Firefox/Chrome), select the console tab,
  19. and paste the following code there and then press Enter there in the browser.
  20. var newCaptchaDiv = document.createElement( "div" );
  21. newCaptchaDiv.id = "new-grecaptcha-container";
  22. document.getElementById( "grecaptcha-container" ).insertAdjacentElement( "afterend", newCaptchaDiv );
  23. grecaptcha.render( newCaptchaDiv.id, {
  24. sitekey: "6LeEbUwUAAAAADJxtlhMsvgnR7SsFpMm4sirr1CJ",
  25. callback: function( response ) { console.log( "Captcha response:" ); console.log( response ); }
  26. } );
  27. A captcha should show up below the Sign-in form. Once you solve the captcha its response will be written
  28. below the pasted code in the browser's console. Copy the response (the line below "Captcha response:")
  29. and paste it here.
  30. """ )
  31. captcha = input( "Captcha response: " ).strip()
  32. print( "" )
  33. Globals.Kobo.Login( email, password, captcha )
  34. def Main() -> None:
  35. argumentParser = argparse.ArgumentParser( add_help = False )
  36. argumentParser.add_argument( "--help", "-h", default = False, action = "store_true" )
  37. subparsers = argumentParser.add_subparsers( dest = "Command", title = "commands", metavar = "command" )
  38. getParser = subparsers.add_parser( "get", help = "Download book" )
  39. getParser.add_argument( "OutputPath", metavar = "output-path", help = "If the output path is a directory then the file will be named automatically." )
  40. getParser.add_argument( "RevisionId", metavar = "book-id", nargs = "?", help = "The identifier of the book" )
  41. getParser.add_argument( "--all", default = False, action = "store_true", help = "Download all my books" )
  42. infoParser = subparsers.add_parser( "info", help = "Show the location of the program's configuration file" )
  43. listParser = subparsers.add_parser( "list", help = "List unread books" )
  44. listParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  45. pickParser = subparsers.add_parser( "pick", help = "Download books using interactive selection" )
  46. pickParser.add_argument( "OutputPath", metavar = "output-path", help = "Output path must be an existing directory" )
  47. pickParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  48. wishListParser = subparsers.add_parser( "wishlist", help = "List your wish listed books" )
  49. arguments = argumentParser.parse_args()
  50. if arguments.Command is None:
  51. Commands.ShowUsage()
  52. return
  53. InitializeGlobals()
  54. if arguments.Command == "info":
  55. Commands.Info()
  56. return
  57. InitializeKoboApi()
  58. if arguments.Command == "get":
  59. Commands.GetBookOrBooks( arguments.RevisionId, arguments.OutputPath, arguments.all )
  60. elif arguments.Command == "list":
  61. Commands.ListBooks( arguments.all )
  62. elif arguments.Command == "pick":
  63. Commands.PickBooks( arguments.OutputPath, arguments.all )
  64. elif arguments.Command == "wishlist":
  65. Commands.ListWishListedBooks()
  66. if __name__ == '__main__':
  67. try:
  68. Main()
  69. except KoboException as e:
  70. print( "ERROR: %s" % e )