2017-03-17 00:36:37 +01:00
try :
from pydrive . auth import GoogleAuth
from pydrive . drive import GoogleDrive
2017-03-19 17:14:16 +01:00
from apiclient import errors
2017-03-17 00:36:37 +01:00
except ImportError :
pass
2017-03-29 21:43:55 +02:00
import os
2017-02-20 19:34:37 +01:00
from ub import config
2017-12-02 11:15:51 +01:00
import cli
2017-02-20 19:34:37 +01:00
from sqlalchemy import *
from sqlalchemy . ext . declarative import declarative_base
from sqlalchemy . orm import *
import web
2017-12-02 11:15:51 +01:00
engine = create_engine ( ' sqlite:/// {0} ' . format ( cli . gdpath ) , echo = False )
2017-02-20 19:34:37 +01:00
Base = declarative_base ( )
# Open session for database connection
Session = sessionmaker ( )
Session . configure ( bind = engine )
2017-03-01 23:38:03 +01:00
session = scoped_session ( Session )
2017-02-20 19:34:37 +01:00
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
class GdriveId ( Base ) :
2017-04-14 20:29:11 +02:00
__tablename__ = ' gdrive_ids '
2017-02-20 19:34:37 +01:00
id = Column ( Integer , primary_key = True )
gdrive_id = Column ( Integer , unique = True )
path = Column ( String )
2017-03-01 23:38:03 +01:00
__table_args__ = ( UniqueConstraint ( ' gdrive_id ' , ' path ' , name = ' _gdrive_path_uc ' ) , )
2017-02-20 19:34:37 +01:00
def __repr__ ( self ) :
return str ( self . path )
2017-04-14 20:29:11 +02:00
2017-03-03 00:55:32 +01:00
class PermissionAdded ( Base ) :
2017-04-14 20:29:11 +02:00
__tablename__ = ' permissions_added '
2017-03-03 00:55:32 +01:00
id = Column ( Integer , primary_key = True )
gdrive_id = Column ( Integer , unique = True )
def __repr__ ( self ) :
return str ( self . gdrive_id )
2017-04-14 20:29:11 +02:00
2017-03-01 23:38:03 +01:00
def migrate ( ) :
2017-03-03 00:55:32 +01:00
if not engine . dialect . has_table ( engine . connect ( ) , " permissions_added " ) :
PermissionAdded . __table__ . create ( bind = engine )
2017-03-01 23:38:03 +01:00
for sql in session . execute ( " select sql from sqlite_master where type= ' table ' " ) :
if ' CREATE TABLE gdrive_ids ' in sql [ 0 ] :
2017-04-14 20:29:11 +02:00
currUniqueConstraint = ' UNIQUE (gdrive_id) '
2017-03-01 23:38:03 +01:00
if currUniqueConstraint in sql [ 0 ] :
sql = sql [ 0 ] . replace ( currUniqueConstraint , ' UNIQUE (gdrive_id, path) ' )
2017-04-14 20:29:11 +02:00
sql = sql . replace ( GdriveId . __tablename__ , GdriveId . __tablename__ + ' 2 ' )
2017-03-01 23:38:03 +01:00
session . execute ( sql )
session . execute ( ' INSERT INTO gdrive_ids2 (id, gdrive_id, path) SELECT id, gdrive_id, path FROM gdrive_ids; ' )
session . commit ( )
session . execute ( ' DROP TABLE %s ' % ' gdrive_ids ' )
session . execute ( ' ALTER TABLE gdrive_ids2 RENAME to gdrive_ids ' )
break
2017-12-02 11:15:51 +01:00
if not os . path . exists ( cli . gdpath ) :
2017-02-20 19:34:37 +01:00
try :
Base . metadata . create_all ( engine )
except Exception :
raise
2017-03-01 23:38:03 +01:00
migrate ( )
2017-04-14 20:29:11 +02:00
2017-09-17 07:15:08 +02:00
def getDrive ( drive = None , gauth = None ) :
2017-02-20 19:34:37 +01:00
if not drive :
2017-09-17 07:15:08 +02:00
if not gauth :
gauth = GoogleAuth ( settings_file = ' settings.yaml ' )
# Try to load saved client credentials
gauth . LoadCredentialsFile ( " gdrive_credentials " )
if gauth . access_token_expired :
# Refresh them if expired
gauth . Refresh ( )
else :
# Initialize the saved creds
gauth . Authorize ( )
# Save the current credentials to a file
return GoogleDrive ( gauth )
2017-02-20 19:34:37 +01:00
if drive . auth . access_token_expired :
drive . auth . Refresh ( )
2017-09-17 07:15:08 +02:00
return drive
def getEbooksFolder ( drive = None ) :
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
ebooksFolder = " title = ' %s ' and ' root ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % config . config_google_drive_folder
2017-02-20 19:34:37 +01:00
fileList = drive . ListFile ( { ' q ' : ebooksFolder } ) . GetList ( )
return fileList [ 0 ]
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def getEbooksFolderId ( drive = None ) :
2017-04-14 20:29:11 +02:00
storedPathName = session . query ( GdriveId ) . filter ( GdriveId . path == ' / ' ) . first ( )
2017-02-20 19:34:37 +01:00
if storedPathName :
return storedPathName . gdrive_id
else :
2017-04-14 20:29:11 +02:00
gDriveId = GdriveId ( )
gDriveId . gdrive_id = getEbooksFolder ( drive ) [ ' id ' ]
gDriveId . path = ' / '
2017-02-20 19:34:37 +01:00
session . merge ( gDriveId )
session . commit ( )
2017-04-03 21:05:28 +02:00
return
2017-02-20 19:34:37 +01:00
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def getFolderInFolder ( parentId , folderName , drive = None ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
folder = " title = ' %s ' and ' %s ' in parents and mimeType = ' application/vnd.google-apps.folder ' and trashed = false " % ( folderName . replace ( " ' " , " \\ ' " ) , parentId )
2017-02-20 19:34:37 +01:00
fileList = drive . ListFile ( { ' q ' : folder } ) . GetList ( )
2017-04-03 21:05:28 +02:00
return fileList [ 0 ]
2017-02-20 19:34:37 +01:00
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def getFile ( pathId , fileName , drive = None ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
metaDataFile = " ' %s ' in parents and trashed = false and title = ' %s ' " % ( pathId , fileName . replace ( " ' " , " \\ ' " ) )
2017-02-20 19:34:37 +01:00
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
return fileList [ 0 ]
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def getFolderId ( path , drive = None ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
currentFolderId = getEbooksFolderId ( drive )
sqlCheckPath = path if path [ - 1 ] == ' / ' else path + ' / '
storedPathName = session . query ( GdriveId ) . filter ( GdriveId . path == sqlCheckPath ) . first ( )
2017-02-20 19:34:37 +01:00
if not storedPathName :
2017-04-14 20:29:11 +02:00
dbChange = False
s = path . split ( ' / ' )
2017-02-20 19:34:37 +01:00
for i , x in enumerate ( s ) :
if len ( x ) > 0 :
2017-04-14 20:29:11 +02:00
currentPath = " / " . join ( s [ : i + 1 ] )
2017-02-20 19:34:37 +01:00
if currentPath [ - 1 ] != ' / ' :
currentPath = currentPath + ' / '
2017-04-14 20:29:11 +02:00
storedPathName = session . query ( GdriveId ) . filter ( GdriveId . path == currentPath ) . first ( )
2017-02-20 19:34:37 +01:00
if storedPathName :
2017-04-14 20:29:11 +02:00
currentFolderId = storedPathName . gdrive_id
2017-02-20 19:34:37 +01:00
else :
2017-04-14 20:29:11 +02:00
currentFolderId = getFolderInFolder ( currentFolderId , x , drive ) [ ' id ' ]
gDriveId = GdriveId ( )
gDriveId . gdrive_id = currentFolderId
gDriveId . path = currentPath
2017-02-20 19:34:37 +01:00
session . merge ( gDriveId )
2017-04-14 20:29:11 +02:00
dbChange = True
2017-02-20 19:34:37 +01:00
if dbChange :
session . commit ( )
else :
2017-04-14 20:29:11 +02:00
currentFolderId = storedPathName . gdrive_id
2017-02-20 19:34:37 +01:00
return currentFolderId
def getFileFromEbooksFolder ( drive , path , fileName ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-02-20 19:34:37 +01:00
if path :
2017-03-29 21:43:55 +02:00
# sqlCheckPath=path if path[-1] =='/' else path + '/'
2017-04-14 20:29:11 +02:00
folderId = getFolderId ( path , drive )
2017-02-20 19:34:37 +01:00
else :
2017-04-14 20:29:11 +02:00
folderId = getEbooksFolderId ( drive )
2017-04-03 21:05:28 +02:00
2017-02-20 19:34:37 +01:00
return getFile ( folderId , fileName , drive )
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def copyDriveFileRemote ( drive , origin_file_id , copy_title ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-02-20 19:34:37 +01:00
copied_file = { ' title ' : copy_title }
try :
file_data = drive . auth . service . files ( ) . copy (
2017-04-14 20:29:11 +02:00
fileId = origin_file_id , body = copied_file ) . execute ( )
2017-02-20 19:34:37 +01:00
return drive . CreateFile ( { ' id ' : file_data [ ' id ' ] } )
except errors . HttpError as error :
print ( ' An error occurred: %s ' % error )
return None
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def downloadFile ( drive , path , filename , output ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
f = getFileFromEbooksFolder ( drive , path , filename )
2017-02-20 19:34:37 +01:00
f . GetContentFile ( output )
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def backupCalibreDbAndOptionalDownload ( drive , f = None ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
metaDataFile = " ' %s ' in parents and title = ' metadata.db ' and trashed = false " % getEbooksFolderId ( )
2017-02-20 19:34:37 +01:00
fileList = drive . ListFile ( { ' q ' : metaDataFile } ) . GetList ( )
2017-04-04 19:05:09 +02:00
2017-04-14 20:29:11 +02:00
databaseFile = fileList [ 0 ]
2017-02-20 19:34:37 +01:00
if f :
databaseFile . GetContentFile ( f )
2017-04-02 08:45:47 +02:00
2017-02-20 19:34:37 +01:00
def copyToDrive ( drive , uploadFile , createRoot , replaceFiles ,
2017-09-17 10:54:23 +02:00
ignoreFiles = None ,
2017-04-02 08:45:47 +02:00
parent = None , prevDir = ' ' ) :
2017-09-17 10:54:23 +02:00
ignoreFiles = ignoreFiles or [ ]
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
isInitial = not bool ( parent )
2017-02-20 19:34:37 +01:00
if not parent :
2017-04-14 20:29:11 +02:00
parent = getEbooksFolder ( drive )
2017-02-20 19:34:37 +01:00
if os . path . isdir ( os . path . join ( prevDir , uploadFile ) ) :
2017-04-14 20:29:11 +02:00
existingFolder = drive . ListFile ( { ' q ' : " title = ' %s ' and ' %s ' in parents and trashed = false " % ( os . path . basename ( uploadFile ) , parent [ ' id ' ] ) } ) . GetList ( )
2017-02-20 19:34:37 +01:00
if len ( existingFolder ) == 0 and ( not isInitial or createRoot ) :
2017-04-14 20:29:11 +02:00
parent = drive . CreateFile ( { ' title ' : os . path . basename ( uploadFile ) , ' parents ' : [ { " kind " : " drive#fileLink " , ' id ' : parent [ ' id ' ] } ] ,
" mimeType " : " application/vnd.google-apps.folder " } )
2017-02-20 19:34:37 +01:00
parent . Upload ( )
else :
if ( not isInitial or createRoot ) and len ( existingFolder ) > 0 :
2017-04-14 20:29:11 +02:00
parent = existingFolder [ 0 ]
for f in os . listdir ( os . path . join ( prevDir , uploadFile ) ) :
2017-02-20 19:34:37 +01:00
if f not in ignoreFiles :
2017-04-14 20:29:11 +02:00
copyToDrive ( drive , f , True , replaceFiles , ignoreFiles , parent , os . path . join ( prevDir , uploadFile ) )
2017-02-20 19:34:37 +01:00
else :
if os . path . basename ( uploadFile ) not in ignoreFiles :
2017-04-14 20:29:11 +02:00
existingFiles = drive . ListFile ( { ' q ' : " title = ' %s ' and ' %s ' in parents and trashed = false " % ( os . path . basename ( uploadFile ) , parent [ ' id ' ] ) } ) . GetList ( )
2017-02-20 19:34:37 +01:00
if len ( existingFiles ) > 0 :
2017-04-14 20:29:11 +02:00
driveFile = existingFiles [ 0 ]
2017-02-22 23:06:59 +01:00
else :
2017-04-14 20:29:11 +02:00
driveFile = drive . CreateFile ( { ' title ' : os . path . basename ( uploadFile ) , ' parents ' : [ { " kind " : " drive#fileLink " , ' id ' : parent [ ' id ' ] } ] , } )
driveFile . SetContentFile ( os . path . join ( prevDir , uploadFile ) )
2017-02-20 19:34:37 +01:00
driveFile . Upload ( )
2017-04-14 20:29:11 +02:00
2017-03-08 01:26:15 +01:00
def uploadFileToEbooksFolder ( drive , destFile , f ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-04-14 20:29:11 +02:00
parent = getEbooksFolder ( drive )
splitDir = destFile . split ( ' / ' )
2017-03-08 01:26:15 +01:00
for i , x in enumerate ( splitDir ) :
if i == len ( splitDir ) - 1 :
2017-04-14 20:29:11 +02:00
existingFiles = drive . ListFile ( { ' q ' : " title = ' %s ' and ' %s ' in parents and trashed = false " % ( x , parent [ ' id ' ] ) } ) . GetList ( )
2017-03-08 01:26:15 +01:00
if len ( existingFiles ) > 0 :
2017-04-14 20:29:11 +02:00
driveFile = existingFiles [ 0 ]
2017-03-08 01:26:15 +01:00
else :
2017-04-14 20:29:11 +02:00
driveFile = drive . CreateFile ( { ' title ' : x , ' parents ' : [ { " kind " : " drive#fileLink " , ' id ' : parent [ ' id ' ] } ] , } )
2017-03-08 01:26:15 +01:00
driveFile . SetContentFile ( f )
driveFile . Upload ( )
else :
2017-04-14 20:29:11 +02:00
existingFolder = drive . ListFile ( { ' q ' : " title = ' %s ' and ' %s ' in parents and trashed = false " % ( x , parent [ ' id ' ] ) } ) . GetList ( )
2017-03-08 01:26:15 +01:00
if len ( existingFolder ) == 0 :
2017-04-14 20:29:11 +02:00
parent = drive . CreateFile ( { ' title ' : x , ' parents ' : [ { " kind " : " drive#fileLink " , ' id ' : parent [ ' id ' ] } ] ,
" mimeType " : " application/vnd.google-apps.folder " } )
2017-03-08 01:26:15 +01:00
parent . Upload ( )
else :
2017-04-14 20:29:11 +02:00
parent = existingFolder [ 0 ]
2017-03-08 01:26:15 +01:00
2017-02-20 19:34:37 +01:00
def watchChange ( drive , channel_id , channel_type , channel_address ,
channel_token = None , expiration = None ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-03-29 21:43:55 +02:00
# Watch for all changes to a user's Drive.
# Args:
# service: Drive API service instance.
# channel_id: Unique string that identifies this channel.
# channel_type: Type of delivery mechanism used for this channel.
# channel_address: Address where notifications are delivered.
# channel_token: An arbitrary string delivered to the target address with
# each notification delivered over this channel. Optional.
# channel_address: Address where notifications are delivered. Optional.
# Returns:
# The created channel if successful
# Raises:
# apiclient.errors.HttpError: if http request to create channel fails.
2017-02-20 19:34:37 +01:00
body = {
2017-04-14 20:29:11 +02:00
' id ' : channel_id ,
' type ' : channel_type ,
' address ' : channel_address
2017-02-20 19:34:37 +01:00
}
if channel_token :
body [ ' token ' ] = channel_token
if expiration :
body [ ' expiration ' ] = expiration
2017-04-14 20:29:11 +02:00
return drive . auth . service . changes ( ) . watch ( body = body ) . execute ( )
2017-02-20 19:34:37 +01:00
def watchFile ( drive , file_id , channel_id , channel_type , channel_address ,
channel_token = None , expiration = None ) :
""" Watch for any changes to a specific file.
Args :
service : Drive API service instance .
file_id : ID of the file to watch .
channel_id : Unique string that identifies this channel .
channel_type : Type of delivery mechanism used for this channel .
channel_address : Address where notifications are delivered .
channel_token : An arbitrary string delivered to the target address with
each notification delivered over this channel . Optional .
channel_address : Address where notifications are delivered . Optional .
Returns :
The created channel if successful
Raises :
apiclient . errors . HttpError : if http request to create channel fails .
"""
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-02-20 19:34:37 +01:00
body = {
' id ' : channel_id ,
' type ' : channel_type ,
' address ' : channel_address
}
if channel_token :
body [ ' token ' ] = channel_token
if expiration :
body [ ' expiration ' ] = expiration
return drive . auth . service . files ( ) . watch ( fileId = file_id , body = body ) . execute ( )
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def stopChannel ( drive , channel_id , resource_id ) :
""" Stop watching to a specific channel.
Args :
service : Drive API service instance .
channel_id : ID of the channel to stop .
resource_id : Resource ID of the channel to stop .
Raises :
apiclient . errors . HttpError : if http request to create channel fails .
"""
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-03-29 21:43:55 +02:00
# service=drive.auth.service
2017-02-20 19:34:37 +01:00
body = {
2017-04-14 20:29:11 +02:00
' id ' : channel_id ,
' resourceId ' : resource_id
2017-02-20 19:34:37 +01:00
}
return drive . auth . service . channels ( ) . stop ( body = body ) . execute ( )
2017-04-14 20:29:11 +02:00
2017-02-20 19:34:37 +01:00
def getChangeById ( drive , change_id ) :
2017-09-17 07:15:08 +02:00
drive = getDrive ( drive )
2017-03-31 18:41:05 +02:00
# Print a single Change resource information.
#
# Args:
# service: Drive API service instance.
# change_id: ID of the Change resource to retrieve.
2017-02-20 19:34:37 +01:00
try :
change = drive . auth . service . changes ( ) . get ( changeId = change_id ) . execute ( )
return change
2017-04-06 16:50:05 +02:00
except ( errors . HttpError , error ) :
2017-04-03 21:05:28 +02:00
web . app . logger . exception ( error )
2017-02-20 19:34:37 +01:00
return None