Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
D
douban-api-proxy
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
captainwong
douban-api-proxy
Commits
82fe282e
Commit
82fe282e
authored
Aug 04, 2018
by
Ozzie Isaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Converting books from gdrive implemented
parent
ff1b4791
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
43 additions
and
18 deletions
+43
-18
converter.py
cps/converter.py
+7
-2
helper.py
cps/helper.py
+36
-16
No files found.
cps/converter.py
View file @
82fe282e
...
@@ -86,6 +86,8 @@ def convert_kindlegen(file_path, book):
...
@@ -86,6 +86,8 @@ def convert_kindlegen(file_path, book):
uncompressed_size
=
os
.
path
.
getsize
(
file_path
+
".mobi"
)
uncompressed_size
=
os
.
path
.
getsize
(
file_path
+
".mobi"
)
))
))
db
.
session
.
commit
()
db
.
session
.
commit
()
if
ub
.
config
.
config_use_google_drive
:
os
.
remove
(
file_path
+
u".epub"
)
return
file_path
+
".mobi"
,
RET_SUCCESS
return
file_path
+
".mobi"
,
RET_SUCCESS
else
:
else
:
web
.
app
.
logger
.
info
(
"convert_kindlegen: kindlegen failed with error while converting book"
)
web
.
app
.
logger
.
info
(
"convert_kindlegen: kindlegen failed with error while converting book"
)
...
@@ -101,8 +103,8 @@ def convert_calibre(file_path, book):
...
@@ -101,8 +103,8 @@ def convert_calibre(file_path, book):
web
.
app
.
logger
.
error
(
"convert_calibre: "
+
error_message
)
web
.
app
.
logger
.
error
(
"convert_calibre: "
+
error_message
)
return
error_message
,
RET_FAIL
return
error_message
,
RET_FAIL
try
:
try
:
command
=
(
"
\"
"
+
ub
.
config
.
config_converterpath
+
"
\"
"
+
ub
.
config
.
config_calibre
+
command
=
(
"
\"
"
+
ub
.
config
.
config_converterpath
+
"
\"
\"
"
+
file_path
+
u".epub
\"
\"
"
"
\"
"
+
file_path
+
u".epub
\"
\"
"
+
file_path
+
u".mobi
\"
"
)
.
encode
(
sys
.
getfilesystemencoding
())
+
file_path
+
u".mobi
\"
"
+
ub
.
config
.
config_calibre
)
.
encode
(
sys
.
getfilesystemencoding
())
p
=
subprocess
.
Popen
(
command
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
shell
=
True
)
p
=
subprocess
.
Popen
(
command
,
stdout
=
subprocess
.
PIPE
,
stderr
=
subprocess
.
PIPE
,
shell
=
True
)
except
Exception
as
e
:
except
Exception
as
e
:
error_message
=
_
(
u"Ebook-convert failed, no execution permissions"
)
error_message
=
_
(
u"Ebook-convert failed, no execution permissions"
)
...
@@ -124,6 +126,9 @@ def convert_calibre(file_path, book):
...
@@ -124,6 +126,9 @@ def convert_calibre(file_path, book):
uncompressed_size
=
os
.
path
.
getsize
(
file_path
+
".mobi"
)
uncompressed_size
=
os
.
path
.
getsize
(
file_path
+
".mobi"
)
))
))
db
.
session
.
commit
()
db
.
session
.
commit
()
if
ub
.
config
.
config_use_google_drive
:
os
.
remove
(
file_path
+
u".epub"
)
return
file_path
+
".mobi"
,
RET_SUCCESS
return
file_path
+
".mobi"
,
RET_SUCCESS
else
:
else
:
web
.
app
.
logger
.
info
(
"convert_calibre: Ebook-convert failed with error while converting book"
)
web
.
app
.
logger
.
info
(
"convert_calibre: Ebook-convert failed with error while converting book"
)
...
...
cps/helper.py
View file @
82fe282e
...
@@ -74,10 +74,25 @@ def make_mobi(book_id, calibrepath):
...
@@ -74,10 +74,25 @@ def make_mobi(book_id, calibrepath):
error_message
=
_
(
u"epub format not found for book id:
%(book)
d"
,
book
=
book_id
)
error_message
=
_
(
u"epub format not found for book id:
%(book)
d"
,
book
=
book_id
)
app
.
logger
.
error
(
"make_mobi: "
+
error_message
)
app
.
logger
.
error
(
"make_mobi: "
+
error_message
)
return
error_message
,
RET_FAIL
return
error_message
,
RET_FAIL
if
ub
.
config
.
config_use_google_drive
:
df
=
gd
.
getFileFromEbooksFolder
(
book
.
path
,
data
.
name
+
u".epub"
)
if
df
:
datafile
=
os
.
path
.
join
(
calibrepath
,
book
.
path
,
data
.
name
+
u".epub"
)
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
calibrepath
,
book
.
path
)):
os
.
makedirs
(
os
.
path
.
join
(
calibrepath
,
book
.
path
))
df
.
GetContentFile
(
datafile
)
else
:
error_message
=
"make_mobi: epub not found on gdrive:
%
s.epub"
%
data
.
name
return
error_message
,
RET_FAIL
# else:
file_path
=
os
.
path
.
join
(
calibrepath
,
book
.
path
,
data
.
name
)
file_path
=
os
.
path
.
join
(
calibrepath
,
book
.
path
,
data
.
name
)
if
os
.
path
.
exists
(
file_path
+
u".epub"
):
if
os
.
path
.
exists
(
file_path
+
u".epub"
):
return
converter
.
convert_mobi
(
file_path
,
book
)
# convert book, and upload in case of google drive
res
=
converter
.
convert_mobi
(
file_path
,
book
)
if
ub
.
config
.
config_use_google_drive
:
gd
.
updateGdriveCalibreFromLocal
()
# time.sleep(10)
return
res
else
:
else
:
error_message
=
"make_mobi: epub not found:
%
s.epub"
%
file_path
error_message
=
"make_mobi: epub not found:
%
s.epub"
%
file_path
return
error_message
,
RET_FAIL
return
error_message
,
RET_FAIL
...
@@ -104,17 +119,18 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
...
@@ -104,17 +119,18 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
msg
.
attach
(
MIMEText
(
text
.
encode
(
'UTF-8'
),
'plain'
,
'UTF-8'
))
msg
.
attach
(
MIMEText
(
text
.
encode
(
'UTF-8'
),
'plain'
,
'UTF-8'
))
book
=
db
.
session
.
query
(
db
.
Books
)
.
filter
(
db
.
Books
.
id
==
book_id
)
.
first
()
book
=
db
.
session
.
query
(
db
.
Books
)
.
filter
(
db
.
Books
.
id
==
book_id
)
.
first
()
data
=
db
.
session
.
query
(
db
.
Data
)
.
filter
(
db
.
Data
.
book
==
book
.
id
)
data
=
db
.
session
.
query
(
db
.
Data
)
.
filter
(
db
.
Data
.
book
==
book
.
id
)
.
all
()
formats
=
{}
formats
=
{}
index
=
0
for
entry
in
data
:
for
indx
,
entry
in
enumerate
(
data
)
:
if
entry
.
format
==
"MOBI"
:
if
entry
.
format
==
"MOBI"
:
formats
[
"mobi"
]
=
entry
.
name
+
".mobi"
# os.path.join(calibrepath, book.path, entry.name + ".mobi")
formats
[
"mobi"
]
=
entry
.
name
+
".mobi"
if
entry
.
format
==
"EPUB"
:
if
entry
.
format
==
"EPUB"
:
formats
[
"epub"
]
=
entry
.
name
+
".epub"
# os.path.join(calibrepath, book.path, entry.name + ".epub")
formats
[
"epub"
]
=
entry
.
name
+
".epub"
index
=
indx
if
entry
.
format
==
"PDF"
:
if
entry
.
format
==
"PDF"
:
formats
[
"pdf"
]
=
entry
.
name
+
".pdf"
# os.path.join(calibrepath, book.path, entry.name + ".pdf")
formats
[
"pdf"
]
=
entry
.
name
+
".pdf"
if
len
(
formats
)
==
0
:
if
len
(
formats
)
==
0
:
return
_
(
"Could not find any formats suitable for sending by email"
)
return
_
(
"Could not find any formats suitable for sending by email"
)
...
@@ -124,14 +140,15 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
...
@@ -124,14 +140,15 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
if
result
:
if
result
:
msg
.
attach
(
result
)
msg
.
attach
(
result
)
elif
'epub'
in
formats
:
elif
'epub'
in
formats
:
# returns filename if sucess, otherwise errormessage
data
,
resultCode
=
make_mobi
(
book
.
id
,
calibrepath
)
data
,
resultCode
=
make_mobi
(
book
.
id
,
calibrepath
)
if
resultCode
==
RET_SUCCESS
:
if
resultCode
==
RET_SUCCESS
:
result
=
get_attachment
(
calibrepath
,
book
.
path
,
data
)
# toDo check data
result
=
get_attachment
(
calibrepath
,
book
.
path
,
os
.
path
.
basename
(
data
))
if
result
:
if
result
:
msg
.
attach
(
result
)
msg
.
attach
(
result
)
else
:
else
:
app
.
logger
.
error
=
data
app
.
logger
.
error
(
data
)
return
data
# _("Could not convert epub to mobi")
return
data
elif
'pdf'
in
formats
:
elif
'pdf'
in
formats
:
result
=
get_attachment
(
calibrepath
,
book
.
path
,
formats
[
'pdf'
])
result
=
get_attachment
(
calibrepath
,
book
.
path
,
formats
[
'pdf'
])
if
result
:
if
result
:
...
@@ -144,22 +161,25 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
...
@@ -144,22 +161,25 @@ def send_mail(book_id, kindle_mail, calibrepath, user_id):
else
:
else
:
return
_
(
'The requested file could not be read. Maybe wrong permissions?'
)
return
_
(
'The requested file could not be read. Maybe wrong permissions?'
)
# For gdrive download book from gdrive to calibredir (temp dir for books), read contents in both cases and append
# it in MIME Base64 encoded to
def
get_attachment
(
calibrepath
,
bookpath
,
filename
):
def
get_attachment
(
calibrepath
,
bookpath
,
filename
):
"""Get file as MIMEBase message"""
"""Get file as MIMEBase message"""
if
ub
.
config
.
config_use_google_drive
:
if
ub
.
config
.
config_use_google_drive
:
df
=
gd
.
getFileFromEbooksFolder
(
bookpath
,
filename
)
df
=
gd
.
getFileFromEbooksFolder
(
bookpath
,
filename
)
if
df
:
if
df
:
# tmpDir = gettempdir()
datafile
=
os
.
path
.
join
(
calibrepath
,
bookpath
,
filename
)
datafile
=
os
.
path
.
join
(
calibrepath
,
bookpath
,
filename
)
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
calibrepath
,
bookpath
)):
if
not
os
.
path
.
exists
(
os
.
path
.
join
(
calibrepath
,
bookpath
)):
os
.
makedirs
(
os
.
path
.
join
(
calibrepath
,
bookpath
))
os
.
makedirs
(
os
.
path
.
join
(
calibrepath
,
bookpath
))
df
.
GetContentFile
(
datafile
)
df
.
GetContentFile
(
datafile
)
file_
=
open
(
datafile
,
'rb'
)
data
=
file_
.
read
()
file_
.
close
()
os
.
remove
(
datafile
)
else
:
else
:
return
None
return
None
file_
=
open
(
datafile
,
'rb'
)
data
=
file_
.
read
()
file_
.
close
()
os
.
remove
(
datafile
)
else
:
else
:
try
:
try
:
file_
=
open
(
os
.
path
.
join
(
calibrepath
,
bookpath
,
filename
),
'rb'
)
file_
=
open
(
os
.
path
.
join
(
calibrepath
,
bookpath
,
filename
),
'rb'
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment