import json from pathlib import Path def get_book_list() -> str: """ Get list of available books. Returns: str: JSON string containing the list of book names. """ books_dir = Path("./books") if not books_dir.exists(): return json.dumps("Error: Books directory not found.") text_files = [] for file in books_dir.iterdir(): if file.is_file() and file.suffix.lower() == ".txt": text_files.append(file.stem) return json.dumps(sorted(text_files)) def get_book_content(book_name: str, max_length: int = 0) -> str: """ Get the content of a book. Args: book_name (str): The name of the book (without .txt extension). max_length (int): The maximum length of the content to return. If 0, return the full content. Returns: str: The content of the book, or an error message if the book is not found. """ books_dir = Path("./books") book_file = books_dir / f"{book_name}.txt" if not book_file.exists(): return f"Error: Book '{book_name}' not found." with open(book_file, "r", encoding="utf-8") as f: lines = f.readlines() content = _remove_header_footer(lines) if not max_length: return content else: return content[:max_length] def _remove_header_footer(lines: list[str]) -> str: """ Remove the header and footer from the book content. Args: lines (list[str]): List of lines from the book content. Returns: str: The content without the header and footer. """ # Find start index start_index = 0 for i, line in enumerate(lines): if line.startswith("*** START OF THE PROJECT GUTENBERG EBOOK"): start_index = i + 1 break # Find end index end_index = len(lines) for i in range(len(lines) - 1, -1, -1): if lines[i].startswith("*** END OF THE PROJECT GUTENBERG EBOOK"): end_index = i break return '\n'.join(lines[start_index:end_index]) if __name__ == "__main__": print("Available books:", get_book_list()) book_name = "Winnie_the_Pooh_by_A_A_Milne_9255" book_content = get_book_content(book_name) print(f"Start of '{book_name}':\n{book_content[:500]}...\n") print(f"End of '{book_name}':\n{book_content[-500:]}\n")