Ejemplo n.º 1
0
def writeToGDrive(filename, source, folder_id, md5):
    file_metadata = {
        'name': filename,
        'parents': [folder_id],
        'mimeType': '*/*',
        'description': md5
    }
    media = MediaFileUpload(source,
                            chunksize=1024 * 1024,
                            mimetype='*/*',
                            resumable=True)

    if fileInGDrive(filename, folder_id) is False:
        file = SERVICE.files().create(body=file_metadata,
                                      media_body=media,
                                      fields='id')
        response = None
        while response is None:
            status, response = file.next_chunk()
            if status:
                print("Uploaded %d%%." % int(status.progress() * 100))

        check = fileInGDrive(filename, folder_id)
        print('Upload Success for ' + filename)
        return check

    else:
        print('File already exists as', filename)
Ejemplo n.º 2
0
async def upload_file(http, file_path, file_name, mime_type, event, parent_id):
    # Create Google Drive service instance
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    # File body description
    media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
    body = {
        "title": file_name,
        "description": "Uploaded using @UniBorg gDrive v2",
        "mimeType": mime_type,
    }
    if parent_id is not None:
        body["parents"] = [{"id": parent_id}]
    # 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,
                                        supportsTeamDrives=True)
    response = None
    display_message = ""
    while response is None:
        status, response = file.next_chunk()
        await asyncio.sleep(1)
        if status:
            percentage = int(status.progress() * 100)
            progress_str = "[{0}{1}]\nProgress: {2}%\n".format(
                "".join(["█" for i in range(math.floor(percentage / 5))]),
                "".join(["░" for i in range(20 - math.floor(percentage / 5))]),
                round(percentage, 2))
            current_message = f"uploading to gDrive\nFile Name: {file_name}\n{progress_str}"
            if display_message != current_message:
                try:
                    await event.edit(current_message)
                    display_message = current_message
                except Exception as e:
                    logger.info(str(e))
                    pass
    file_id = response.get("id")
    try:
        # Insert new permissions
        drive_service.permissions().insert(fileId=file_id,
                                           body=permissions).execute()
    except:
        pass
    # Define file instance and get url for download
    file = drive_service.files().get(fileId=file_id,
                                     supportsTeamDrives=True).execute()
    download_url = file.get("webContentLink")
    return download_url
Ejemplo n.º 3
0
async def upload_file(http, file_path, file_name, mime_type, event, parent_id):
    # Create Google Drive service instance
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    # File body description
    media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
    body = {
        "title": file_name,
        "description": "Uploaded using @UniBorg gDrive v2",
        "mimeType": mime_type,
    }
    if parent_id is not None:
        body["parents"] = [{"id": parent_id}]
    # 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)
    response = None
    display_message = ""
    while response is None:
        status, response = file.next_chunk(
        )  #Credits: https://github.com/AvinashReddy3108/PaperplaneExtended/commit/df65da55d16a6563aa9023cac2bedf43248379f5
        #await asyncio.sleep(5)
        #if status:
        #percentage = int(status.progress() * 100)
        #progress_str = "[{0}{1}]\nProgress: {2}%\n".format(
        #"".join(["⬤" for i in range(math.floor(percentage / 5))]),
        #"".join(["◯" for i in range(20 - math.floor(percentage / 5))]),
        #round(percentage, 2)
        #)
        #current_message = f"Uploading to G-Drive:\nFile Name: `{file_name}`\n{progress_str}"
        #if display_message != current_message:
        #try:
        #await event.edit(current_message)
        #display_message = current_message
        #except Exception as e:
        #logger.info(str(e))
        #pass
    file_id = response.get("id")
    # 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("webContentLink")
    return download_url
