@ -73,7 +73,8 @@ if not os.path.exists(dbpath):
migrate ( )
migrate ( )
def getDrive ( gauth = None ) :
def getDrive ( drive = None , gauth = None ) :
if not drive :
if not gauth :
if not gauth :
gauth = GoogleAuth ( settings_file = ' settings.yaml ' )
gauth = GoogleAuth ( settings_file = ' settings.yaml ' )
# Try to load saved client credentials
# Try to load saved client credentials
@ -86,13 +87,13 @@ def getDrive(gauth=None):
gauth . Authorize ( )
gauth . Authorize ( )
# Save the current credentials to a file
# Save the current credentials to a file
return GoogleDrive ( gauth )
return GoogleDrive ( gauth )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
return drive
def getEbooksFolder ( drive = None ) :
def getEbooksFolder ( drive = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
ebooksFolder = " title = ' %s ' and ' root ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % config . config_google_drive_folder
ebooksFolder = " title = ' %s ' and ' root ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % config . config_google_drive_folder
fileList = drive . ListFile ( { ' q ' : ebooksFolder } ) . GetList ( )
fileList = drive . ListFile ( { ' q ' : ebooksFolder } ) . GetList ( )
@ -113,20 +114,14 @@ def getEbooksFolderId(drive=None):
def getFolderInFolder ( parentId , folderName , drive = None ) :
def getFolderInFolder ( parentId , folderName , drive = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
folder = " title = ' %s ' and ' %s ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % ( folderName . replace ( " ' " , " \\ ' " ) , parentId )
folder = " title = ' %s ' and ' %s ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % ( folderName . replace ( " ' " , " \\ ' " ) , parentId )
fileList = drive . ListFile ( { ' q ' : folder } ) . GetList ( )
fileList = drive . ListFile ( { ' q ' : folder } ) . GetList ( )
return fileList [ 0 ]
return fileList [ 0 ]
def getFile ( pathId , fileName , drive = None ) :
def getFile ( pathId , fileName , drive = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
metaDataFile = " ' %s ' in parents and trashed = false and title = ' %s ' " % ( pathId , fileName . replace ( " ' " , " \\ ' " ) )
metaDataFile = " ' %s ' in parents and trashed = false and title = ' %s ' " % ( pathId , fileName . replace ( " ' " , " \\ ' " ) )
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
@ -134,10 +129,7 @@ def getFile(pathId, fileName, drive=None):
def getFolderId ( path , drive = None ) :
def getFolderId ( path , drive = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
currentFolderId = getEbooksFolderId ( drive )
currentFolderId = getEbooksFolderId ( drive )
sqlCheckPath = path if path [ - 1 ] == ' / ' else path + ' / '
sqlCheckPath = path if path [ - 1 ] == ' / ' else path + ' / '
storedPathName = session . query ( GdriveId ) . filter ( GdriveId . path == sqlCheckPath ) . first ( )
storedPathName = session . query ( GdriveId ) . filter ( GdriveId . path == sqlCheckPath ) . first ( )
@ -168,10 +160,7 @@ def getFolderId(path, drive=None):
def getFileFromEbooksFolder ( drive , path , fileName ) :
def getFileFromEbooksFolder ( drive , path , fileName ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
if path :
if path :
# sqlCheckPath=path if path[-1] =='/' else path + '/'
# sqlCheckPath=path if path[-1] =='/' else path + '/'
folderId = getFolderId ( path , drive )
folderId = getFolderId ( path , drive )
@ -182,10 +171,7 @@ def getFileFromEbooksFolder(drive, path, fileName):
def copyDriveFileRemote ( drive , origin_file_id , copy_title ) :
def copyDriveFileRemote ( drive , origin_file_id , copy_title ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
copied_file = { ' title ' : copy_title }
copied_file = { ' title ' : copy_title }
try :
try :
file_data = drive . auth . service . files ( ) . copy (
file_data = drive . auth . service . files ( ) . copy (
@ -197,19 +183,13 @@ def copyDriveFileRemote(drive, origin_file_id, copy_title):
def downloadFile ( drive , path , filename , output ) :
def downloadFile ( drive , path , filename , output ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
f = getFileFromEbooksFolder ( drive , path , filename )
f = getFileFromEbooksFolder ( drive , path , filename )
f . GetContentFile ( output )
f . GetContentFile ( output )
def backupCalibreDbAndOptionalDownload ( drive , f = None ) :
def backupCalibreDbAndOptionalDownload ( drive , f = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
metaDataFile = " ' %s ' in parents and title = ' metadata.db ' and trashed = false " % getEbooksFolderId ( )
metaDataFile = " ' %s ' in parents and title = ' metadata.db ' and trashed = false " % getEbooksFolderId ( )
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
@ -221,12 +201,9 @@ def backupCalibreDbAndOptionalDownload(drive, f=None):
def copyToDrive ( drive , uploadFile , createRoot , replaceFiles ,
def copyToDrive ( drive , uploadFile , createRoot , replaceFiles ,
ignoreFiles = [ ] ,
ignoreFiles = ignoreFiles or [ ] ,
parent = None , prevDir = ' ' ) :
parent = None , prevDir = ' ' ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
isInitial = not bool ( parent )
isInitial = not bool ( parent )
if not parent :
if not parent :
parent = getEbooksFolder ( drive )
parent = getEbooksFolder ( drive )
@ -254,10 +231,7 @@ def copyToDrive(drive, uploadFile, createRoot, replaceFiles,
def uploadFileToEbooksFolder ( drive , destFile , f ) :
def uploadFileToEbooksFolder ( drive , destFile , f ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
parent = getEbooksFolder ( drive )
parent = getEbooksFolder ( drive )
splitDir = destFile . split ( ' / ' )
splitDir = destFile . split ( ' / ' )
for i , x in enumerate ( splitDir ) :
for i , x in enumerate ( splitDir ) :
@ -281,10 +255,7 @@ def uploadFileToEbooksFolder(drive, destFile, f):
def watchChange ( drive , channel_id , channel_type , channel_address ,
def watchChange ( drive , channel_id , channel_type , channel_address ,
channel_token = None , expiration = None ) :
channel_token = None , expiration = None ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
# Watch for all changes to a user's Drive.
# Watch for all changes to a user's Drive.
# Args:
# Args:
# service: Drive API service instance.
# service: Drive API service instance.
@ -327,10 +298,7 @@ def watchFile(drive, file_id, channel_id, channel_type, channel_address,
Raises :
Raises :
apiclient . errors . HttpError : if http request to create channel fails .
apiclient . errors . HttpError : if http request to create channel fails .
"""
"""
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
body = {
body = {
' id ' : channel_id ,
' id ' : channel_id ,
@ -353,10 +321,7 @@ def stopChannel(drive, channel_id, resource_id):
Raises :
Raises :
apiclient . errors . HttpError : if http request to create channel fails .
apiclient . errors . HttpError : if http request to create channel fails .
"""
"""
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
# service=drive.auth.service
# service=drive.auth.service
body = {
body = {
' id ' : channel_id ,
' id ' : channel_id ,
@ -366,10 +331,7 @@ def stopChannel(drive, channel_id, resource_id):
def getChangeById ( drive , change_id ) :
def getChangeById ( drive , change_id ) :
if not drive :
drive = getDrive ( drive )
drive = getDrive ( )
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
# Print a single Change resource information.
# Print a single Change resource information.
#
#
# Args:
# Args: