-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdrive_download.py
40 lines (30 loc) · 1.14 KB
/
gdrive_download.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from pydrive.drive import GoogleDrive
from pydrive.auth import GoogleAuth
import os
def gg(name):
settings_path = 'settings.yaml'
gauth = GoogleAuth(settings_file=settings_path)
gauth.LoadCredentialsFile("mycreds1.txt")
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile("mycreds1.txt")
drive = GoogleDrive(gauth)
print('Started!!')
file_list = drive.ListFile({'q': "'YOUR API KEY' in parents and trashed=False"}).GetList()
f = drive.CreateFile({'title': f"{name}_final.csv", 'parents': [{'id': 'YOUR API KEY'}]}) #change to your drive id
file_name = f['title']
print('File name read....\n')
try:
for f1 in file_list:
print(f1)
if f1['title'] == file_name:
print("Downloading")
if os.path.exists(f"{name}_final.csv"):
os.remove(f"{name}_final.csv")
f1.GetContentFile(f"{name}_final.csv")
except:
print('Unable to get file.')