Skip to content

Instantly share code, notes, and snippets.

@victormurcia
Created September 5, 2022 04:38
Show Gist options
  • Select an option

  • Save victormurcia/2f463960edc66654daaf605105bc4d80 to your computer and use it in GitHub Desktop.

Select an option

Save victormurcia/2f463960edc66654daaf605105bc4d80 to your computer and use it in GitHub Desktop.
download one book from project gutenberg
# 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