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
449d31e8
Commit
449d31e8
authored
Sep 07, 2020
by
Ozzieisaacs
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactored update_dir_structure_file
parent
fe825838
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
80 deletions
+66
-80
db.py
cps/db.py
+1
-1
editbooks.py
cps/editbooks.py
+23
-40
helper.py
cps/helper.py
+42
-39
No files found.
cps/db.py
View file @
449d31e8
...
@@ -340,7 +340,7 @@ class Books(Base):
...
@@ -340,7 +340,7 @@ class Books(Base):
self
.
series_index
=
series_index
self
.
series_index
=
series_index
self
.
last_modified
=
last_modified
self
.
last_modified
=
last_modified
self
.
path
=
path
self
.
path
=
path
self
.
has_cover
=
has_cover
self
.
has_cover
=
(
has_cover
!=
None
)
#def as_dict(self):
#def as_dict(self):
# return {c.name: getattr(self, c.name) for c in self.__table__.columns}
# return {c.name: getattr(self, c.name) for c in self.__table__.columns}
...
...
cps/editbooks.py
View file @
449d31e8
...
@@ -598,6 +598,7 @@ def edit_book(book_id):
...
@@ -598,6 +598,7 @@ def edit_book(book_id):
merge_metadata
(
to_save
,
meta
)
merge_metadata
(
to_save
,
meta
)
# Update book
# Update book
edited_books_id
=
None
edited_books_id
=
None
#handle book title
#handle book title
if
book
.
title
!=
to_save
[
"book_title"
]
.
rstrip
()
.
strip
():
if
book
.
title
!=
to_save
[
"book_title"
]
.
rstrip
()
.
strip
():
if
to_save
[
"book_title"
]
==
''
:
if
to_save
[
"book_title"
]
==
''
:
...
@@ -805,42 +806,17 @@ def upload():
...
@@ -805,42 +806,17 @@ def upload():
if
not
db_author
:
if
not
db_author
:
db_author
=
stored_author
db_author
=
stored_author
sort_author
=
stored_author
.
sort
sort_author
=
stored_author
.
sort
sort_authors_list
.
append
(
sort_author
)
# helper.get_sorted_author(sort_author))
sort_authors_list
.
append
(
sort_author
)
sort_authors
=
' & '
.
join
(
sort_authors_list
)
sort_authors
=
' & '
.
join
(
sort_authors_list
)
title_dir
=
helper
.
get_valid_filename
(
title
)
title_dir
=
helper
.
get_valid_filename
(
title
)
author_dir
=
helper
.
get_valid_filename
(
db_author
.
name
)
author_dir
=
helper
.
get_valid_filename
(
db_author
.
name
)
filepath
=
os
.
path
.
join
(
config
.
config_calibre_dir
,
author_dir
,
title_dir
)
saved_filename
=
os
.
path
.
join
(
filepath
,
title_dir
+
meta
.
extension
.
lower
())
# check if file path exists, otherwise create it, copy file to calibre path and delete temp file
if
not
os
.
path
.
exists
(
filepath
):
try
:
os
.
makedirs
(
filepath
)
except
OSError
:
log
.
error
(
"Failed to create path
%
s (Permission denied)"
,
filepath
)
flash
(
_
(
u"Failed to create path
%(path)
s (Permission denied)."
,
path
=
filepath
),
category
=
"error"
)
return
Response
(
json
.
dumps
({
"location"
:
url_for
(
"web.index"
)}),
mimetype
=
'application/json'
)
try
:
copyfile
(
meta
.
file_path
,
saved_filename
)
os
.
unlink
(
meta
.
file_path
)
except
OSError
as
e
:
log
.
error
(
"Failed to move file
%
s:
%
s"
,
saved_filename
,
e
)
flash
(
_
(
u"Failed to Move File
%(file)
s:
%(error)
s"
,
file
=
saved_filename
,
error
=
e
),
category
=
"error"
)
return
Response
(
json
.
dumps
({
"location"
:
url_for
(
"web.index"
)}),
mimetype
=
'application/json'
)
if
meta
.
cover
is
None
:
has_cover
=
0
copyfile
(
os
.
path
.
join
(
constants
.
STATIC_DIR
,
'generic_cover.jpg'
),
os
.
path
.
join
(
filepath
,
"cover.jpg"
))
else
:
has_cover
=
1
# combine path and normalize path from windows systems
# combine path and normalize path from windows systems
path
=
os
.
path
.
join
(
author_dir
,
title_dir
)
.
replace
(
'
\\
'
,
'/'
)
path
=
os
.
path
.
join
(
author_dir
,
title_dir
)
.
replace
(
'
\\
'
,
'/'
)
# Calibre adds books with utc as timezone
# Calibre adds books with utc as timezone
db_book
=
db
.
Books
(
title
,
""
,
sort_authors
,
datetime
.
utcnow
(),
datetime
(
101
,
1
,
1
),
db_book
=
db
.
Books
(
title
,
""
,
sort_authors
,
datetime
.
utcnow
(),
datetime
(
101
,
1
,
1
),
'1'
,
datetime
.
utcnow
(),
path
,
has_
cover
,
db_author
,
[],
""
)
'1'
,
datetime
.
utcnow
(),
path
,
meta
.
cover
,
db_author
,
[],
""
)
modif_date
|=
modify_database_object
(
input_authors
,
db_book
.
authors
,
db
.
Authors
,
calibre_db
.
session
,
modif_date
|=
modify_database_object
(
input_authors
,
db_book
.
authors
,
db
.
Authors
,
calibre_db
.
session
,
'author'
)
'author'
)
...
@@ -858,7 +834,7 @@ def upload():
...
@@ -858,7 +834,7 @@ def upload():
modif_date
|=
edit_book_series
(
meta
.
series
,
db_book
)
modif_date
|=
edit_book_series
(
meta
.
series
,
db_book
)
# Add file to book
# Add file to book
file_size
=
os
.
path
.
getsize
(
saved_filename
)
file_size
=
os
.
path
.
getsize
(
meta
.
file_path
)
db_data
=
db
.
Data
(
db_book
,
meta
.
extension
.
upper
()[
1
:],
file_size
,
title_dir
)
db_data
=
db
.
Data
(
db_book
,
meta
.
extension
.
upper
()[
1
:],
file_size
,
title_dir
)
db_book
.
data
.
append
(
db_data
)
db_book
.
data
.
append
(
db_data
)
calibre_db
.
session
.
add
(
db_book
)
calibre_db
.
session
.
add
(
db_book
)
...
@@ -866,25 +842,32 @@ def upload():
...
@@ -866,25 +842,32 @@ def upload():
# flush content, get db_book.id available
# flush content, get db_book.id available
calibre_db
.
session
.
flush
()
calibre_db
.
session
.
flush
()
# Comments needs book id therfore only possibl
w
after flush
# Comments needs book id therfore only possibl
e
after flush
modif_date
|=
edit_book_comments
(
Markup
(
meta
.
description
)
.
unescape
(),
db_book
)
modif_date
|=
edit_book_comments
(
Markup
(
meta
.
description
)
.
unescape
(),
db_book
)
book_id
=
db_book
.
id
book_id
=
db_book
.
id
title
=
db_book
.
title
title
=
db_book
.
title
error
=
helper
.
update_dir_stucture
(
book_id
,
config
.
config_calibre_dir
,
input_authors
[
0
])
error
=
helper
.
update_dir_stucture
(
book_id
,
config
.
config_calibre_dir
,
input_authors
[
0
],
meta
.
file_path
,
title_dir
+
meta
.
extension
)
# move cover to final directory, including book id
# move cover to final directory, including book id
if
has_cover
:
if
meta
.
cover
:
new_coverpath
=
os
.
path
.
join
(
config
.
config_calibre_dir
,
db_book
.
path
,
"cover.jpg"
)
coverfile
=
meta
.
cover
try
:
else
:
copyfile
(
meta
.
cover
,
new_coverpath
)
coverfile
=
os
.
path
.
join
(
constants
.
STATIC_DIR
,
'generic_cover.jpg'
)
os
.
unlink
(
meta
.
cover
)
new_coverpath
=
os
.
path
.
join
(
config
.
config_calibre_dir
,
db_book
.
path
,
"cover.jpg"
)
except
OSError
as
e
:
try
:
log
.
error
(
"Failed to move cover file
%
s:
%
s"
,
new_coverpath
,
e
)
copyfile
(
coverfile
,
new_coverpath
)
flash
(
_
(
u"Failed to Move Cover File
%(file)
s:
%(error)
s"
,
file
=
new_coverpath
,
os
.
unlink
(
meta
.
cover
)
error
=
e
),
except
OSError
as
e
:
category
=
"error"
)
log
.
error
(
"Failed to move cover file
%
s:
%
s"
,
new_coverpath
,
e
)
flash
(
_
(
u"Failed to Move Cover File
%(file)
s:
%(error)
s"
,
file
=
new_coverpath
,
error
=
e
),
category
=
"error"
)
# save data to database, reread data
# save data to database, reread data
calibre_db
.
session
.
commit
()
calibre_db
.
session
.
commit
()
...
...
cps/helper.py
View file @
449d31e8
...
@@ -351,67 +351,70 @@ def delete_book_file(book, calibrepath, book_format=None):
...
@@ -351,67 +351,70 @@ def delete_book_file(book, calibrepath, book_format=None):
path
=
book
.
path
)
path
=
book
.
path
)
def
update_dir_structure_file
(
book_id
,
calibrepath
,
first_author
):
# Moves files in file storage during author/title rename, or from temp dir to file storage
def
update_dir_structure_file
(
book_id
,
calibrepath
,
first_author
,
orignal_filepath
,
db_filename
):
# get book database entry from id, if original path overwrite source with original_filepath
localbook
=
calibre_db
.
get_book
(
book_id
)
localbook
=
calibre_db
.
get_book
(
book_id
)
path
=
os
.
path
.
join
(
calibrepath
,
localbook
.
path
)
if
orignal_filepath
:
path
=
orignal_filepath
else
:
path
=
os
.
path
.
join
(
calibrepath
,
localbook
.
path
)
# Create (current) authordir and titledir from database
authordir
=
localbook
.
path
.
split
(
'/'
)[
0
]
authordir
=
localbook
.
path
.
split
(
'/'
)[
0
]
titledir
=
localbook
.
path
.
split
(
'/'
)[
1
]
# Create new_authordir from parameter or from database
# Create new titledir from database and add id
if
first_author
:
if
first_author
:
new_authordir
=
get_valid_filename
(
first_author
)
new_authordir
=
get_valid_filename
(
first_author
)
else
:
else
:
new_authordir
=
get_valid_filename
(
localbook
.
authors
[
0
]
.
name
)
new_authordir
=
get_valid_filename
(
localbook
.
authors
[
0
]
.
name
)
titledir
=
localbook
.
path
.
split
(
'/'
)[
1
]
new_titledir
=
get_valid_filename
(
localbook
.
title
)
+
" ("
+
str
(
book_id
)
+
")"
new_titledir
=
get_valid_filename
(
localbook
.
title
)
+
" ("
+
str
(
book_id
)
+
")"
if
titledir
!=
new_titledir
:
if
titledir
!=
new_titledir
or
authordir
!=
new_authordir
or
orignal_filepath
:
new_title_path
=
os
.
path
.
join
(
os
.
path
.
dirname
(
path
),
new_titledir
)
new_path
=
os
.
path
.
join
(
calibrepath
,
new_authordir
,
new_titledir
)
new_name
=
get_valid_filename
(
localbook
.
title
)
+
' - '
+
get_valid_filename
(
new_authordir
)
try
:
try
:
if
not
os
.
path
.
exists
(
new_title_path
):
if
orignal_filepath
:
os
.
renames
(
os
.
path
.
normcase
(
path
),
os
.
path
.
normcase
(
new_title_path
))
os
.
renames
(
os
.
path
.
normcase
(
path
),
else
:
os
.
path
.
normcase
(
os
.
path
.
join
(
new_path
,
db_filename
)))
log
.
info
(
"Copying title:
%
s into existing:
%
s"
,
path
,
new_title_path
)
log
.
debug
(
"Moving title:
%
s to
%
s/
%
s"
,
path
,
new_path
,
new_name
)
# Check new path is not valid path
elif
not
os
.
path
.
exists
(
new_path
):
# move original path to new path
os
.
renames
(
os
.
path
.
normcase
(
path
),
os
.
path
.
normcase
(
new_path
))
log
.
debug
(
"Moving title:
%
s to
%
s"
,
path
,
new_path
)
else
:
# path is valid copy only files to new location (merge)
log
.
info
(
"Moving title:
%
s into existing:
%
s"
,
path
,
new_path
)
# Take all files and subfolder from old path (strange command)
for
dir_name
,
__
,
file_list
in
os
.
walk
(
path
):
for
dir_name
,
__
,
file_list
in
os
.
walk
(
path
):
for
file
in
file_list
:
for
file
in
file_list
:
os
.
renames
(
os
.
path
.
normcase
(
os
.
path
.
join
(
dir_name
,
file
)),
os
.
renames
(
os
.
path
.
normcase
(
os
.
path
.
join
(
dir_name
,
file
)),
os
.
path
.
normcase
(
os
.
path
.
join
(
new_
title_
path
+
dir_name
[
len
(
path
):],
file
)))
os
.
path
.
normcase
(
os
.
path
.
join
(
new_path
+
dir_name
[
len
(
path
):],
file
)))
path
=
new_title_
path
# change location in database to new author/title
path
localbook
.
path
=
localbook
.
path
.
split
(
'/'
)[
0
]
+
'/'
+
new_titledir
localbook
.
path
=
os
.
path
.
join
(
new_authordir
,
new_titledir
)
except
OSError
as
ex
:
except
OSError
as
ex
:
log
.
error
(
"Rename title from:
%
s to
%
s:
%
s"
,
path
,
new_
title_
path
,
ex
)
log
.
error
(
"Rename title from:
%
s to
%
s:
%
s"
,
path
,
new_path
,
ex
)
log
.
debug
(
ex
,
exc_info
=
True
)
log
.
debug
(
ex
,
exc_info
=
True
)
return
_
(
"Rename title from: '
%(src)
s' to '
%(dest)
s' failed with error:
%(error)
s"
,
return
_
(
"Rename title from: '
%(src)
s' to '
%(dest)
s' failed with error:
%(error)
s"
,
src
=
path
,
dest
=
new_title_path
,
error
=
str
(
ex
))
src
=
path
,
dest
=
new_path
,
error
=
str
(
ex
))
if
authordir
!=
new_authordir
:
new_author_path
=
os
.
path
.
join
(
calibrepath
,
new_authordir
,
os
.
path
.
basename
(
path
))
# Rename all files from old names to new names
try
:
os
.
renames
(
os
.
path
.
normcase
(
path
),
os
.
path
.
normcase
(
new_author_path
))
localbook
.
path
=
new_authordir
+
'/'
+
localbook
.
path
.
split
(
'/'
)[
1
]
except
OSError
as
ex
:
log
.
error
(
"Rename author from:
%
s to
%
s:
%
s"
,
path
,
new_author_path
,
ex
)
log
.
debug
(
ex
,
exc_info
=
True
)
return
_
(
"Rename author from: '
%(src)
s' to '
%(dest)
s' failed with error:
%(error)
s"
,
src
=
path
,
dest
=
new_author_path
,
error
=
str
(
ex
))
# Rename all files from old names to new names
if
authordir
!=
new_authordir
or
titledir
!=
new_titledir
:
new_name
=
""
try
:
try
:
new_name
=
get_valid_filename
(
localbook
.
title
)
+
' - '
+
get_valid_filename
(
new_authordir
)
path_name
=
os
.
path
.
join
(
calibrepath
,
new_authordir
,
os
.
path
.
basename
(
path
))
for
file_format
in
localbook
.
data
:
for
file_format
in
localbook
.
data
:
os
.
renames
(
os
.
path
.
normcase
(
os
.
renames
(
os
.
path
.
normcase
(
os
.
path
.
join
(
path_name
,
file_format
.
name
+
'.'
+
file_format
.
format
.
lower
())),
os
.
path
.
join
(
new_path
,
file_format
.
name
+
'.'
+
file_format
.
format
.
lower
())),
os
.
path
.
normcase
(
os
.
path
.
join
(
path_name
,
new_name
+
'.'
+
file_format
.
format
.
lower
())))
os
.
path
.
normcase
(
os
.
path
.
join
(
new_path
,
new_name
+
'.'
+
file_format
.
format
.
lower
())))
file_format
.
name
=
new_name
file_format
.
name
=
new_name
except
OSError
as
ex
:
except
OSError
as
ex
:
log
.
error
(
"Rename file in path
%
s to
%
s:
%
s"
,
path
,
new_name
,
ex
)
log
.
error
(
"Rename file in path
%
s to
%
s:
%
s"
,
new_
path
,
new_name
,
ex
)
log
.
debug
(
ex
,
exc_info
=
True
)
log
.
debug
(
ex
,
exc_info
=
True
)
return
_
(
"Rename file in path '
%(src)
s' to '
%(dest)
s' failed with error:
%(error)
s"
,
return
_
(
"Rename file in path '
%(src)
s' to '
%(dest)
s' failed with error:
%(error)
s"
,
src
=
path
,
dest
=
new_name
,
error
=
str
(
ex
))
src
=
new_
path
,
dest
=
new_name
,
error
=
str
(
ex
))
return
False
return
False
def
update_dir_structure_gdrive
(
book_id
,
first_author
,
original_filepath
):
def
update_dir_structure_gdrive
(
book_id
,
first_author
):
error
=
False
error
=
False
book
=
calibre_db
.
get_book
(
book_id
)
book
=
calibre_db
.
get_book
(
book_id
)
path
=
book
.
path
path
=
book
.
path
...
@@ -513,11 +516,11 @@ def uniq(inpt):
...
@@ -513,11 +516,11 @@ def uniq(inpt):
# ################################# External interface #################################
# ################################# External interface #################################
def
update_dir_stucture
(
book_id
,
calibrepath
,
first_author
=
None
):
def
update_dir_stucture
(
book_id
,
calibrepath
,
first_author
=
None
,
orignal_filepath
=
None
,
db_filename
=
None
):
if
config
.
config_use_google_drive
:
if
config
.
config_use_google_drive
:
return
update_dir_structure_gdrive
(
book_id
,
first_author
)
return
update_dir_structure_gdrive
(
book_id
,
first_author
,
orignal_filepath
,
db_filename
)
else
:
else
:
return
update_dir_structure_file
(
book_id
,
calibrepath
,
first_author
)
return
update_dir_structure_file
(
book_id
,
calibrepath
,
first_author
,
orignal_filepath
,
db_filename
)
def
delete_book
(
book
,
calibrepath
,
book_format
):
def
delete_book
(
book
,
calibrepath
,
book_format
):
...
...
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