diff --git a/README.md b/README.md index 3bfb974..c1bc68d 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ https://user-images.githubusercontent.com/66560242/210459589-751c82d7-e874-4119- - The program may take some time to run, depending on the specified parameters and the performance of the GPT-3 API. Please be patient while the book is being generated. - The program may not always generate the wished amount of words for each chapter. This can happen, if there is not enough data available for the specified topic. - Currently, it is only possible to generate Non-Fiction books. -- Since this is a really early version (v0.6.0), there are many missing features, that will be added by time +- Since this is a really early version (v0.6.2), there are many missing features, that will be added by time ## License diff --git a/requirements.txt b/requirements.txt index 78ac98d..e254bd4 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ openai~=0.25.0 -pyfiglet~=0.8.post1 \ No newline at end of file +pyfiglet~=0.8.post1 +markdown~=3.4.1 +pdfkit~=1.0.0 \ No newline at end of file diff --git a/src/book.py b/src/book.py index ef7adb8..021c6d1 100644 --- a/src/book.py +++ b/src/book.py @@ -1,5 +1,6 @@ import openai -import os +import markdown +import pdfkit from categories import * @@ -38,6 +39,8 @@ def __init__(self, chapter_amount: int, words_per_chapter: int, topic: str, cate # Define category self.category = category + print('Initializing..') + # Get book using `get_category` method self.book = self.get_category() @@ -50,15 +53,6 @@ def __init__(self, chapter_amount: int, words_per_chapter: int, topic: str, cate # Set the structure of the book self.structure = self.get_structure() - def __str__(self): - """ - This method returns the book as a string. - :return: The book as a string. - """ - - # Return the combined chapters - return self.combine() - @staticmethod def __get_edit(input_text, instruction, temperature: float = 0): """ @@ -229,15 +223,41 @@ def get_content(self): # Return the content return content - def combine(self): + def generate(self): """ - This method combines the title, chapter titles, and structure of the book. - :return: The combined book. + This method generates the book. """ - # Get the content of the book + # Get content content = self.get_content() + # Pack book + book = {'title': self.title, 'chapters': content, 'chapter_titles': self.chapter_titles, 'structure': self.structure} + + # Return the book + self.packed = book + + def get_md(self): + """ + This method returns the book in Markdown format. + """ + + # Check if variable packed is exists + if self.packed is None: + print('Book not generated yet. Generating book...') + self.generate() + + # Get the book + book = self.packed + + # Define the variables + title = book['title'] + content = book['chapters'] + chapter_titles = book['chapter_titles'] + structure = book['structure'] + + # Build the book + # Add the title of the book book = '# ' + self.title + '\n\n\n' @@ -263,33 +283,24 @@ def combine(self): # Return the combined book. return book - def save_md(self): + def get_html(self): """ - This method saves the book as a text file. + This method returns the book in HTML format. """ - # Check if the directory to save the book in exists - if not os.path.exists('books'): - - # If it does not exist, create the directory - os.mkdir('books') + # Getting Markdown element + md = markdown.markdown(self.get_md()) - # Define the variable for the file name as the title of the book with all spaces replaced by underscores - file_name = (self.title + '.txt').replace(' ', '_').replace('"', '').replace('?', '') + # Return the HTML + return md - # Checking the file name has a colon - if ':' in file_name: - # If it does, split the file name at the colon and set the file name as the first part - file_name = file_name.split(':')[0] - - # Define the file path as the file name plus the directory to save the book in - path = 'books/' + file_name + '.md' - - # Open the file - with open(path, 'w') as f: + def get_pdf(self): + """ + This method returns the book in PDF format. + """ - # Write the book to the file - f.write(str(self)) + # Get the HTML + html = self.get_html() - # Print the file path - print(f'Book saved as {path}.') + # Return the PDF + return pdfkit.from_string(html, False) diff --git a/src/run.py b/src/run.py index 446e565..8a1cb45 100644 --- a/src/run.py +++ b/src/run.py @@ -4,7 +4,6 @@ from utils import * import json import openai -import os # Get the OpenAI API key from the config file