From dd38a2a0bab019e79e2e5771fa56af5dc50e4462 Mon Sep 17 00:00:00 2001 From: JackED42 Date: Tue, 7 Mar 2017 22:56:49 +0000 Subject: [PATCH 1/2] Update readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a7973234..0c73b3a5 100755 --- a/readme.md +++ b/readme.md @@ -4,7 +4,7 @@ I have been messing around with calibre-web in a few different ways and thought 1. Marking books as read/unread. In the Book View, there is now a row that says whether or not a book is read. If you click it, it will toggle. Further down the line, I plan on adding goodreads support, which if the book has a goodreads link, it will automatically move the book to the "to read" and "read" bookshelves. -2. Google drive support. In my local version, I currently have this working, but it is essentially to make the website have all the books / covers / metadata.db served directly from google drive. I am currently, still optimising a bit of code, and will hopefully have this on GitHub ASAP. +2. Google drive support. In my local version, I currently have this working, but it is essentially to make the website have all the books / covers / metadata.db served directly from google drive. I am currently, still optimising a bit of code, and will hopefully have this on GitHub ASAP. Running python cps.py -g, will now make it run with gevent ##Using Google Drive integration From 6ab8d0330aed828be764801763bb00689d915186 Mon Sep 17 00:00:00 2001 From: Jack Darlington Date: Wed, 8 Mar 2017 00:26:15 +0000 Subject: [PATCH 2/2] Fix for editing book cover with drive + fixed found bug that has_cover not set --- cps/gdriveutils.py | 26 ++++++++++++++++++++++++++ cps/web.py | 14 +++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/cps/gdriveutils.py b/cps/gdriveutils.py index 3f29d28a..9ec83059 100644 --- a/cps/gdriveutils.py +++ b/cps/gdriveutils.py @@ -238,6 +238,32 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles, driveFile.SetContentFile(os.path.join(prevDir,uploadFile)) driveFile.Upload() +def uploadFileToEbooksFolder(drive, destFile, f): + if not drive: + drive=getDrive() + if drive.auth.access_token_expired: + drive.auth.Refresh() + parent=getEbooksFolder(drive) + splitDir=destFile.split('/') + for i, x in enumerate(splitDir): + if i == len(splitDir)-1: + existingFiles=drive.ListFile({'q' : "title = '%s' and '%s' in parents and trashed = false" % (x, parent['id'])}).GetList() + if len(existingFiles) > 0: + driveFile=existingFiles[0] + else: + driveFile = drive.CreateFile({'title': x, 'parents' : [{"kind": "drive#fileLink", 'id' : parent['id']}], }) + driveFile.SetContentFile(f) + driveFile.Upload() + else: + existingFolder=drive.ListFile({'q' : "title = '%s' and '%s' in parents and trashed = false" % (x, parent['id'])}).GetList() + if len(existingFolder) == 0: + parent = drive.CreateFile({'title': x, 'parents' : [{"kind": "drive#fileLink", 'id' : parent['id']}], + "mimeType": "application/vnd.google-apps.folder" }) + parent.Upload() + else: + parent=existingFolder[0] + + def watchChange(drive, channel_id, channel_type, channel_address, channel_token=None, expiration=None): if not drive: diff --git a/cps/web.py b/cps/web.py index be317349..d6019c18 100755 --- a/cps/web.py +++ b/cps/web.py @@ -2382,9 +2382,17 @@ def edit_book(book_id): if to_save["cover_url"] and os.path.splitext(to_save["cover_url"])[1].lower() == ".jpg": img = requests.get(to_save["cover_url"]) - f = open(os.path.join(config.config_calibre_dir, book.path, "cover.jpg"), "wb") - f.write(img.content) - f.close() + if config.config_use_google_drive: + tmpDir=tempfile.gettempdir() + f = open(os.path.join(tmpDir, "uploaded_cover.jpg"), "wb") + f.write(img.content) + f.close() + gdriveutils.uploadFileToEbooksFolder(Gdrive.Instance().drive, os.path.join(book.path, 'cover.jpg'), os.path.join(tmpDir, f.name)) + else: + f = open(os.path.join(config.config_calibre_dir, book.path, "cover.jpg"), "wb") + f.write(img.content) + f.close() + book.has_cover=1 if book.series_index != to_save["series_index"]: book.series_index = to_save["series_index"]