def get_file(user_auth: dict, user_email, file_dict): # do auth and create service gdrive_auth = user_auth #get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # get the file itself request = service.files().export_media( fileId=file_dict['id'], mimeType='application/pdf') #.get_media(fileId=file_dict['id']) fh = io.BytesIO() downloader = MediaIoBaseDownload(fd=fh, request=request) done = False while not done: status, done = downloader.next_chunk() # log the progress somewhere print('download progress {0}'.format(status.progress() * 100)) fh.seek(0) # store it on the server somewhere # this will be done by the datamanager # pass the recieved string of bits to the datamanager with location, # driveid(just gonna use these for naming ease), and ya well figure that out when we get there file_path = store_file(user_auth, file_dict, fh, "drive_storage") file_dict['localpath'] = file_path return file_dict
def get_file_metadata(user_auth: dict, user_email, file_id: str): # initialize "service" # do some auth, probably need to mess w/ this in GDriveInterface gdrive_auth = user_auth # get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # make request for file metadata request = service.files().get(fileId=file_id, fields=_FILE_FIELDS).execute() filemeta = request ''' # if the file hasn't been moved to trash by the user if not request['trashed']: # build the file object with no local_file_path, can be added with a second call to get_file try: filemeta = DigiJsonBuilder.create_file(name=request['name'], drive_id=request['id'], class_id=None, local_file_path=None, drive_path=request['parents'], classpath=None, size=request['size']) except KeyError as k: if k == 'size': filemeta = DigiJsonBuilder.create_file(name=request['name'], drive_id=request['id'], class_id=None, local_file_path=None, drive_path=request['parents'], classpath=None, size=None) # add the other stuff we get from gdrive to the object, don't necessarily need it but might be useful eventually filemeta.update({'mimeType': request['mimeType'], 'capabilities': request['capabilities']}) # return metadata ''' return filemeta
def upload_file(user_auth, user_email, file_dict, fileobj, drivepath): uploadjson = None gdrive_auth = get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES) # get the (local) file and needed data to upload to drive # create drive json # make sure file got uploaded correctly/completely uploadsuccess = service.files().create(body='json object', media_body='filepath') return uploadsuccess
def get_file_comments(user_auth, user_email, fileid): comments = [] # do auth and create service gdrive_auth = get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # get comments on the file file_comm_list = service.comments.list(fileId=fileid) # just create a (python)json object # because json i can just slap that shit in there and not care about it lol comm = {"comments": file_comm_list['comments']} return comm
def get_drive_list(user_auth, user_email): # do auth and create service gdrive_auth = get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # get list of drive objects (this is like, mydrive, shared drives, etc. no files) # with all list getters, there is a list_next(prev_req, prev_resp) that can be used, gonna need a while loop # for those in the future but for now well just do one page of results gdrive_list = service.drives().list() mydrive = get_file_list(user_auth, user_email, 'my-drive') drivelist = [mydrive] for drive in gdrive_list['drives']: driveobj = DigiJsonBuilder.create_drive( drive['id'], get_file_list(user_auth, drive['id']), drive['capabilities']) driveobj = _get_drive_permissions(driveobj, drive) drivelist.append(driveobj) # convert to digijson return drivelist
def get_file_list(user_auth, user_email, driveid): filelist = [] # get list of files in the specified drive gdrive_auth = user_auth # get_gdrive_auth(user_auth['user_id']) service = create_service(gdrive_auth, _API_NAME, _API_VERSION, _SCOPES, user_email=user_email) # see get_drive_list if driveid == 'my-drive': gdrive_list = service.files().list(fields="files(" + _FILE_FIELDS + ")").execute() else: gdrive_list = service.files().list(fields="files(" + _FILE_FIELDS + ")", driveId=driveid, supportsAllDrives=True, includeItemsFromAllDrives=True, corpora='drive').execute() filemeta = {} for file in gdrive_list['files']: if not file['trashed']: try: filemeta = DigiJsonBuilder.create_file( name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=file['size']) except KeyError as k: print(k) try: filemeta.update({'mimeType': file['mimeType']}) filemeta.update({'capabilities': file['capabilities']}) except KeyError as k: print(k) filelist.append(filemeta) ''' for file in gdrive_list['files']: if not file['trashed']: try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=file['size']) except KeyError as k: if k == 'parents': try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=None, classpath=None, size=file['size']) except KeyError as k1: if k1 == 'size': filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=None, classpath=None, size=None) if k == 'size': try: filemeta = DigiJsonBuilder.create_file(name=file['name'], drive_id=file['id'], class_id=None, local_file_path=None, drive_path=file['parents'], classpath=None, size=None) except KeyError as k1: # throw an error return k1, k else: return k try: filemeta.update({'mimeType': file['mimeType']}) filemeta.update({'capabilities': file['capabilities']}) except KeyError as k: if k == 'mimeType': try: filemeta.update({'capabilities': file['capabilities']}) except KeyError as k1: # throw an error? or just log? return None if k == 'capabilities': filemeta.update({'capabilities': None}) # should probably do some logging here... filelist.append(filemeta) ''' # convert to digijson return filelist