__main__.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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():
  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. Globals.Kobo.Login( email, password )
  16. def Main() -> None:
  17. argumentParser = argparse.ArgumentParser( add_help = False )
  18. argumentParser.add_argument( "--help", "-h", default = False, action = "store_true" )
  19. subparsers = argumentParser.add_subparsers( dest = "Command", title = "commands", metavar = "command" )
  20. getParser = subparsers.add_parser( "get", help = "Download book" )
  21. getParser.add_argument( "OutputPath", metavar = "output-path", help = "If the output path is a directory then the file will be named automatically." )
  22. getParser.add_argument( "RevisionId", metavar = "book-id", nargs = "?", help = "The identifier of the book" )
  23. getParser.add_argument( "--all", default = False, action = "store_true", help = "Download all my books" )
  24. infoParser = subparsers.add_parser( "info", help = "Show the location of the program's configuration file" )
  25. listParser = subparsers.add_parser( "list", help = "List unread books" )
  26. listParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  27. arguments = argumentParser.parse_args()
  28. if arguments.Command is None:
  29. Commands.ShowUsage()
  30. return
  31. Initialize()
  32. if arguments.Command == "get":
  33. Commands.GetBookOrBooks( arguments.RevisionId, arguments.OutputPath, arguments.all )
  34. elif arguments.Command == "info":
  35. Commands.Info()
  36. elif arguments.Command == "list":
  37. Commands.ListBooks( arguments.all )
  38. if __name__ == '__main__':
  39. try:
  40. Main()
  41. except KoboException as e:
  42. print( "ERROR: %s" % e )