-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgdrive.py
69 lines (56 loc) · 1.87 KB
/
gdrive.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
import requests
import os
import csv
import json
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
parentID = "1jjWjQkEbKTmj2T6CR2utL7P1VBja-M5A"
gauth = GoogleAuth()
# Try to load saved client credentials
gauth.LoadCredentialsFile("mycreds.txt")
if gauth.credentials is None:
# Authenticate if they're not there
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
# Refresh them if expired
gauth.Refresh()
else:
# Initialize the saved creds
gauth.Authorize()
# Save the current credentials to a file
gauth.SaveCredentialsFile("mycreds.txt")
drive = GoogleDrive(gauth)
'''
fileOutsidePune = drive.CreateFile({"parents": [{"kind": "drive#fileLink","id": "1kxRZk7hBjBDtUgKm4hYgHvw2NDZw2V8R"}]})
fileOutsidePune.SetContentFile("outsidePune.csv")
fileOutsidePune.Upload()
'''
def createFolder(folderName):
folder_metadata = {
'title': folderName,
# Define the file type as folder
'mimeType': 'application/vnd.google-apps.folder',
# ID of the parent folder
'parents': [{"kind": "drive#fileLink", "id": parentID}]
}
folder = drive.CreateFile(folder_metadata)
folder.Upload()
# insert new permission
permission = folder.InsertPermission({
'type': 'anyone'
,'value': 'anyone'
,'role': 'writer'
})
def uploadFile(folderID,fileName):
file = drive.CreateFile({"parents": [{"kind": "drive#fileLink","id": folderID}]})
file.SetContentFile(fileName)
file.Upload()
def getFolderDict():
folderDict = {}
file_list = drive.ListFile({'q': "'1jjWjQkEbKTmj2T6CR2utL7P1VBja-M5A' in parents and trashed=false"}).GetList()
for file1 in file_list:
folderDict[file1['title']]=file1['id']
return(folderDict)
#shaileshFolderID = "1M8jEcrKH-MqDXhGdkFuqapTdFEqaayFX"
#createFolder("shellkore")