Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH 19 - EPUB Import Support #82

Merged
merged 7 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lute/book/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ class NewBookForm(FlaskForm):
text = TextAreaField("Text", description=desc)
textfile = FileField(
"Text file",
# TODO epub: add epub to the list, change prompt.
# "Please upload a valid .txt or .epub file"
validators=[FileAllowed(["txt"], "Please upload a valid text document")],
validators=[
FileAllowed(
["txt", "epub"],
"Please upload a valid .txt or .epub file.",
)
],
)
source_uri = StringField("Text source", validators=[Length(max=255)])
audiofile = FileField(
Expand Down
8 changes: 5 additions & 3 deletions lute/book/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ def _get_file_content(filefielddata):
return str(content, "utf-8")

if ext == ".epub":
content = service.get_epub_content(filefielddata)
return str(content, "utf-8")
return service.get_epub_content(filefielddata)

raise ValueError(f'Unknown file extension "{ext}"')

Expand All @@ -80,7 +79,10 @@ def new():
if form.validate_on_submit():
form.populate_obj(b)
if form.textfile.data:
b.text = _get_file_content(form.textfile.data)
content = _get_file_content(form.textfile.data)
if not content:
return redirect("/")
b.text = content
f = form.audiofile.data
if f:
b.audio_filename = service.save_audio_file(f)
Expand Down
22 changes: 21 additions & 1 deletion lute/book/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import os
from datetime import datetime
import requests
from tempfile import TemporaryFile, SpooledTemporaryFile
from bs4 import BeautifulSoup
from flask import current_app, flash
from openepub import Epub, EpubError
from werkzeug.utils import secure_filename
from lute.book.model import Book

Expand Down Expand Up @@ -35,7 +37,25 @@ def get_epub_content(epub_file_field_data):
"""
Get the content of the epub as a single string.
"""
raise ValueError("TODO epub: to be implemented.")
content = ""
try:
if hasattr(epub_file_field_data.stream, "seekable"):
epub = Epub(stream=epub_file_field_data.stream)
content = epub.get_text()
else:
# We get a SpooledTemporaryFile from the form but this doesn't
# implement all file-like methods until python 3.11. So we need
# to rewrite it into a TemporaryFile
with TemporaryFile() as tf:
epub_file_field_data.stream.seek(0)
tf.write(epub_file_field_data.stream.read())
epub = Epub(stream=tf)
content = epub.get_text()
except EpubError as e:
msg = f"Could not parse {epub_file_field_data.filename} (error: {str(e)})"
flash(msg, "notice")
content = ""
return content


def book_from_url(url):
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ authors = [
]
readme = "README_PyPi.md"

# TODO epub: add epub parsing library to dependencies
dependencies = [
"Flask-SQLAlchemy>=3.1.1,<4",
"Flask-WTF>=1.2.1,<2",
Expand All @@ -31,7 +30,8 @@ dependencies = [
"beautifulsoup4>=4.12.2,<5",
"PyYAML>=6.0.1,<7",
"toml>=0.10.2,<1",
"waitress>=2.1.2,<3"
"waitress>=2.1.2,<3",
"openepub>=0.0.5<1"
]

[project.scripts]
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,4 @@ wrapt==1.15.0
wsproto==1.2.0
WTForms==3.0.1
zipp==3.17.0
openepub==0.0.5
13 changes: 6 additions & 7 deletions tests/acceptance/book.feature
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,12 @@ Feature: Books and stats are available
And the reading pane shows:
Hola/. /Tengo/ /un/ /perro/.

# TODO epub: add an epub file to sample_files, activate this test.
### Scenario: I can import an epub file.
### Given I visit "/"
### Given a Spanish book "Hola" from file Hola.epub
### Then the page title is Reading "Hola"
### And the reading pane shows:
### Tengo/ /un/ /amigo/.
Scenario: I can import an epub file.
Given I visit "/"
Given a Spanish book "Hola" from file Hola.epub
Then the page title is Reading "Hola"
And the reading pane shows:
Tengo/ /un/ /amigo/.

Scenario: Books and stats are shown on the first page.
Given I visit "/"
Expand Down
Binary file added tests/acceptance/sample_files/Hola.epub
Binary file not shown.