-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathapi.py
More file actions
276 lines (249 loc) · 11.6 KB
/
api.py
File metadata and controls
276 lines (249 loc) · 11.6 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import logging
import requests
import json
import random
import time
import os.path as osp
from pathlib import Path
from getpass import getpass
from filters import Filter
from downloader import download_scenes
M2M_ENDPOINT = 'https://m2m.cr.usgs.gov/api/api/json/{}/'
logging.getLogger('requests').setLevel(logging.WARNING)
class M2MError(Exception):
"""
Raised when an M2M gets an error.
"""
pass
class M2M(object):
"""M2M EarthExplorer API."""
def __init__(self, username=None, password=None, token=None, version="stable"):
self.serviceUrl = M2M_ENDPOINT.format(version)
self.apiKey = None
self.authenticate(username, password, token)
allDatasets = self.sendRequest('dataset-search')
self.datasetNames = [dataset['datasetAlias'] for dataset in allDatasets]
self.permissions = self.sendRequest('permissions')
def authenticate(self, username, password, token):
config_path = '~/.config/m2m_api'
config_path = Path(osp.expandvars(config_path)).expanduser().resolve()
config_file = config_path / 'config.json'
try:
config = json.load(open(config_file))
except:
config_path.mkdir(parents=True, exist_ok=True)
config = {}
self.username = username
if self.username is None:
self.username = config.get('username')
if self.username is None:
username = input("Enter your username (or email): ")
self.username = username
config['username'] = username
if password != None:
self.login(password)
elif token != None:
config = {
'username': username,
'token': token
}
json.dump(config, open(config_file, 'w'), indent=4, separators=(',', ': '))
self.loginToken(token)
else:
token = config.get('token')
if token is None:
option = None
while option not in ["p", "P", "t", "T"]:
option = input("Want to use password (p) or token (t)? ")
if option in ["p", "P"]:
password = getpass()
self.login(password)
else:
token = input('Enter your token: ')
config = {
'username': username,
'token': token
}
json.dump(config, open(config_file, 'w'), indent=4, separators=(',', ': '))
self.loginToken(token)
else:
self.loginToken(token)
def sendRequest(self, endpoint, data={}, max_retries=5):
url = osp.join(self.serviceUrl, endpoint)
logging.info('sendRequest - url = {}'.format(url))
json_data = json.dumps(data)
if self.apiKey == None:
response = retry_connect(url, json_data, max_retries=max_retries)
else:
headers = {'X-Auth-Token': self.apiKey}
response = retry_connect(url, json_data, headers=headers, max_retries=max_retries)
if response == None:
raise M2MError("No output from service")
status = response.status_code
try:
output = json.loads(response.text)
except:
output = response.text
if status != 200:
if isinstance(output,dict):
msg = "{} - {} - {}".format(status,output['errorCode'],output['errorMessage'])
else:
msg = "{} - {}".format(status,output)
raise M2MError(msg)
else:
if isinstance(output,dict):
if output['data'] is None and output['errorCode'] is not None and endpoint != 'logout':
msg = "{} - {}".format(output['errorCode'],output['errorMessage'])
raise M2MError(msg)
else:
msg = "{} - {}".format(status,output)
raise M2MError(msg)
response.close()
return output['data']
def login(self, password=None):
if password is None:
raise M2MError('password not provided')
loginParameters = {'username': self.username, 'password': password}
self.apiKey = self.sendRequest('login', loginParameters)
def loginToken(self, token=None):
if token is None:
raise M2MError('token not provided')
loginParameters = {'username': self.username, 'token': token}
self.apiKey = self.sendRequest('login-token', loginParameters)
def searchDatasets(self, **args):
args['processList'] = ['datasetName','acquisitionFilter','spatialFilter']
params = Filter(args)
return self.sendRequest('dataset-search', params)
def datasetFilters(self, **args):
args['processList'] = ['datasetName']
params = Filter(args)
return self.sendRequest('dataset-filters', params)
def searchScenes(self, datasetName, **args):
if datasetName not in self.datasetNames:
raise M2MError("Dataset {} not one of the available datasets {}".format(datasetName,self.datasetNames))
args['datasetName'] = datasetName
if 'metadataInfo' in args and len(args['metadataInfo']):
args['datasetFilters'] = self.datasetFilters(**args)
args['processList'] = ['datasetName','sceneFilter','maxResults']
params = Filter(args)
scenes = self.sendRequest('scene-search', params)
if scenes['totalHits'] > scenes['recordsReturned']:
logging.warning('M2M.searchScenes - more hits {} than returned records {}, consider increasing maxResults parameter.'.format(scenes['totalHits'],
scenes['recordsReturned']))
return scenes
def sceneListAdd(self, listId, datasetName, **args):
args['listId'] = listId
if datasetName not in self.datasetNames:
raise M2MError("Dataset {} not one of the available datasets {}".format(datasetName,self.datasetNames))
args['datasetName'] = datasetName
self.sendRequest('scene-list-add', args)
def sceneListGet(self, listId, **args):
args['listId'] = listId
self.sendRequest('scene-list-get', args)
def sceneListRemove(self, listId, **args):
args['listId'] = listId
self.sendRequest('scene-list-remove', args)
def downloadOptions(self, datasetName, filterOptions={}, **args):
if datasetName not in self.datasetNames:
raise M2MError("Dataset {} not one of the available datasets {}".format(datasetName,self.datasetNames))
args['datasetName'] = datasetName
downloadOptions = self.sendRequest('download-options', args)
filteredOptions = apply_filter(downloadOptions, filterOptions)
return filteredOptions
def downloadRequest(self, downloadList, label='m2m-api_download'):
params = {'downloads': downloadList,
'label': label}
return self.sendRequest('download-request', params)
def downloadRetrieve(self, label='m2m-api_download'):
params = {'label': label}
return self.sendRequest('download-retrieve', params)
def downloadSearch(self, label=None):
if label is not None:
params = {'label': label}
return self.sendRequest('download-search', params)
return self.sendRequest('download-search')
def downloadOrderRemove(self, label):
params = {'label': label}
self.sendRequest('download-order-remove', params)
def retrieveScenes(self, datasetName, scenes, filterOptions={}, label='m2m-api_download'):
entityIds = [scene['entityId'] for scene in scenes['results']]
self.sceneListAdd(label, datasetName, entityIds=entityIds)
downloadMeta = {}
if not len(filterOptions):
filterOptions = {'downloadSystem': lambda x: x in ['dds', 'ls_zip'], 'available': lambda x: x}
labels = [label]
downloadOptions = self.downloadOptions(
datasetName, filterOptions, listId=label, includeSecondaryFileGroups=False
)
downloads = [
{
'entityId' : product['entityId'], 'productId' : product['id']
} for product in downloadOptions
]
requestedDownloadsCount = len(downloads)
if requestedDownloadsCount:
logging.info('M2M.retrieveScenes - Requested downloads count={}'.format(requestedDownloadsCount))
requestResults = self.downloadRequest(downloads, label=label)
if len(requestResults['duplicateProducts']):
for product in requestResults['duplicateProducts'].values():
if product not in labels:
labels.append(product)
for label in labels:
downloadSearch = self.downloadSearch(label)
if downloadSearch is not None:
for ds in downloadSearch:
downloadMeta.update({str(ds['downloadId']): ds})
if requestResults['preparingDownloads'] != None and len(requestResults['preparingDownloads']) > 0:
downloadIds = []
for label in labels:
requestResultsUpdated = self.downloadRetrieve(label)
downloadUpdate = requestResultsUpdated['available'] + requestResultsUpdated['requested']
download_scenes(downloadUpdate, downloadMeta)
downloadIds += downloadMeta
while len(downloadIds) < requestedDownloadsCount:
preparingDownloads = requestedDownloadsCount - len(downloadIds)
logging.info('M2M.retrieveScenes - {} downloads are not available. Waiting 10 seconds...'.format(preparingDownloads))
time.sleep(10)
for label in labels:
requestResultsUpdated = self.downloadRetrieve(label)
downloadUpdate = requestResultsUpdated['available']
download_scenes(downloadUpdate, downloadMeta)
downloadIds += downloadUpdate
else:
download_scenes(requestResults['availableDownloads'], downloadMeta)
else:
logging.info('M2M.retrieveScenes - No download options found')
for label in labels:
self.downloadOrderRemove(label)
self.sceneListRemove(label)
return downloadMeta
def logout(self):
r = self.sendRequest('logout')
if r != None:
raise M2MError("Not able to logout")
self.apiKey = None
def __exit__(self):
self.logout()
def retry_connect(url, json_data, headers={}, max_retries=5, sleep_seconds=2, timeout=600):
retries = 0
while retries < max_retries:
try:
response = requests.post(url, json_data, headers=headers, timeout=timeout)
return response
except requests.exceptions.Timeout:
retries += 1
logging.info('Connection Timeout - retry number {} of {}'.format(retries,max_retries))
sec = random.random() * sleep_seconds + 100.
time.sleep(sec)
raise M2MError("Maximum retries exceeded")
def apply_filter(elements, key_filters):
result = []
if elements != None:
for element in elements:
get_elem = True
for key,filt in key_filters.items():
if not filt(element[key]):
get_elem = False
if get_elem:
result.append(element)
return result