Exemplo n.º 1
0
def search(query, limit=10):
    filelist = []
    # Search through file names
    page_token = None
    while True:
        response = service.files().list(
            q="name contains '%s'" % query,
            spaces='drive',
            fields='nextPageToken, files(id, name)',
            pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            filelist.append((file.get('name'), file.get('id')))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    # Search through files
    page_token = None
    while True:
        response = service.files().list(
            q="fullText contains '%s'" % query,
            spaces='drive',
            fields='nextPageToken, files(id, name)',
            pageToken=page_token).execute()
        for file in response.get('files', []):
            # Process change
            filelist.append((file.get('name'), file.get('id')))
        page_token = response.get('nextPageToken', None)
        if page_token is None:
            break

    for name, fileID in filelist[:limit]:
        print('Found file: %s (%s)' % (name, fileID))
Exemplo n.º 2
0
def download():
    global service
    root_id = "'0AM4f9RkfOJX_Uk9PVA'"
    result_num = 1
    filename = []
    fileid = []
    query = service.files().list(q=root_id + " in parents").execute()
    for file in query.get('files', []):
        fn = file.get('name')
        fi = file.get('id')

        filename.append(fn)
        fileid.append(fi)
        print "Result Id : ", result_num
        print "File : " + fn + "\nFile Id : " + fi
        print "------------------"
        result_num += 1
    choice = input("Enter The Result Number To DOwnload : ")
    if (choice >= 1 and choice < result_num):
        request = service.files().get_media(fileId=fileid[choice - 1])
        fh = io.BytesIO()
        fh = io.FileIO("Hello/" + filename[choice - 1], 'wb')
        downloader = MediaIoBaseDownload(fh, request)
        done = False
        while done is False:
            status, done = downloader.next_chunk()
            print "Download %d%%." % int(status.progress() * 100)
Exemplo n.º 3
0
def search_folder(service, update_drive_folder_name=None):
    """
    如果云端文件夹名称相同,则只会选择一个文件夹上传,请勿取名相同
    :param service: 认证用
    :param update_drive_folder_name: 取得指定文件夹的id,沒有的话回传None,给错也会回传None
    :return:
    """
    get_folder_id_list = []
    # print(len(get_folder_id_list))
    if update_drive_folder_name is not None:
        response = service.files().list(
            fields="nextPageToken, files(id, name)",
            spaces='drive',
            q="name = '" + update_drive_folder_name +
            "' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
        ).execute()
        for file in response.get('files', []):
            # Process change
            print('Found folder: %s (%s)' % (file.get('name'), file.get('id')))
            get_folder_id_list.append(file.get('id'))
        if len(get_folder_id_list) == 0:
            print(
                "Cloud folder does not exist! , so the file is uploaded to the cloud root"
            )
            return None
        else:
            return get_folder_id_list
    return None
Exemplo n.º 4
0
def uploadFile(ingrediantFileName, stepsFileName, recipeName):
    drive = getAuthorization()
    folder_id = '1G5VdY0IyyevSlZMtH8GYRx4FOmN279-B'
    metadata = {
        'name': '{}'.format(recipeName),
        'mimeType': 'application/vnd.google-apps.folder',
        'parents': [folder_id]
    }
    file = drive.files().create(body=metadata, fields='id').execute()

    folder_id = file.get('id')
    metadata = {
        'name': '{}.txt'.format(ingrediantFileName),
        'parents': [folder_id]
    }
    media = MediaFileUpload('{}.txt'.format(ingrediantFileName),
                            mimetype='text/plain')
    file = drive.files().create(body=metadata, media_body=media,
                                fields='id').execute()

    metadata = {'name': '{}.txt'.format(stepsFileName), 'parents': [folder_id]}
    media = MediaFileUpload('{}.txt'.format(stepsFileName),
                            mimetype='text/plain')
    file = drive.files().create(body=metadata, media_body=media,
                                fields='id').execute()

    print('File ID: %s' % file.get('id'))
Exemplo n.º 5
0
async def create_directory(http, directory_name, parent_id):
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    permissions = {
        "role": "reader",
        "type": "anyone",
        "value": None,
        "withLink": True
    }
    file_metadata = {
        "title": directory_name,
        "mimeType": G_DRIVE_DIR_MIME_TYPE
    }
    if parent_id is not None:
        file_metadata["parents"] = [{"id": parent_id}]
    file = drive_service.files().insert(body=file_metadata,
                                        supportsTeamDrives=True).execute()
    file_id = file.get("id")
    try:
        drive_service.permissions().insert(fileId=file_id,
                                           body=permissions).execute()
    except:
        pass
    logger.info("Created Gdrive Folder:\nName: {}\nID: {} ".format(
        file.get("title"), file_id))
    return file_id
Exemplo n.º 6
0
async def gdrive_search(http, search_query):
    if parent_id:
        query = "'{}' in parents and (title contains '{}')".format(
            parent_id, search_query)
    else:
        query = "title contains '{}'".format(search_query)
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    page_token = None
    res = ""
    while True:
        try:
            response = drive_service.files().list(
                q=query,
                spaces="drive",
                fields="nextPageToken, items(id, title, mimeType)",
                pageToken=page_token).execute()
            for file in response.get("items", []):
                file_title = file.get("title")
                file_id = file.get("id")
                if file.get("mimeType") == G_DRIVE_DIR_MIME_TYPE:
                    res += f"`[FOLDER] {file_title}`\nhttps://drive.google.com/drive/folders/{file_id}\n\n"
                else:
                    res += f"`{file_title}`\nhttps://drive.google.com/uc?id={file_id}&export=download\n\n"
            page_token = response.get("nextPageToken", None)
            if page_token is None:
                break
        except Exception as e:
            res += str(e)
            break
    if search_query != '':
        msg = f"**Google Drive Query**:\n`{search_query}`\n\n**Results**\n\n{res}"
    else:
        msg = f"**Google Drive Query**:\n\n**Results**\n\n{res}"
    return msg
Exemplo n.º 7
0
    def list_files_from_folder_id(self, folder_id):
        itemsList = []

        page_token = None
        while True:
            response = self.drive_service.files().list(
                q="'" + folder_id + "' in parents",
                spaces='drive',
                fields='nextPageToken, files(id, name, modifiedTime, mimeType, size)',
                pageToken=page_token
            ).execute()

            for file in response.get('files', []):
                # Process change
                itemsList.append({
                    'id': file.get('id'),
                    'name': file.get('name'),
                    'modifiedTime': file.get('modifiedTime'),
                    'type': file.get('mimeType'),
                    'size': file.get('size')
                })
            page_token = response.get('nextPageToken', None)
            if page_token is None:
                break

        return itemsList
Exemplo n.º 8
0
def createFolder(name):
    file_metadata = {
        'name': name,
        'mimeType': 'application/vnd.google-apps.folder',
        'parents': [shiro_store_user_folder_id],
    }
    user_permission = {'role': 'reader', 'type': 'anyone'}
    file = service.files().create(body=file_metadata, fields='id').execute()

    def callback(request_id, response, exception):
        if exception:
            # Handle error
            print
            exception
        else:
            print
            "Permission Id: %s" % response.get('id')

    batch = service.new_batch_http_request(callback=callback)
    batch.add(service.permissions().create(
        fileId=file.get('id'),
        body=user_permission,
        fields='id',
    ))
    batch.execute()
    # permission = service.permissions().create(fileId=file.get('id'),body=file_metadata,fields='id').execute()
    print('Folder ID: %s' % file.get('id'))
    return file.get('id')
Exemplo n.º 9
0
def createfolder(folder, recursive=False):
    if recursive:
        print('recursive ON')
        ids = {}
        for root, sub, files in os.walk(folder):
            par = os.path.dirname(root)

            file_metadata = {
                'name': os.path.basename(root),
                'mimeType': 'application/vnd.google-apps.folder'
            }
            if par in ids.keys():
                file_metadata['parents'] = [ids[par]]
            print(root)
            file = service.files().create(body=file_metadata,
                                          fields='id').execute()
            id = file.get('id')
            print(id)
            ids[root] = id
            for f in files:
                print(root+'/'+f)
                upload(root + '/' + f, id)
    else:
        print('recursive OFF')
        file_metadata = {
                'name': os.path.basename(folder),
                'mimeType': 'application/vnd.google-apps.folder'
            }
        file = service.files().create(body=file_metadata,
                                          fields='id').execute()
        print(file.get('id'))
Exemplo n.º 10
0
def search_folder(service, update_drive_folder_name=None):
    """
    如果雲端資料夾名稱相同,則只會選擇一個資料夾上傳,請勿取名相同名稱
    :param service: 認證用
    :param update_drive_folder_name: 取得指定資料夾的id,沒有的話回傳None,給錯也會回傳None
    :return:
    """
    get_folder_id_list = []
    print(len(get_folder_id_list))
    if update_drive_folder_name is not None:
        response = service.files().list(
            fields="nextPageToken, files(id, name)",
            spaces='drive',
            q="name = '" + update_drive_folder_name +
            "' and mimeType = 'application/vnd.google-apps.folder' and trashed = false"
        ).execute()
        for file in response.get('files', []):
            # Process change
            print('Found folder: %s (%s)' % (file.get('name'), file.get('id')))
            get_folder_id_list.append(file.get('id'))
        if len(get_folder_id_list) == 0:
            print("你給的資料夾名稱沒有在你的雲端上!,因此檔案會上傳至雲端根目錄")
            return None
        else:
            return get_folder_id_list
    return None
Exemplo n.º 11
0
async def create_directory(http, directory_name, parent_id, shared_drive=None):
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    permissions = {
        "role": "reader",
        "type": "anyone",
        "value": None,
        "withLink": True
    }
    if shared_drive is not None:
        if bool(shared_drive):
            if parent_id is "root":
                results = drive_service.drives().list(pageSize=10).execute()
                parent_id = results['drives'][0]['id']
    file_metadata = {
        "title": directory_name,
        "mimeType": G_DRIVE_DIR_MIME_TYPE
    }
    if parent_id is not None:
        file_metadata["parents"] = [{"id": parent_id}]
    file = drive_service.files().insert(body=file_metadata).execute()
    file_id = file.get("id")
    drive_service.permissions().insert(fileId=file_id,
                                       body=permissions).execute()
    logger.info("Created Gdrive Folder:\nName: {}\nID: {} ".format(
        file.get("title"), file_id))
    return file_id
Exemplo n.º 12
0
def createFolder(folderName):
    #create folder with provided name
    file_metadata = {
        'name': folderName,
        'mimeType': 'application/vnd.google-apps.folder',
        'parents': [music_folder_id],
    }
    file = drive_service.files().create(body=file_metadata, fields='id').execute()
    
    #share folder with custom permission https://developers.google.com/drive/api/v3/manage-sharing
    def callback(request_id, response, exception):
        if exception:
            # Handle error
            print(exception)
        else:
            print("Permission Id: %s" % response.get('id'))

    batch = drive_service.new_batch_http_request(callback=callback)
    user_permission = {
        'type': 'anyone',
        'role': 'reader'
    }
    batch.add(drive_service.permissions().create(
        fileId=file.get('id'),
        body=user_permission,
        fields='id',
    ))
    batch.execute()
    print(f"Folder ID: {file.get('id')}")
    return file.get('id')
Exemplo n.º 13
0
    def get_list(self):
        files = []
        data = {}

        # if folder is not created: create folder and return empty list
        if self.folder is None:
            self._create_folder()
            data['files'] = files
            return data

        data['folder_id'] = self.folder.folder_id

        page_token = None

        while True:
            q = "'" + data['folder_id'] + "' in parents"
            response = self.service.files().list(q=q,
                                                  fields='nextPageToken, files(id, name)',
                                                  pageToken=page_token).execute()
            for file in response.get('files', []):
                files.append({'file_id':file.get('id'), 'name':file.get('name')})
            page_token = response.get('nextPageToken', None)
            if page_token is None:
                break

        data['files'] = files
        return data
    def __list_items_by_name(self, name, extraQuery=None):
        query = 'name contains \'' + name + '\''

        if extraQuery != None:
            query += " and " + extraQuery

        print(query)
        itemsList = []

        page_token = None
        while True:
            response = self.drive_service.files().list(
                q=query,
                spaces='drive',
                fields=
                'nextPageToken, files(id, name, modifiedTime, mimeType, size ,webContentLink)',
                pageToken=page_token).execute()

            for file in response.get('files', []):
                # Process change
                itemsList.append({
                    'webContentLink': file.get('webContentLink'),
                    'id': file.get('id')
                })
            page_token = response.get('nextPageToken', None)
            if page_token is None:
                break

        return itemsList
Exemplo n.º 15
0
def syncFolder(DRIVE):
    response = DRIVE.files().list(
        q="mimeType='application/vnd.google-apps.folder'"
        and "name='socialCopsFolderSync'",
        spaces='drive').execute()
    for file in response.get('files', []):
        print('Folder at : %s (%s)' % (file.get('name'), file.get('id')))
        folder = file
        break

    parentFolderId = folder.get('id')
    page_token = None
    Q = ("'" + str(parentFolderId) + "'" + ' in parents')
    response = DRIVE.files().list(q=Q,
                                  spaces='drive',
                                  fields='nextPageToken, files(id, name)',
                                  pageToken=page_token).execute()
    for file in response.get('files', []):
        if file.get('name') == "logs":
            pass
        else:
            request = DRIVE.files().get_media(fileId=file.get('id'))
            fh = io.FileIO(file.get('name'), mode='wb')
            downloader = MediaIoBaseDownload(fh, request)
            done = False
            while done is False:
                status, done = downloader.next_chunk()
    try:
        print("Download %d%%." % int(status.progress() * 100))
        print("Done!!!>")
    except:
        pass
Exemplo n.º 16
0
    def find_folder_id(self, folder_name, to_print=True):
        """
		The function find the ID of a folder. In order to maximize the search
		it is best to give unique name to folder
		"""
        search = "mimeType = 'application/vnd.google-apps.folder'" \
        " and name contains '" + str(folder_name) + "'"
        page_token = None
        while True:
            response = self.service_drive.files().list(
                q=search,
                spaces='drive',
                fields='nextPageToken,'
                'files(id, name)',
                pageToken=page_token).execute()
            for file in response.get('files', []):
                # Process change
                folder_id = file.get('id')
                if to_print:
                    print('Found file: %s (%s)' %
                          (file.get('name'), folder_id))
                page_token = response.get('nextPageToken', None)
                return folder_id
            if page_token is None:
                break
        print('Folder {} not found'.format(folder_name))
Exemplo n.º 17
0
    def upload(self, path, email):
        mime = MimeTypes()
        credentials = self.get_credentials()
        # http = credentials.authorize(httplib2.Http())
        # service = discovery.build('drive', 'v3', http=http)
        service = build('drive', 'v3', credentials=credentials)

        file_metadata = {
            'name': os.path.basename(path),
        }

        media = MediaFileUpload(
            path, mimetype=mime.guess_type(
                os.path.basename(path))[0])  #, resumable=True)
        try:
            file = service.files().create(body=file_metadata,
                                          media_body=media,
                                          fields='id').execute()
            print(file.get('id'))
        except HttpError:
            print('The file is corrupted')
            pass

        self.share(file.get('id'), email)
        link = 'https://drive.google.com/open?id=' + file.get('id')
        print('Uploaded file to {url}'.format(
            url='https://drive.google.com/open?id=' + file.get('id')))
        return link
Exemplo n.º 18
0
def ocr(input, input_filetype, output):
    # get the mimetype based on the file extension
    mime_types = {"pdf": 'application/pdf', "jpg": 'image/jpeg', "png": 'image/png',
                 "gif": 'image/gif', "bmp": 'image/bmp', "doc": 'application/msword'}
    input_mime_type = mime_types[input_filetype]

    file_metadata = {'name': input, 'mimeType': 'application/vnd.google-apps.spreadsheet'}
    # Upload the file to Google Drive
    media = MediaFileUpload(input, mimetype=input_mime_type, resumable=True)
    file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
    # Print the file id
    print('File ID: %s' % file.get('id'))
    # Export the file to txt and download it
    request = service.files().export_media(fileId=file.get('id'), mimeType="text/plain")
    dl = MediaIoBaseDownload(io.FileIO(output, "wb"), request)
    is_complete = False
    while not is_complete:
        status, is_complete = dl.next_chunk()
    # Delete the uploaded file
    service.files().delete(fileId=file["id"]).execute()
    print("Output saved to " + output + ".")
    f = open(output, 'r')
    contents = f.read()
    contents = contents.replace('\n', ' ').replace('\r', '')
    contents = contents.replace('  ', ' ').replace(' ', '')
    unwanted = [':',',']
    for i in unwanted :
        contents = contents.replace(i, '')
    #print("the output of OCR : "+contents)
    vehicle_regex = re.compile(r'[A-Z]{2}[0-9]{1,2}[A-Z]{1,2}[0-9]{4}')
    vehicle_nos = vehicle_regex.search(contents)
    if vehicle_nos is not None:
        print(vehicle_nos)
Exemplo n.º 19
0
    def list_files_from_folder_id(self, folder_id):
        itemsList = []

        page_token = None
        while True:
            response = (self.drive_service.files().list(
                q="'" + folder_id + "' in parents",
                spaces="drive",
                fields=
                "nextPageToken, files(id, name, modifiedTime, mimeType, size)",
                page_token=page_token,
            ).execute())

            for file in response.get("files", []):
                # Process change
                itemsList.append({
                    "id": file.get("id"),
                    "name": file.get("name"),
                    "modifiedTime": file.get("modifiedTime"),
                    "type": file.get("mimeType"),
                    "size": file.get("size"),
                })
            page_token = response.get("nextPageToken", None)
            if page_token is None:
                break

        return itemsList
Exemplo n.º 20
0
async def gdrive_search(http, search_query):
    if G_DRIVE_F_PARENT_ID is not None:
        query = "'{}' in parents and (title contains '{}')".format(
            G_DRIVE_F_PARENT_ID, search_query)
    else:
        query = "title contains '{}'".format(search_query)
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    page_token = None
    msg = f"<b>G-Drive Search Query</b>: <code>{search_query}</code>\n\n<b>Results</b>\n"
    while True:
        try:
            response = drive_service.files().list(
                q=query,
                spaces="drive",
                fields="nextPageToken, items(id, title, mimeType)",
                pageToken=page_token).execute()
            for file in response.get("items", []):
                file_title = file.get("title")
                file_id = file.get("id")
                if file.get("mimeType") == G_DRIVE_DIR_MIME_TYPE:
                    msg += f"🗃️ <a href='https://drive.google.com/drive/folders/{file_id}'>{file_title}</a>"
                    msg += f" <code>{file_id}</code>\n"
                else:
                    msg += f"👉 <a href='https://drive.google.com/uc?id={file_id}&export=download'>{file_title}</a>"
                    msg += f" <code>{file_id}</code>\n"
            page_token = response.get("nextPageToken", None)
            if page_token is None:
                break
        except Exception as e:
            msg += str(e)
            break
    return msg
Exemplo n.º 21
0
	def upload_file(filename, file_name, mime_type):
		global download_url
		global size
		drive_service = build('drive', 'v2', http=http)
		media_body = MediaFileUpload(filename,
						mimetype=mime_type,
						resumable=True)
		body = {
			'title': file_name,
			'description': 'backup',
			'mimeType': mime_type,
			}
		# Permissions body description: anyone who has link can upload
		# Other permissions can be found at https://developers.google.com/drive/v2/reference/permissions
		permissions = {
			'role': 'reader',
			'type': 'anyone',
			'value': None,
			'withLink': True
					}
		# Insert a file
		file = drive_service.files().insert(body=body, media_body=media_body).execute()
		# Insert new permissions
		drive_service.permissions().insert(fileId=file['id'], body=permissions).execute()
		# Define file instance and get url for download
		file = drive_service.files().get(fileId=file['id']).execute()
		download_url = file.get('alternateLink')
		size_unicode = file.get('fileSize')
		size_int = int(size_unicode)
		size_int = size_int/(1048576)
		size = str(size_int)
		return download_url
Exemplo n.º 22
0
def ocr(input, input_filetype, output):
    # get the mimetype based on the file extension
    mime_types = {
        "pdf": 'application/pdf',
        "jpg": 'image/jpeg',
        "png": 'image/png',
        "gif": 'image/gif',
        "bmp": 'image/bmp',
        "doc": 'application/msword'
    }
    input_mime_type = mime_types[input_filetype]

    file_metadata = {
        'name': input,
        'mimeType': 'application/vnd.google-apps.spreadsheet'
    }
    # Upload the file to Google Drive
    media = MediaFileUpload(input, mimetype=input_mime_type, resumable=True)
    file = service.files().create(body=file_metadata,
                                  media_body=media,
                                  fields='id').execute()
    # Print the file id
    print('File ID: %s' % file.get('id'))
    # Export the file to txt and download it
    request = service.files().export_media(fileId=file.get('id'),
                                           mimeType="text/plain")
    dl = MediaIoBaseDownload(io.FileIO(output, "wb"), request)
    is_complete = False
    while not is_complete:
        status, is_complete = dl.next_chunk()
    # Delete the uploaded file
    service.files().delete(fileId=file["id"]).execute()
    print("Output saved to " + output + ".")
Exemplo n.º 23
0
def uploadFile(filename, filepath, mimetype, folder_id=shiro_store_folder_id):
    file_metadata = {'name': filename, 'parents': ['%s' % folder_id]}
    media = MediaFileUpload(filepath, mimetype=mimetype)
    file = service.files().create(body=file_metadata,
                                  media_body=media,
                                  fields='id').execute()
    print("Uploaded file ", file.get('id'))
    return file.get('id')
Exemplo n.º 24
0
def upload(filename, filepath):
    file_metadata = {'name': filename}
    media = MediaFileUpload(filepath)
    file = service.files().create(body=file_metadata,
                                  media_body=media,
                                  fields='id').execute()
    print('File ID: %s' % file.get('id'))
    return file.get('id')
Exemplo n.º 25
0
def find_folder(name):
    page_token = None
    while True:
        response = DRIVE.files().list(q="name='" + name + "'",
                                      pageToken=page_token).execute()
        for file in response.get('files', []):
            print('Found file: %s (%s)' % (file.get('name'), file.get('id')))
            return (file.get('id'))
        page_token = response.get('nextPageToken', None)
        if page_token is None: break
def upload_to_drive(path_folder, filename, filename_diff, path_file, path_file_diff):
    from oauth2client import file, client, tools
    # try:
    #     import argparse
    #     flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
    # except ImportError:
    #     flags = None

    SCOPES = 'https://www.googleapis.com/auth/drive.appfolder https://www.googleapis.com/auth/drive.file'
    store = file.Storage('credentials.json')
    creds = store.get()

    if not creds or creds.invalid:
        flow = client.flow_from_clientsecrets('client_secret.json', scope=SCOPES)
        creds = tools.run_flow(flow, store)
    DRIVE = discovery.build('drive', 'v3', http=creds.authorize(Http()))

    folder_metadata = {
        'name': path_folder,
        'mimeType': 'application/vnd.google-apps.folder'
    }
    file = DRIVE.files().create(body=folder_metadata,
                                fields='id').execute()
    # print ('Folder ID: %s' % file.get('id'))

    folder_id = file.get('id')

    file_metadata = {
        'name': filename,
        'parents': [folder_id]
    }

    diff_metadata = {
        'name' : filename_diff,
        'parents' : [folder_id]
    }

    media = MediaFileUpload(path_file,
                            mimetype=None,
                            resumable=False)

    media_diff = MediaFileUpload(path_file_diff,
                                 mimetype=None,
                                 resumable=False)

    file = DRIVE.files().create(body=file_metadata,
                                media_body=media,
                                fields='id').execute()
    print('File ID: %s' % file.get('id'))

    file_diff = DRIVE.files().create(body=diff_metadata,
                                media_body=media_diff,
                                fields='id').execute()
    print('File ID: %s' % file_diff.get('id'))
Exemplo n.º 27
0
    def create_folder(self, folder_name, verbose=False):
        file_metadata = {
            'name': folder_name,  # 폴더 명
            'mimeType': 'application/vnd.google-apps.folder'
        }
        file = self.service.files().create(body=file_metadata,
                                           fields='id').execute()  # 폴더 실행문
        if verbose:
            print('Folder ID: %s' % file.get('id'))

        return file.get('id')
Exemplo n.º 28
0
 def create_folder(self, name="CloudBackup"):
     file_metadata = {
         'name': name,
         'mimeType': 'application/vnd.google-apps.folder'
     }
     file = self.drive_service().files().create(body=file_metadata,
                                                fields='id').execute()
     config = self.get_config()
     config['folder_id'] = str(file.get('id'))
     self.update_config(config)
     open('folder_id', 'w+').write(str(file.get('id')))
Exemplo n.º 29
0
def download_message_image(folderName="random"):
    print('Fetching Image')
    file = {}
    data = {}
    try:
        file = get_message_image(folderName)
        data = download_file(file.get("file"))
    except Exception as e:
        settings.maybePrint(e)
        return {}
    settings.update_value("input",data.get("path"))
    return {"path":data.get("path"), "file":file.get("file")}
Exemplo n.º 30
0
def list_owners(service, folder_id):
    page_token = None

    response = service.files().list(q="trashed = false", # and '%s' in parents" % (folder_id,),
                                    spaces='drive',
                                    fields='nextPageToken, files(id, name)',
                                    pageToken=page_token).execute()

    for file in response.get('files', []):
        id = file.get('id')
        ownership = service.files().get(fileId=id, fields='owners').execute()['owners']
        # owner = resource['owners']
        print('File %s has is owned by %s' % (file.get('name'), ownership[0]['emailAddress']))
Exemplo n.º 31
0
def _drive_walk(service, path, folder_id):
    """
    フォルダの階層をたどる
    """
    #print("フォルダ: {}".format(path))
    # フォルダのみを取得する 
    q = "'{}' in parents and mimeType = '{}'".format(
        folder_id, MIME_TYPE['フォルダ'])
    response = service.files().list(fields="files(id, name)", q=q).execute()

    for file in response.get('files', []):
        new_path = path + file.get('name') + '/'

        # データベースに追加(INSERT)または更新(REPLACE)する
        fields = {'path': new_path, 'id': file.get('id')}
        Folder.insert(fields).upsert(upsert=True).execute()

        # 下の階層を処理する
        _drive_walk(service, new_path, file.get('id'))