I am developing a python script that downloads some files from that website. Is there a way to download and upload them directly to google drive with the API?
This is the snipet that download the file:
def download(): #sólo para fichero de oferta
from webbot import Browser
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
url='www.myweb.com'
web=Browser()
web.go_to(url)
web.click(xpath='/html/body/div[2]/div/div/button')
web.type('myusername', into='idUsuario', id='Usuario')
web.type('password' , into='password' , id='password') #specific selection
web.click(tag='Entrar', xpath='//*[@id="enter-button"]/a')
time.sleep(3)
web.driver.find_element_by_xpath('//*[@id="NavSeleccionApl"]/ul/li[6]/a').click()
time.sleep(1)
web.driver.find_element_by_xpath('//*[@id="formContent:tablaPlantillasPorPerfil:2:j_id710"]/span').click()
try:
time.sleep(30)
web.driver.find_element_by_xpath('//*[@id="formContent:j_id197"]').click()
except:
print('try again')
and this is the one that uploads my downloaded file from my computer:
def adrive():
import datetime
path=r'C:\Users\myself'
print('Uploading to Drive..')
now = datetime.datetime.now()
file_name=f'file_{"_"+str(now.day) + "_"+str(now.month)+ "_"+str(now.year)}.xlsx'
file_to_up=os.path.join(file_name, path)
headers = {"Authorization": "Bearer myauthcode"} #auth code
para = {
"name": file_name,
"parents":["1KPrqoChDo6ZL"] #folder in drive
}
files = {
'data': ('metadata', json.dumps(para), 'application/json; charset=UTF-8'),
'file': open(file_to_up, "rb")
}
r = requests.post(
"https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart",
headers=headers,
files=files
)
print('Subido a Drive')
If I am not wrong, you have already saved the file, you need to just upload it. Now, you had used google-driver-api
tag so, this answer is how to use it:
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from googleapiclient.http import MediaFileUpload
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/drive.metadata.readonly','https://www.googleapis.com/auth/drive.file']
def upload_files():
"""
Creates a folder and upload a file to it
"""
# authenticate account
service = get_gdrive_service()
# folder details we want to make
folder_metadata = {
"name": "TestFolder",
"mimeType": "application/vnd.google-apps.folder"
}
# create the folder
file = service.files().create(body=folder_metadata, fields="id").execute()
# get the folder id
folder_id = file.get("id")
print("Folder ID:", folder_id)
# upload a file text file
# first, define file metadata, such as the name and the parent folder ID
file_metadata = {
"name": "test.txt",
"parents": [folder_id]
}
# upload
media = MediaFileUpload("test.txt", resumable=True)
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
print("File created, id:", file.get("id"))
You may visit here for more/detail information. This will also be helpful for you.