Ejemplo n.º 4
0
async def upload_file(http, file_path, file_name, mime_type, event, parent_id):
    # Google Drive servis örneği oluşturur.
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    # Dosya tipi açıklaması
    media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
    body = {
        "title": file_name,
        "description": "Seden UserBot kullanılarak yüklendi.",
        "mimeType": mime_type,
    }
    if parent_id:
        body["parents"] = [{"id": parent_id}]
    # İzinlerin açıklaması: Linke sahip olan herkes görebilir.
    # Diğer izinler şurada bulunabilir: https://developers.google.com/drive/v2/reference/permissions
    permissions = {
        "role": "reader",
        "type": "anyone",
        "value": None,
        "withLink": True
    }
    # Dosyayı ekler
    file = drive_service.files().insert(body=body, media_body=media_body)
    response = None
    display_message = ""
    while response is None:
        status, response = file.next_chunk()
        await asyncio.sleep(1)
        if status:
            percentage = int(status.progress() * 100)
            progress_str = "[{0}{1}] {2}%".format(
                "".join(["▰" for i in range(math.floor(percentage / 10))]),
                "".join(["▱"
                         for i in range(10 - math.floor(percentage / 10))]),
                round(percentage, 2))
            current_message = f"Google Drive'a uploadlanıyor.\nDosya Adı: {file_name}\n{progress_str}"
            if display_message != current_message:
                try:
                    await event.edit(current_message)
                    display_message = current_message
                except Exception as e:
                    LOGS.info(str(e))
                    pass
    file_id = response.get("id")
    # Yeni izinleri ekler.
    drive_service.permissions().insert(fileId=file_id,
                                       body=permissions).execute()
    # Dosya örneğini tanımlar ve indirmek için bağlantıyı edinir.
    file = drive_service.files().get(fileId=file_id).execute()
    download_url = file.get("webContentLink")
    return download_url
Ejemplo n.º 5
0
async def upload_file(http, file_path, file_name, mime_type, event):
    # Create Google Drive service instance
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    # File body description
    media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
    body = {
        "title": file_name,
        "description": "Uploaded using PaperplaneExtended Userbot.",
        "mimeType": mime_type,
    }
    if parent_id:
        body["parents"] = [{"id": parent_id}]
    # 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)
    response = None
    while response is None:
        status, response = file.next_chunk()
        await asyncio.sleep(1)
        if status:
            percentage = int(status.progress() * 100)
            progress_str = "[{0}{1}]\nProgress: {2}%\n".format(
                ''.join(["█" for i in range(math.floor(percentage / 5))]),
                ''.join(["░" for i in range(20 - math.floor(percentage / 5))]),
                round(percentage, 2))
            await event.edit(
                f"Uploading to Google Drive...\n\nFile Name: {file_name}\n{progress_str}"
            )
    if file:
        await event.edit(file_name + " uploaded successfully")
    # Insert new permissions
    drive_service.permissions().insert(fileId=response.get('id'),
                                       body=permissions).execute()
    # Define file instance and get url for download
    file = drive_service.files().get(fileId=response.get('id')).execute()
    download_url = response.get("webContentLink")
    return download_url
Ejemplo n.º 6
0
async def upload_to_drive(http, file_path, file_name, mime_type, event,
                          parent_id):
    drive_service = build("drive", "v2", http=http, cache_discovery=False)
    media_body = MediaFileUpload(file_path, mimetype=mime_type, resumable=True)
    body = {
        "title": file_name,
        "description": "Used for hosting OTA Installations - IPAbox",
        "mimeType": mime_type,
    }
    if parent_id is not None:
        body["parents"] = [{"id": parent_id}]
    permissions = {
        "role": "reader",
        "type": "anyone",
        "value": None,
        "withLink": True
    }
    file = drive_service.files().insert(body=body, media_body=media_body)
    response = None
    display_message = ""
    while response is None:
        status, response = file.next_chunk()
        await asyncio.sleep(1)
        if status:
            percentage = int(status.progress() * 100)
            if display_message != percentage:
                try:
                    await event.edit(f"Processing `{file_name}`: {percentage}%"
                                     )
                    display_message = percentage
                except Exception as e:
                    logger.info(str(e))
                    pass
    file_id = response.get("id")
    try:
        drive_service.permissions().insert(fileId=file_id,
                                           body=permissions).execute()
    except:
        pass
    file = drive_service.files().get(fileId=file_id).execute()
    download_url = file.get("webContentLink")
    return file_id