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
027e103c
Commit
027e103c
authored
Jun 05, 2016
by
Pavel Yakunin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactoring to make adding new formats possible
parent
db70e475
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
101 additions
and
22 deletions
+101
-22
book_formats.py
cps/book_formats.py
+50
-0
uploader.py
cps/uploader.py
+30
-0
web.py
cps/web.py
+21
-22
No files found.
cps/book_formats.py
0 → 100644
View file @
027e103c
__author__
=
'lemmsh'
import
uploader
import
os
try
:
from
wand.image
import
Image
use_generic_pdf_cover
=
False
except
ImportError
,
e
:
use_generic_pdf_cover
=
True
def
process
(
tmp_file_path
,
original_file_name
,
original_file_extension
):
if
(
".PDF"
==
original_file_extension
.
upper
()):
return
pdf_meta
(
tmp_file_path
,
original_file_name
,
original_file_extension
)
else
:
return
None
def
pdf_meta
(
tmp_file_path
,
original_file_name
,
original_file_extension
):
from
PyPDF2
import
PdfFileReader
pdf
=
PdfFileReader
(
open
(
tmp_file_path
,
'rb'
))
doc_info
=
pdf
.
getDocumentInfo
()
print
(
"!!!!!!!!!!!!!!"
)
print
(
doc_info
.
producer
)
if
(
doc_info
is
not
None
):
author
=
doc_info
.
author
title
=
doc_info
.
title
subject
=
doc_info
.
subject
else
:
author
=
"Unknown"
title
=
original_file_name
subject
=
""
return
uploader
.
BookMeta
(
file_path
=
tmp_file_path
,
extension
=
original_file_extension
,
title
=
title
,
author
=
author
,
cover
=
pdf_preview
(
tmp_file_path
,
original_file_name
),
description
=
subject
,
tags
=
""
,
series
=
""
,
series_id
=
""
)
def
pdf_preview
(
tmp_file_path
,
tmp_dir
):
if
use_generic_pdf_cover
:
return
None
else
:
cover_file_name
=
os
.
path
.
splitext
(
tmp_file_path
)[
0
]
+
".cover.jpg"
with
Image
(
filename
=
tmp_file_path
+
"[0]"
,
resolution
=
150
)
as
img
:
img
.
compression_quality
=
88
img
.
save
(
filename
=
os
.
path
.
join
(
tmp_dir
,
cover_file_name
))
return
cover_file_name
cps/uploader.py
0 → 100644
View file @
027e103c
import
os
import
hashlib
from
collections
import
namedtuple
import
book_formats
tmp_dir
=
"/tmp/calibre-web"
BookMeta
=
namedtuple
(
'BookMeta'
,
'file_path, extension, title, author, cover, description, tags, series, series_id'
)
"""
:rtype: BookMeta
"""
def
upload
(
file
):
if
not
os
.
path
.
isdir
(
tmp_dir
):
os
.
mkdir
(
tmp_dir
)
filename
=
file
.
filename
filename_root
,
file_extension
=
os
.
path
.
splitext
(
filename
)
md5
=
hashlib
.
md5
()
md5
.
update
(
filename
)
tmp_file_path
=
os
.
path
.
join
(
tmp_dir
,
md5
.
hexdigest
())
file
.
save
(
tmp_file_path
)
meta
=
book_formats
.
process
(
tmp_file_path
,
filename_root
,
file_extension
)
return
meta
cps/web.py
View file @
027e103c
...
...
@@ -23,6 +23,7 @@ import base64
from
sqlalchemy.sql
import
*
import
json
import
datetime
import
book_formats
from
uuid
import
uuid4
try
:
from
wand.image
import
Image
...
...
@@ -1075,6 +1076,8 @@ def edit_book(book_id):
else
:
return
render_template
(
'edit_book.html'
,
book
=
book
,
authors
=
author_names
,
cc
=
cc
)
import
uploader
@
app
.
route
(
"/upload"
,
methods
=
[
"GET"
,
"POST"
])
@
login_required
@
upload_required
...
...
@@ -1086,20 +1089,17 @@ def upload():
db
.
session
.
connection
()
.
connection
.
connection
.
create_function
(
'uuid4'
,
0
,
lambda
:
str
(
uuid4
()))
if
request
.
method
==
'POST'
and
'btn-upload'
in
request
.
files
:
file
=
request
.
files
[
'btn-upload'
]
filename
=
file
.
filename
filename_root
,
fileextension
=
os
.
path
.
splitext
(
filename
)
if
fileextension
.
upper
()
==
".PDF"
:
title
=
filename_root
author
=
"Unknown"
else
:
flash
(
"Upload is only available for PDF files"
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
meta
=
uploader
.
upload
(
file
)
title
=
meta
.
title
author
=
meta
.
author
title_dir
=
helper
.
get_valid_filename
(
title
,
False
)
author_dir
=
helper
.
get_valid_filename
(
author
.
decode
(
'utf-8'
),
False
)
data_name
=
title_dir
filepath
=
config
.
DB_ROOT
+
"/"
+
author_dir
+
"/"
+
title_dir
saved_filename
=
filepath
+
"/"
+
data_name
+
file
extension
saved_filename
=
filepath
+
"/"
+
data_name
+
meta
.
extension
if
not
os
.
path
.
exists
(
filepath
):
try
:
os
.
makedirs
(
filepath
)
...
...
@@ -1107,21 +1107,20 @@ def upload():
flash
(
"Failed to create path
%
s (Permission denied)."
%
filepath
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
try
:
file
.
save
(
saved_filename
)
copyfile
(
meta
.
file_path
,
saved_filename
)
#remove as well
except
OSError
:
flash
(
"Failed to store file
%
s (Permission denied)."
%
saved_filename
,
category
=
"error"
)
return
redirect
(
url_for
(
'index'
))
file_size
=
os
.
path
.
getsize
(
saved_filename
)
has_cover
=
0
if
fileextension
.
upper
()
==
".PDF"
:
if
use_generic_pdf_cover
:
basedir
=
os
.
path
.
dirname
(
__file__
)
copyfile
(
os
.
path
.
join
(
basedir
,
"static/generic_cover.jpg"
),
os
.
path
.
join
(
filepath
,
"cover.jpg"
))
else
:
with
Image
(
filename
=
saved_filename
+
"[0]"
,
resolution
=
150
)
as
img
:
img
.
compression_quality
=
88
img
.
save
(
filename
=
os
.
path
.
join
(
filepath
,
"cover.jpg"
))
has_cover
=
1
if
meta
.
cover
is
None
:
has_cover
=
0
basedir
=
os
.
path
.
dirname
(
__file__
)
copyfile
(
os
.
path
.
join
(
basedir
,
"static/generic_cover.jpg"
),
os
.
path
.
join
(
filepath
,
"cover.jpg"
))
else
:
has_cover
=
1
copyfile
(
meta
.
cover
,
os
.
path
.
join
(
filepath
,
"cover.jpg"
))
is_author
=
db
.
session
.
query
(
db
.
Authors
)
.
filter
(
db
.
Authors
.
name
==
author
)
.
first
()
if
is_author
:
db_author
=
is_author
...
...
@@ -1131,7 +1130,7 @@ def upload():
path
=
os
.
path
.
join
(
author_dir
,
title_dir
)
db_book
=
db
.
Books
(
title
,
""
,
""
,
datetime
.
datetime
.
now
(),
datetime
.
datetime
(
101
,
01
,
01
),
1
,
datetime
.
datetime
.
now
(),
path
,
has_cover
,
db_author
,
[])
db_book
.
authors
.
append
(
db_author
)
db_data
=
db
.
Data
(
db_book
,
file
extension
.
upper
()[
1
:],
file_size
,
data_name
)
db_data
=
db
.
Data
(
db_book
,
meta
.
extension
.
upper
()[
1
:],
file_size
,
data_name
)
db_book
.
data
.
append
(
db_data
)
db
.
session
.
add
(
db_book
)
...
...
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