__main__.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. pickParser = subparsers.add_parser( "pick", help = "Download books using interactive selection" )
  45. pickParser.add_argument( "OutputPath", metavar = "output-path", help = "Output path must be an existing directory" )
  46. pickParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  47. arguments = argumentParser.parse_args()
  48. if arguments.Command is None:
  49. Commands.ShowUsage()
  50. return
  51. Initialize()
  52. if arguments.Command == "get":
  53. Commands.GetBookOrBooks( arguments.RevisionId, arguments.OutputPath, arguments.all )
  54. elif arguments.Command == "info":
  55. Commands.Info()
  56. elif arguments.Command == "list":
  57. Commands.ListBooks( arguments.all )
  58. elif arguments.Command == "pick":
  59. Commands.PickBooks( arguments.OutputPath, arguments.all )
  60. if __name__ == '__main__':
  61. try:
  62. Main()
  63. except KoboException as e:
  64. print( "ERROR: %s" % e )