Esempio n. 1
0
    def get_file_values(self, spreadsheet_id, rows_range):
        if spreadsheet_id is None:
            raise MissingGoogleDriveFileException(
                'Missing file: {}'.format(spreadsheet_id))

        sheets_service = super().get_service(self.SHEETS_SERVICE_ID,
                                             self.SHEETS_SERVICE_VERSION)

        if spreadsheet_id in self.cached_file_values:
            if rows_range in self.cached_file_values[spreadsheet_id]:
                return self.cached_file_values[spreadsheet_id][rows_range]

        sheet = sheets_service.spreadsheets()

        try:
            result = sheet.values().get(spreadsheetId=spreadsheet_id,
                                        range=rows_range).execute()
            values = result.get('values', [])

            if len(values) > 0:
                self.cached_file_values.update(
                    {spreadsheet_id: {
                        rows_range: values
                    }})
            return values
        except HttpError as e:
            http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
            raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 2
0
    def get_file_from_id(self, file_id: str):
        drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                            self.DRIVE_SERVICE_VERSION)
        try:
            google_file_dict = drive_service.files().get(
                fileId=file_id, fields=self.FIELDS_FILE_METADATA).execute()

            return GoogleFileDictToGoogleFile(
            ).google_file_dict_to_google_file(google_file_dict)
        except HttpError as e:
            http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
            raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 3
0
 def update_file_parent(self, file_id, current_parent, new_parent):
     drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                         self.DRIVE_SERVICE_VERSION)
     try:
         file_update = drive_service.files().update(
             fileId=file_id,
             addParents=new_parent,
             removeParents=current_parent)
         file_update.execute()
     except HttpError as e:
         http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
         raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 4
0
 def create_permission(self, document_id: str, role: str, email_address):
     drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                         self.DRIVE_SERVICE_VERSION)
     try:
         drive_service.permissions().create(fileId=document_id,
                                            body={
                                                'type': 'user',
                                                'emailAddress':
                                                email_address,
                                                'role': role,
                                            }).execute()
     except HttpError as e:
         http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
         raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 5
0
    def create_folder(self, name):
        file_metadata = {'name': name, 'mimeType': GoogleDrive.MIMETYPE_FOLDER}
        drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                            self.DRIVE_SERVICE_VERSION)
        try:
            folder = drive_service.files().create(
                body=file_metadata,
                fields=self.FIELDS_BASIC_FILE_METADATA).execute()
        except HttpError as e:
            http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
            raise GoogleApiClientHttpErrorException(http_error)

        return GoogleFileDictToGoogleFile().google_file_dict_to_google_file(
            folder)
Esempio n. 6
0
 def copy_file(self, file_id, new_filename):
     drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                         self.DRIVE_SERVICE_VERSION)
     try:
         results = drive_service.files().copy(fileId=file_id,
                                              body={
                                                  'name':
                                                  new_filename,
                                                  'mimeType':
                                                  self.MIMETYPE_DOCUMENT
                                              }).execute()
         return results.get('id')
     except HttpError as e:
         http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
         raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 7
0
    def __get_files(self, query: str):
        try:
            page_token = None
            total_google_files = []
            while True:
                google_files, next_page_token = self.list_files(
                    page_token=page_token, query=query)
                total_google_files = total_google_files + google_files

                if next_page_token is None:
                    return total_google_files
                else:
                    page_token = next_page_token
            return None
        except HttpError as e:
            http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
            raise GoogleApiClientHttpErrorException(http_error)
Esempio n. 8
0
    def list_files(self, page_token: str, query: str):
        drive_service = super().get_service(self.DRIVE_SERVICE_ID,
                                            self.DRIVE_SERVICE_VERSION)
        try:
            response = drive_service.files().list(
                q=query,
                pageSize=100,
                spaces='drive',
                corpora='user',
                fields=f'nextPageToken, files({self.FIELDS_FILE_METADATA})',
                pageToken=page_token).execute()
        except HttpError as e:
            http_error = GoogleApiClientHttpErrorBuilder().from_http_error(e)
            raise GoogleApiClientHttpErrorException(http_error)

        google_files = [
            GoogleFileDictToGoogleFile().google_file_dict_to_google_file(
                google_file_dict)
            for google_file_dict in response.get('files', [])
        ]
        next_page_token = response.get('nextPageToken', None)

        return google_files, next_page_token