Răsfoiți Sursa

[added] wishlist command to list all books from the wish list (#7)

TnS-hun 6 ani în urmă
părinte
comite
fabea4097e

+ 5 - 1
README.md

@@ -48,6 +48,10 @@ To download all your books:
 ```
 python kobo-book-downloader get /dir/ --all
 ```
+To list all your books from your wish list:
+```
+python kobo-book-downloader wishlist
+```
 To show the location of the program's configuration file:
 ```
 python kobo-book-downloader info
@@ -56,7 +60,7 @@ Running the program without any arguments will show the help:
 ```
 python kobo-book-downloader
 ```
-To get additional help for the **list** command (it works for **get** too):
+To get additional help for the **list** command (it works for **get** and **pick** too):
 ```
 python kobo-book-downloader list --help
 ```

+ 34 - 3
kobo-book-downloader/Commands.py

@@ -28,6 +28,7 @@ Commands:
   info     Show the location of the configuration file
   list     List your books
   pick     Download books using interactive selection
+  wishlist List your wish listed books
 
 Optional arguments:
   -h, --help    Show this help message and exit
@@ -39,9 +40,10 @@ Examples:
   kobo-book-downloader info                                                      Show the location of the program's configuration file
   kobo-book-downloader list                                                      List your unread books
   kobo-book-downloader list --all                                                List all your books
-  kobo-book-downloader list --help                                               Get additional help for the list command (it works for get too)
-  kobo-book-downloader pick /dir/                                                Interactively select unread books to download 
-  kobo-book-downloader pick /dir/ --all                                          Interactively select books to download"""
+  kobo-book-downloader list --help                                               Get additional help for the list command (it works for get and pick too)
+  kobo-book-downloader pick /dir/                                                Interactively select unread books to download
+  kobo-book-downloader pick /dir/ --all                                          Interactively select books to download
+  kobo-book-downloader wishlist                                                  List your wish listed books"""
 
 		print( usage )
 
@@ -288,6 +290,35 @@ Examples:
 		rowsToDownload = Commands.__GetPickedBookRows( rows )
 		Commands.__DownloadPickedBooks( outputPath, rowsToDownload )
 
+	@staticmethod
+	def ListWishListedBooks() -> None:
+		rows = []
+
+		wishList = Globals.Kobo.GetMyWishList()
+		for wishListEntry in wishList:
+			productMetadata = wishListEntry.get( "ProductMetadata" )
+			if productMetadata is None:
+				continue
+
+			book = productMetadata.get( "Book" )
+			if book is None:
+				continue
+
+			title = colorama.Style.BRIGHT + book[ "Title" ] + colorama.Style.RESET_ALL
+			author = Commands.__GetBookAuthor( book )
+			isbn = book.get( "ISBN", "" )
+
+			row = title
+			if len( author ) > 0:
+				row += " by " + author
+			if len( isbn ) > 0:
+				row += " (ISBN: %s)" % isbn
+
+			rows.append( row )
+
+		rows = sorted( rows, key = lambda row: row.lower() )
+		print( "\n".join( rows ) )
+
 	@staticmethod
 	def Info():
 		print( "The configuration file is located at:\n%s" % Globals.Settings.SettingsFilePath )

+ 26 - 0
kobo-book-downloader/Kobo.py

@@ -238,6 +238,32 @@ class Kobo:
 
 		return fullBookList
 
+	def GetMyWishList( self ) -> list:
+		items = []
+		currentPageIndex = 0
+
+		while True:
+			url = self.InitializationSettings[ "user_wishlist" ]
+			headers = Kobo.GetHeaderWithAccessToken()
+			hooks = Kobo.__GetReauthenticationHook()
+
+			params = {
+				"PageIndex": currentPageIndex,
+				"PageSize": 100, # 100 is the default if PageSize is not specified.
+			}
+
+			response = Globals.Kobo.Session.get( url, params = params, headers = headers, hooks = hooks )
+			response.raise_for_status()
+			wishList = response.json()
+
+			items.extend( wishList[ "Items" ] )
+
+			currentPageIndex += 1
+			if currentPageIndex >= wishList[ "TotalPageCount" ]:
+				break
+
+		return items
+
 	def __GetContentAccessBook( self, productId: str, displayProfile: str ) -> dict:
 		url = self.InitializationSettings[ "content_access_book" ].replace( "{ProductId}", productId )
 		params = { "DisplayProfile": displayProfile }

+ 3 - 0
kobo-book-downloader/__main__.py

@@ -56,6 +56,7 @@ def Main() -> None:
 	pickParser = subparsers.add_parser( "pick", help = "Download books using interactive selection" )
 	pickParser.add_argument( "OutputPath", metavar = "output-path", help = "Output path must be an existing directory" )
 	pickParser.add_argument( "--all", default = False, action = "store_true", help = "List read books too" )
+	wishListParser = subparsers.add_parser( "wishlist", help = "List your wish listed books" )
 	arguments = argumentParser.parse_args()
 
 	if arguments.Command is None:
@@ -72,6 +73,8 @@ def Main() -> None:
 		Commands.ListBooks( arguments.all )
 	elif arguments.Command == "pick":
 		Commands.PickBooks( arguments.OutputPath, arguments.all )
+	elif arguments.Command == "wishlist":
+		Commands.ListWishListedBooks()
 
 
 if __name__ == '__main__':