-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.py
140 lines (112 loc) · 4.44 KB
/
api.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
# -*- coding: utf-8 -*-
'''
OPUS API class
'''
import requests
from .url import clean
from .data import Data
from .metadata import Metadata
from .images import Images, Image
from .files import Files, File
from .mults import Mults
from .range import Range
from .fields import Fields, Field
from .categories import Categories, Category
API_URL = 'https://tools.pds-rings.seti.org/opus/api'
FMT = ['json', 'html', 'zip', 'csv']
class API(object):
'''OPUS Seti Ring-Node API class'''
def __init__(self, url=API_URL, verbose=False):
self.url = clean(url)
self.verbose = verbose
def __str__(self):
return self.url
def __repr__(self):
return "OPUS Seti Ring-Node API: {}".format(self.url)
def request(self, entry, fmt='json', **kwargs):
if fmt not in FMT:
raise ValueError(
"Format '{}' not in {}".format(fmt, FMT)
)
params = ''
if len(kwargs) != 0:
params = '?' + '&'.join(
'{}={}'.format(key, value)
for key, value in kwargs.items()
)
return self.url + entry + '.' + fmt + params
def load(self, entry, **kwargs):
url = self.request(entry, fmt='json', **kwargs)
if self.verbose:
print('Call to: {}'.format(url))
response = requests.get(url)
if response.ok:
return response.json()
else:
raise RuntimeError('The request at {} failed'.format(url))
def count(self, **kwargs):
'''Get result count for a search'''
res = self.load('meta/result_count', **kwargs)
return int(res['data'][0]['result_count'])
def data(self, limit=100, page=1, **kwargs):
'''Get data for a search'''
if limit is None:
kwargs['limit'] = self.count(**kwargs)
else:
kwargs['limit'] = limit
kwargs['page'] = page
return Data(self.load('data', **kwargs))
def metadata(self, opus_id):
'''Get detail for a single observation'''
return Metadata(self.load('metadata_v2/'+opus_id))
def images(self, size='med', limit=100, page=1, **kwargs):
'''Get image results for a search'''
size = size.lower()
if size not in ['thumb', 'small', 'med', 'full']:
raise ValueError(
'Image size {} unknown (available: [thumb,small,med,full])'.format(size))
if limit is None:
kwargs['limit'] = self.count(**kwargs)
else:
kwargs['limit'] = limit
kwargs['page'] = page
return Images(self.load('images/'+size, **kwargs), size)
def image(self, opus_id, size='med'):
'''Get images for a single observation'''
size = size.lower()
if size not in ['thumb', 'small', 'med', 'full']:
raise ValueError(
'Image size {} unknown (available: [thumb,small,med,full])'.format(size))
json = self.load('image/'+size+'/'+opus_id)
return Image(opus_id, json['path'], json['data'][0]['img'])
def file(self, opus_id):
'''Get files for a single observation'''
json = self.load('files/'+opus_id)
return File(opus_id, json['data'][opus_id])
def files(self, limit=100, page=1, **kwargs):
'''Get all files results for a search'''
if limit is None:
kwargs['limit'] = self.count(**kwargs)
else:
kwargs['limit'] = limit
kwargs['page'] = page
return Files(self.load('files', **kwargs))
def mults(self, param='target', **kwargs):
'''Returns all possible values for a given multiple choice
field, given a search, and the result count for each value'''
return Mults(self.load('meta/mults/'+param, **kwargs))
def range(self, param='RINGGEOringradius1', **kwargs):
'''Get range endpoints for a field, given a search'''
return Range(param, self.load('meta/range/endpoints/'+param, **kwargs))
def field(self, field):
'''Get information about a particular field'''
return Field(field, self.load('fields/'+field)['data'][field])
def fields(self):
'''Get list of all fields'''
return Fields(self.load('fields')['data'])
def category(self, opus_id):
'''Get all fields in a category'''
return Categories(self.load('categories/'+opus_id))
def categories(self):
'''List category names'''
return Categories(self.load('categories'))