Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
Simple OPDS
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mirror
Simple OPDS
Commits
5398b5bf
Commit
5398b5bf
authored
Feb 27, 2017
by
Dmitry Shelepnev
Browse files
Options
Downloads
Patches
Plain Diff
Fix fb2 cover extracting
parent
a3d85998
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
book_tools/format/fb2.py
+7
-5
7 additions, 5 deletions
book_tools/format/fb2.py
with
7 additions
and
5 deletions
book_tools/format/fb2.py
+
7
−
5
View file @
5398b5bf
...
@@ -20,7 +20,7 @@ class Namespace(object):
...
@@ -20,7 +20,7 @@ class Namespace(object):
class
FB2Base
(
BookFile
):
class
FB2Base
(
BookFile
):
def
__init__
(
self
,
file
,
original_filename
,
mimetype
):
def
__init__
(
self
,
file
,
original_filename
,
mimetype
):
BookFile
.
__init__
(
self
,
file
,
original_filename
,
mimetype
)
BookFile
.
__init__
(
self
,
file
,
original_filename
,
mimetype
)
self
.
__namespaces
=
{
'
xlink
'
:
Namespace
.
XLINK
}
self
.
__namespaces
=
{
'
fb
'
:
Namespace
.
FICTION_BOOK20
,
'
xlink
'
:
Namespace
.
XLINK
}
try
:
try
:
tree
=
self
.
__create_tree__
()
tree
=
self
.
__create_tree__
()
self
.
__detect_namespaces
(
tree
)
self
.
__detect_namespaces
(
tree
)
...
@@ -47,7 +47,7 @@ class FB2Base(BookFile):
...
@@ -47,7 +47,7 @@ class FB2Base(BookFile):
tree
=
self
.
__create_tree__
()
tree
=
self
.
__create_tree__
()
res
=
tree
.
xpath
(
'
/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image
'
,
namespaces
=
self
.
__namespaces
)
res
=
tree
.
xpath
(
'
/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image
'
,
namespaces
=
self
.
__namespaces
)
cover_id
=
res
[
0
].
get
(
'
{
'
+
Namespace
.
XLINK
+
'
}href
'
)[
1
:]
cover_id
=
res
[
0
].
get
(
'
{
'
+
Namespace
.
XLINK
+
'
}href
'
)[
1
:]
res
=
tree
.
xpath
(
'
/fb:binary[@id=
"
%s
"
]
'
%
cover_id
,
namespaces
=
self
.
__namespaces
)
res
=
tree
.
xpath
(
'
/fb:
FictionBook/fb:
binary[@id=
"
%s
"
]
'
%
cover_id
,
namespaces
=
self
.
__namespaces
)
content
=
base64
.
b64decode
(
res
[
0
].
text
)
content
=
base64
.
b64decode
(
res
[
0
].
text
)
with
open
(
os
.
path
.
join
(
working_dir
,
'
cover.jpeg
'
),
'
wb
'
)
as
cover_file
:
with
open
(
os
.
path
.
join
(
working_dir
,
'
cover.jpeg
'
),
'
wb
'
)
as
cover_file
:
cover_file
.
write
(
content
)
cover_file
.
write
(
content
)
...
@@ -60,15 +60,17 @@ class FB2Base(BookFile):
...
@@ -60,15 +60,17 @@ class FB2Base(BookFile):
tree
=
self
.
__create_tree__
()
tree
=
self
.
__create_tree__
()
res
=
tree
.
xpath
(
'
/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image
'
,
namespaces
=
self
.
__namespaces
)
res
=
tree
.
xpath
(
'
/fb:FictionBook/fb:description/fb:title-info/fb:coverpage/fb:image
'
,
namespaces
=
self
.
__namespaces
)
cover_id
=
res
[
0
].
get
(
'
{
'
+
Namespace
.
XLINK
+
'
}href
'
)[
1
:]
cover_id
=
res
[
0
].
get
(
'
{
'
+
Namespace
.
XLINK
+
'
}href
'
)[
1
:]
res
=
tree
.
xpath
(
'
/fb:binary[@id=
"
%s
"
]
'
%
cover_id
,
namespaces
=
self
.
__namespaces
)
print
(
cover_id
)
res
=
tree
.
xpath
(
'
/fb:FictionBook/fb:binary[@id=
"
%s
"
]
'
%
cover_id
,
namespaces
=
self
.
__namespaces
)
content
=
base64
.
b64decode
(
res
[
0
].
text
)
content
=
base64
.
b64decode
(
res
[
0
].
text
)
return
content
return
content
except
Exception
as
err
:
except
Exception
as
err
:
print
(
"
exception Extract %s
"
%
err
)
return
None
return
None
def
__detect_namespaces
(
self
,
tree
):
def
__detect_namespaces
(
self
,
tree
):
tag
=
tree
.
getroot
().
tag
if
tree
.
getroot
().
tag
.
find
(
Namespace
.
FICTION_BOOK21
)
>
0
:
self
.
__namespaces
[
'
fb
'
]
=
Namespace
.
FICTION_BOOK20
if
tag
.
find
(
Namespace
.
FICTION_BOOK20
)
>
0
else
Namespace
.
FICTION_BOOK21
self
.
__namespaces
[
'
fb
'
]
=
Namespace
.
FICTION_BOOK21
return
None
return
None
def
__detect_title
(
self
,
tree
):
def
__detect_title
(
self
,
tree
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment