__main__.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. from Commands import Commands
  2. from Globals import Globals
  3. from Kobo import Kobo, KoboException
  4. from LogFormatter import LogFormatter
  5. from Settings import Settings
  6. import colorama
  7. import requests
  8. import argparse
  9. import logging
  10. def InitializeGlobals() -> None:
  11. streamHandler = logging.StreamHandler()
  12. streamHandler.setFormatter( LogFormatter() )
  13. Globals.Logger = logging.getLogger()
  14. Globals.Logger.addHandler( streamHandler )
  15. Globals.Kobo = Kobo()
  16. Globals.Settings = Settings()
  17. def InitializeKoboApi() -> None:
  18. if not Globals.Settings.AreAuthenticationSettingsSet():
  19. Globals.Kobo.AuthenticateDevice()
  20. Globals.Kobo.LoadInitializationSettings()
  21. if not Globals.Settings.IsLoggedIn():
  22. Globals.Kobo.Login()
  23. def Main() -> None:
  24. InitializeGlobals()
  25. colorama.init()
  26. argumentParser = argparse.ArgumentParser( add_help = False )
  27. argumentParser.add_argument( "--help", "-h", default = False, action = "store_true" )
  28. argumentParser.add_argument( "--verbose", default = False, action = "store_true", dest = "VerboseLogging" )
  29. subparsers = argumentParser.add_subparsers( dest = "Command", title = "commands", metavar = "command" )
  30. getParser = subparsers.add_parser( "get", help = "Download book" )
  31. getParser.add_argument( "OutputPath", metavar = "output-path", help = "If the output path is a directory then the file will be named automatically." )
  32. getParser.add_argument( "RevisionId", metavar = "book-id", nargs = "?", help = "The identifier of the book" )
  33. getParser.add_argument( "--all", default = False, action = "store_true", help = "Download all my books" )
  34. infoParser = subparsers.add_parser( "info", help = "Show the location of the program's configuration file" )
  35. listParser = subparsers.add_parser( "list", help = "List unread books" )
  36. listParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  37. pickParser = subparsers.add_parser( "pick", help = "Download books using interactive selection" )
  38. pickParser.add_argument( "OutputPath", metavar = "output-path", help = "Output path must be an existing directory" )
  39. pickParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
  40. wishListParser = subparsers.add_parser( "wishlist", help = "List your wish listed books" )
  41. arguments = argumentParser.parse_args()
  42. if arguments.VerboseLogging:
  43. Globals.Logger.setLevel( logging.DEBUG )
  44. if arguments.Command is None:
  45. Commands.ShowUsage()
  46. elif arguments.Command == "info":
  47. Commands.Info()
  48. else:
  49. InitializeKoboApi()
  50. if arguments.Command == "get":
  51. Commands.GetBookOrBooks( arguments.RevisionId, arguments.OutputPath, arguments.all )
  52. elif arguments.Command == "list":
  53. Commands.ListBooks( arguments.all )
  54. elif arguments.Command == "pick":
  55. Commands.PickBooks( arguments.OutputPath, arguments.all )
  56. elif arguments.Command == "wishlist":
  57. Commands.ListWishListedBooks()
  58. if __name__ == '__main__':
  59. try:
  60. Main()
  61. except KoboException as e:
  62. Globals.Logger.error( e )
  63. except requests.exceptions.Timeout as e:
  64. Globals.Logger.error( "The request has timed out." )