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"] } ] diff --git a/storygraph_api/parse/user_parser.py b/storygraph_api/parse/user_parser.py index b4f30c4..63b5f21 100644 --- a/storygraph_api/parse/user_parser.py +++ b/storygraph_api/parse/user_parser.py @@ -5,16 +5,25 @@ 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: - 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: + 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, + 'authors': authors }) data = list({(book['title'], book['book_id']): book for book in books_list}.values()) return data @@ -29,6 +38,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)