-
Notifications
You must be signed in to change notification settings - Fork 0
/
providerGD.py
153 lines (127 loc) · 5.17 KB
/
providerGD.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import apiclient
from execo_engine import logger
import httplib2
from oauth2client.file import Storage
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from apiclient import errors
from provider import Provider
import simplejson
class ProviderGD(Provider):
""" Object to store the provider ids """
def upload_file_sdk(self, service, filePath, fileName, fileType):
"""Upload a file's content.
Args:
service: Drive API service instance.
filePath : Path to the file you want to upload.
fileName : Name of the new file in the drive.
fileType : Type of the file (text, ...)
fileDescription : A short text to describe the file.
Returns:
File uploaded.
"""
media_body = apiclient.http.MediaFileUpload(
filePath,
mimetype=fileType,
resumable=True
)
body = {
'title': fileName,
'description': 'Temporary file',
}
new_file = None
try:
new_file = service.files().insert(body=body, media_body=media_body).execute()
except errors.HttpError as e:
error = simplejson.loads(e.content)
logger.error('Error in Upload ' + error.get('code') + error.get('message'))
return new_file
def download_file_sdk(self, service, drive_file, pathFile):
"""Download a file's content.
Args:
service: Drive API service instance.
drive_file: Drive File instance.
Returns:
File if successful, None otherwise.
"""
if drive_file:
download_url = drive_file.get('downloadUrl')
else:
download_url = self.retrieve_file_metadata(service, pathFile.split('/')[-1])
if download_url:
try:
resp, content = service._http.request(download_url)
if resp.status == 200:
out = open(pathFile, 'wb')
out.write(content)
out.close()
return out
except errors.HttpError as e:
error = simplejson.loads(e.content)
logger.error('Error in Download ' + error.get('code') + error.get('message'))
else:
# The file doesn't have any content stored on Drive.
return None
def retrieve_file_metadata(self, service, fname):
"""Retrieve a list of File resources.
Args:
service: Drive API service instance.
Returns:
List of File resources.
"""
result = []
page_token = None
while True:
try:
param = {'maxResults': 1, 'q': "title = '" + fname + "'"}
if page_token:
param['pageToken'] = page_token
files = service.files().list(**param).execute()
result.extend(files['items'])
page_token = files.get('nextPageToken')
if not page_token:
break
except errors.HttpError, error:
logger.error('An error occurred: ' + error)
break
print result
return result[0]['downloadUrl']
def __init__(self):
"""Init the connexion to Google Drive.
A credentials file is used to store the token and to renew it.
Returns:
The drive service.
"""
Provider.__init__(self, 'googledrive')
def getConnexion(self):
# OAuth 2.0 scope that will be authorized.
OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive'
CRED_FILENAME = 'credentials'
### For storing token
storage = Storage(CRED_FILENAME)
if not storage.get():
# Run through the OAuth flow and retrieve authorization code
flow = OAuth2WebServerFlow(self.app_key, self.app_secret, OAUTH2_SCOPE, 'urn:ietf:wg:oauth:2.0:oob')
authorize_url = flow.step1_get_authorize_url()
print 'Go to the following link in your browser: ' + authorize_url
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
storage.put(credentials)
else:
# Getting access_token,expires_in,token_type,Refresh_toke info from CRED_FILENAME to 'credentials'
credentials = storage.get()
http = httplib2.Http()
credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
return drive_service
def delete_file_sdk(self, service, file_id):
"""Permanently delete a file, skipping the trash.
Args:
service: Drive API service instance.
file_id: ID of the file to delete.
"""
try:
service.files().delete(fileId=file_id).execute()
except errors.HttpError as e:
error = simplejson.loads(e.content)
logger.error('Error in delete ' + error.get('code') + error.get('message'))