Created
September 5, 2022 04:38
-
-
Save victormurcia/2f463960edc66654daaf605105bc4d80 to your computer and use it in GitHub Desktop.
download one book from project gutenberg
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # download one book from project gutenberg | |
| def download_book(book_id, save_path): | |
| print(save_path) | |
| # construct the download url | |
| url = f'https://www.gutenberg.org/files/{book_id}/{book_id}-0.txt' | |
| # download the content | |
| data = download_url(url) | |
| if data is None: | |
| #print(f'Failed to download {url}') | |
| url = f'https://www.gutenberg.org/files/{book_id}/{book_id}.txt' | |
| data = download_url(url) | |
| if data is None: | |
| return f'Failed to download {url}' | |
| else: | |
| # create local path | |
| save_file = join(save_path, f'{book_id}.txt') | |
| # save book to file | |
| with open(save_file, 'wb') as file: | |
| file.write(data) | |
| return f'Saved {save_file}' | |
| # create local path | |
| save_file = join(save_path, f'{book_id}.txt') | |
| # save book to file | |
| with open(save_file, 'wb') as file: | |
| file.write(data) | |
| return f'Saved {save_file}' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment