From b3b4a5eb4f29bba52c4eded6312f093a9fd63e46 Mon Sep 17 00:00:00 2001 From: wavdl Date: Sat, 13 Sep 2025 21:50:59 -0400 Subject: [PATCH 1/5] Add function for getting "Up Next" books from a user's "To Read" list. --- storygraph_api/parse/user_parser.py | 9 ++++++++- storygraph_api/users_client.py | 5 +++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/storygraph_api/parse/user_parser.py b/storygraph_api/parse/user_parser.py index b4f30c4..a610beb 100644 --- a/storygraph_api/parse/user_parser.py +++ b/storygraph_api/parse/user_parser.py @@ -5,8 +5,10 @@ class UserParser: @staticmethod @parsing_exception - def parse_html(html): + def parse_html(html, id_enclosure=None): soup = BeautifulSoup(html, 'html.parser') + if id_enclosure: + soup = soup.find_all('div', attrs={"id": id_enclosure})[0] books_list = [] books = soup.find_all('div', class_="book-title-author-and-series") for book in books: @@ -29,6 +31,11 @@ def to_read(uname, cookie): content = UserScraper.to_read(uname,cookie) return UserParser.parse_html(content) + @staticmethod + def up_next(uname, cookie): + content = UserScraper.to_read(uname, cookie) + return UserParser.parse_html(content, "up-next-section") + @staticmethod def books_read(uname, cookie): content = UserScraper.books_read(uname,cookie) diff --git a/storygraph_api/users_client.py b/storygraph_api/users_client.py index 0b4c41e..1fe5c50 100644 --- a/storygraph_api/users_client.py +++ b/storygraph_api/users_client.py @@ -13,6 +13,11 @@ def to_read(self,uname,cookie): data = UserParser.to_read(uname,cookie) return json.dumps(data,indent=4) + @handle_exceptions + def up_next(self, uname, cookie): + data = UserParser.up_next(uname,cookie) + return json.dumps(data,indent=4) + @handle_exceptions def books_read(self,uname,cookie): data = UserParser.books_read(uname,cookie) From 7a91142178ce7e4a5684b880440dca6dceddcb47 Mon Sep 17 00:00:00 2001 From: wavdl Date: Sun, 15 Mar 2026 20:24:49 -0400 Subject: [PATCH 2/5] Update UserParser to return authors in the returned books list. --- storygraph_api/parse/user_parser.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/storygraph_api/parse/user_parser.py b/storygraph_api/parse/user_parser.py index a610beb..7b3fca0 100644 --- a/storygraph_api/parse/user_parser.py +++ b/storygraph_api/parse/user_parser.py @@ -12,11 +12,19 @@ def parse_html(html, id_enclosure=None): books_list = [] books = soup.find_all('div', class_="book-title-author-and-series") for book in books: - title = book.find('a').text.strip() - book_id = book.find('a')['href'].split('/')[-1] + a_list = book.find_all('a') + authors = [] + for a in a_list: + print(f"a link: {a['href']}") + if a['href'].startswith("/books/"): + title = a.text.strip() + book_id = a['href'].split('/')[-1] + if a['href'].startswith("/authors/"): + authors.append(a.text.strip()) books_list.append({ 'title': title, - 'book_id': book_id + 'book_id': book_id, + 'author': authors }) data = list({(book['title'], book['book_id']): book for book in books_list}.values()) return data From 48927a8bd1a3c77c700365e2219e89461586e794 Mon Sep 17 00:00:00 2001 From: wavdl Date: Sun, 15 Mar 2026 20:32:57 -0400 Subject: [PATCH 3/5] Update usage examples in the README. --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 75f6adf..1d79da0 100644 --- a/README.md +++ b/README.md @@ -92,11 +92,13 @@ print(result) [ { "title": "The Murder After the Night Before", - "book_id": "38cb5b56-23f1-48fd-b4b3-a80e07a19775" + "book_id": "38cb5b56-23f1-48fd-b4b3-a80e07a19775", + "authors": ["Katy Brent"] }, { "title": "The Graces", - "book_id": "653b54b3-a79d-4c2e-ae40-eae281a91315" + "book_id": "653b54b3-a79d-4c2e-ae40-eae281a91315", + "authors": ["Laure Eve"] } ] From f2e43f73857db35818743a943a6d5d67943dd30c Mon Sep 17 00:00:00 2001 From: wavdl Date: Sun, 15 Mar 2026 20:39:28 -0400 Subject: [PATCH 4/5] Remove debugging print statement. --- storygraph_api/parse/user_parser.py | 1 - 1 file changed, 1 deletion(-) diff --git a/storygraph_api/parse/user_parser.py b/storygraph_api/parse/user_parser.py index 7b3fca0..edc4ce7 100644 --- a/storygraph_api/parse/user_parser.py +++ b/storygraph_api/parse/user_parser.py @@ -15,7 +15,6 @@ def parse_html(html, id_enclosure=None): a_list = book.find_all('a') authors = [] for a in a_list: - print(f"a link: {a['href']}") if a['href'].startswith("/books/"): title = a.text.strip() book_id = a['href'].split('/')[-1] From 9c98a89360f99eaa0b29ca9504bf9119538b560c Mon Sep 17 00:00:00 2001 From: wavdl Date: Sun, 15 Mar 2026 20:43:42 -0400 Subject: [PATCH 5/5] Swap "author" with "authors" since there may be multiple. --- storygraph_api/parse/user_parser.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storygraph_api/parse/user_parser.py b/storygraph_api/parse/user_parser.py index edc4ce7..63b5f21 100644 --- a/storygraph_api/parse/user_parser.py +++ b/storygraph_api/parse/user_parser.py @@ -23,7 +23,7 @@ def parse_html(html, id_enclosure=None): books_list.append({ 'title': title, 'book_id': book_id, - 'author': authors + 'authors': authors }) data = list({(book['title'], book['book_id']): book for book in books_list}.values()) return data