__main__.py 3.0 KB

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