ebird-api 3.0.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
ebird/api/__init__.py ADDED
@@ -0,0 +1,51 @@
1
+ # flake8: noqa
2
+
3
+ """A set of wrapper functions for accessing the eBird API."""
4
+
5
+ __version__ = '3.0.6'
6
+
7
+ from ebird.api.constants import (
8
+ LOCALES
9
+ )
10
+
11
+ from ebird.api.observations import (
12
+ get_observations,
13
+ get_notable_observations,
14
+ get_species_observations,
15
+ get_nearby_observations,
16
+ get_nearby_notable,
17
+ get_nearby_species,
18
+ get_nearest_species,
19
+ get_historic_observations,
20
+ )
21
+
22
+ from ebird.api.checklists import (
23
+ get_visits,
24
+ get_checklist,
25
+ )
26
+
27
+ from ebird.api.hotspots import (
28
+ get_hotspots,
29
+ get_nearby_hotspots,
30
+ get_hotspot,
31
+ )
32
+
33
+ from ebird.api.regions import (
34
+ get_regions,
35
+ get_region,
36
+ get_adjacent_regions,
37
+ )
38
+
39
+ from ebird.api.statistics import (
40
+ get_top_100,
41
+ get_totals,
42
+ )
43
+
44
+ from ebird.api.taxonomy import (
45
+ get_taxonomy,
46
+ get_taxonomy_forms,
47
+ get_taxonomy_groups,
48
+ get_taxonomy_versions
49
+ )
50
+
51
+ from ebird.api.client import Client
@@ -0,0 +1,97 @@
1
+ """Functions for fetching checklists and information about visits."""
2
+
3
+ from ebird.api.utils import call
4
+
5
+ from ebird.api.validation import clean_code, clean_max_checklists, clean_area, clean_date
6
+
7
+ CHECKLISTS_DATE_URL = 'https://ebird.org/ws2.0/product/lists/%s/%s'
8
+ CHECKLISTS_RECENT_URL = 'https://ebird.org/ws2.0/product/lists/%s'
9
+ CHECKLIST_URL = 'https://ebird.org/ws2.0/product/checklist/view/%s'
10
+
11
+
12
+ def get_visits(token, area, date=None, max_results=10):
13
+ """
14
+ Get the list of checklists for an area. The most recent checklists are
15
+ returned if a specific date is not given.
16
+
17
+ The maps to the two end points in the eBird API 2.0,
18
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#4416a7cc-623b-4340-ab01-80c599ede73e
19
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#95a206d1-a20d-44e0-8c27-acb09ccbea1a
20
+ which return results in the same format.
21
+
22
+ The eBird API call also has a sortKey parameter which returns records
23
+ ordered by observation date or by creation date. Since checklists are
24
+ often submitted a few days after the actual visit this parameter is
25
+ not currently supported. The results are returned ordered by observation
26
+ date.
27
+
28
+ :param token: the token needed to access the API.
29
+
30
+ :param area: the code for a country, subnational1 region, subnational2
31
+ region or location.
32
+
33
+ :param date: the date, since Jan 1st 1800.
34
+
35
+ :param max_results: the maximum number of checklists to return from
36
+ 1 to 200. The default value is 10.
37
+
38
+ :return: the info for all the checklists submitted.
39
+
40
+ :raises ValueError: if any of the arguments fail the validation checks.
41
+
42
+ :raises URLError if there is an error with the connection to the
43
+ eBird site.
44
+
45
+ :raises HTTPError if the eBird API returns an error.
46
+
47
+ """
48
+ if date is not None:
49
+ url = CHECKLISTS_DATE_URL % (clean_area(area), clean_date(date))
50
+ else:
51
+ url = CHECKLISTS_RECENT_URL % clean_area(area)
52
+
53
+ params = {
54
+ 'maxVisits': clean_max_checklists(max_results),
55
+ 'sortKey': 'obs_dt',
56
+ }
57
+
58
+ headers = {
59
+ 'X-eBirdApiToken': token,
60
+ }
61
+
62
+ return call(url, params, headers)
63
+
64
+
65
+ def get_checklist(token, sub_id):
66
+ """
67
+ Get the contents of a checklist.
68
+
69
+ The information returned include the checklist attributes, date, etc. and the
70
+ list of observations. Only the code for the location and subnational1 are
71
+ included you will need to call get_hotspot_info() to get the full details
72
+ of the location.
73
+
74
+ The maps to the end point in the eBird API 2.0,
75
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#4416a7cc-623b-4340-ab01-80c599ede73e
76
+
77
+ :param token: the token needed to access the API.
78
+
79
+ :param sub_id: the unique identifier for the checklist, e.g. S22893621.
80
+
81
+ :return: the details of the checklist, including the list of observations
82
+
83
+ :raises ValueError: if any of the arguments fail the validation checks.
84
+
85
+ :raises URLError if there is an error with the connection to the
86
+ eBird site.
87
+
88
+ :raises HTTPError if the eBird API returns an error.
89
+
90
+ """
91
+ url = CHECKLIST_URL % clean_code(sub_id)
92
+
93
+ headers = {
94
+ 'X-eBirdApiToken': token,
95
+ }
96
+
97
+ return call(url, {}, headers)
ebird/api/client.py ADDED
@@ -0,0 +1,338 @@
1
+ # pylint: disable=R0902,R0904
2
+
3
+ """Classes for simplifying calls to the eBird API."""
4
+
5
+ from ebird.api import observations, checklists, regions, hotspots, \
6
+ taxonomy, statistics
7
+
8
+ from ebird.api.validation import clean_locale
9
+
10
+
11
+ class Client:
12
+ """Client class to simplify interacting with the API calls.
13
+
14
+ The arguments used to initialize the client are generally API parameters
15
+ which stay relatively fixed for a given application, e.g. including records
16
+ which have not been reviewed (provisional = True) or to include records from
17
+ private and hotspot locations (hotspots = False).
18
+
19
+ All methods raise ValueError if any of the arguments or default values
20
+ cannot be validated; URLError if there is an error with the connection
21
+ to the eBird site or HTTPError if the eBird API returns an error.
22
+
23
+ """
24
+
25
+ def __init__(self, api_key, locale):
26
+ self.api_key = api_key
27
+ self.locale = clean_locale(locale)
28
+ self.max_observations = None
29
+ self.max_visits = 200
30
+ self.max_observers = 100
31
+ self.back = 14
32
+ self.category = None
33
+ self.detail = 'full'
34
+ self.dist = 25
35
+ self.hotspot = False
36
+ self.provisional = True
37
+ self.sort = 'date'
38
+
39
+ def get_observations(self, area):
40
+ """Get recent observations (up to 30 days ago) for a region or location.
41
+
42
+ :param area: a country, subnational1, subnational2 or location code
43
+ or a list of up to 10 codes. All codes must be same type.
44
+
45
+ :return: the list of observations in simple format.
46
+
47
+ """
48
+ return observations.get_observations(
49
+ self.api_key, area,
50
+ back=self.back, max_results=self.max_observations, locale=self.locale,
51
+ provisional=self.provisional, hotspot=self.hotspot, detail=self.detail,
52
+ category=self.category)
53
+
54
+ def get_notable_observations(self, area):
55
+ """Get recent observations of a rare species for a region or location
56
+
57
+ :param area: a country, subnational1, subnational2 or location code
58
+ or a list of up to 10 codes. All codes must be same type.
59
+
60
+ :return: the list of observations.
61
+
62
+ """
63
+ return observations.get_notable_observations(
64
+ self.api_key, area,
65
+ back=self.back, max_results=self.max_observations, locale=self.locale,
66
+ hotspot=self.hotspot, detail=self.detail)
67
+
68
+ def get_species_observations(self, species, area):
69
+ """Get recent observations for a given species in a region.
70
+
71
+ :param species: the scientific name of the species.
72
+
73
+ :param area: a country, subnational1, subnational2 or location code
74
+ or a list of up to 10 codes. All codes must be same type.
75
+
76
+ :return: the list of observations in simple format.
77
+
78
+ """
79
+ return observations.get_species_observations(
80
+ self.api_key, species, area,
81
+ back=self.back, max_results=self.max_observations, locale=self.locale,
82
+ provisional=self.provisional, hotspot=self.hotspot, detail=self.detail,
83
+ category=self.category)
84
+
85
+ def get_historic_observations(self, area, date):
86
+ """Get recent observations for a region.
87
+
88
+ :param area: a country, subnational1, subnational2 or location code
89
+ or a list of up to 10 codes. All codes must be same type.
90
+
91
+ :param date: the date, since Jan 1st 1800.
92
+
93
+ :return: the list of observations in simple format.
94
+
95
+ """
96
+ return observations.get_historic_observations(
97
+ self.api_key, area, date,
98
+ max_results=self.max_observations, locale=self.locale,
99
+ provisional=self.provisional, hotspot=self.hotspot, detail=self.detail,
100
+ category=self.category)
101
+
102
+ def get_nearby_observations(self, lat, lng, dist=25):
103
+ """Get nearby recent observations of each species.
104
+
105
+ :param lat: the latitude, which will be rounded to 2 decimal places.
106
+
107
+ :param lng: the longitude, which will be rounded to 2 decimal places.
108
+
109
+ :param dist: include all sites within this distance, from 0 to 50km
110
+ with a default of 25km.
111
+
112
+ :return: the list of observations in simple format.
113
+
114
+ """
115
+ return observations.get_nearby_observations(
116
+ self.api_key, lat, lng, dist=dist,
117
+ back=self.back, max_results=self.max_observations, locale=self.locale,
118
+ provisional=self.provisional, hotspot=self.hotspot,
119
+ category=self.category)
120
+
121
+ def get_nearby_notable(self, lat, lng, dist=25):
122
+ """Get the nearby, recent observations of rare species.
123
+
124
+ :param lat: the latitude, which will be rounded to 2 decimal places.
125
+
126
+ :param lng: the longitude, which will be rounded to 2 decimal places.
127
+
128
+ :param dist: include all sites within this distance, from 0 to 50km
129
+ with a default of 25km.
130
+
131
+ :return: the list of observations.
132
+
133
+ """
134
+ return observations.get_nearby_notable(
135
+ self.api_key, lat, lng, dist=dist,
136
+ back=self.back, max_results=self.max_observations, locale=self.locale,
137
+ hotspot=self.hotspot, detail=self.detail)
138
+
139
+ def get_nearby_species(self, species, lat, lng, dist=25):
140
+ """Get most recent observation of a species nearby.
141
+
142
+ :param species: the scientific name of the species.
143
+
144
+ :param lat: the latitude, which will be rounded to 2 decimal places.
145
+
146
+ :param lng: the longitude, which will be rounded to 2 decimal places.
147
+
148
+ :param dist: include all sites within this distance, from 0 to 50km
149
+ with a default of 25km.
150
+
151
+ :return: the list of observations in 'simple' form. See the eBird API
152
+ documentation for a description of the fields.
153
+
154
+ """
155
+ return observations.get_nearby_species(
156
+ self.api_key, species, lat, lng, dist=dist,
157
+ back=self.back, max_results=self.max_observations, locale=self.locale,
158
+ provisional=self.provisional, hotspot=self.hotspot,
159
+ category=self.category)
160
+
161
+ def get_nearest_species(self, species, lat, lng, dist=25):
162
+ """Get most recent observation of a species nearby.
163
+
164
+ :param species: the scientific name of the species.
165
+
166
+ :param lat: the latitude, which will be rounded to 2 decimal places.
167
+
168
+ :param lng: the longitude, which will be rounded to 2 decimal places.
169
+
170
+ :param dist: include all sites within this distance, from 0 to 50km
171
+ with a default of 25km.
172
+
173
+ :return: the list of observations in 'simple' form.
174
+
175
+ """
176
+ return observations.get_nearest_species(
177
+ self.api_key, species, lat, lng, dist=dist,
178
+ back=self.back, max_results=self.max_observations, locale=self.locale,
179
+ provisional=self.provisional, hotspot=self.hotspot)
180
+
181
+ def get_hotspots(self, region, back=14):
182
+ """List all hotspots within a region.
183
+
184
+ :param region: the code for a country, subnational1 or subnational2 region.
185
+
186
+ :param back: the past number of days to check, default is 14.
187
+
188
+ :return: the list of hotspots.
189
+
190
+ """
191
+ return hotspots.get_hotspots(self.api_key, region, back)
192
+
193
+ def get_nearby_hotspots(self, lat, lng, dist=25):
194
+ """Get the list of nearby hotspots.
195
+
196
+ :param lat: the latitude, which will be rounded to 2 decimal places.
197
+
198
+ :param lng: the longitude, which will be rounded to 2 decimal places.
199
+
200
+ :param dist: include all sites within this distance, from 0 to 50km
201
+ with a default of 25km.
202
+
203
+ :return: the list of hotspots nearest to the given set of coordinates.
204
+
205
+ """
206
+ return hotspots.get_nearby_hotspots(self.api_key, lat, lng, dist, back=self.back)
207
+
208
+ def get_hotspot(self, loc_id):
209
+ """Get the geographical details of a hotspot.
210
+
211
+ :param loc_id: the location code for a hotspot, eg. L374326.
212
+
213
+ :return: the latitude, longitude, name, region, etc. for the hotspot.
214
+
215
+ """
216
+ return hotspots.get_hotspot(self.api_key, loc_id)
217
+
218
+ def get_regions(self, rtype, region):
219
+ """Get the list of sub-regions or a given region.
220
+
221
+ :param rtype: the region type, either 'country', 'subnational1' or 'subnational2'.
222
+
223
+ :param region: the name of the region, either 'world', a country or a subnational1 code.
224
+
225
+ :return: the list of sub-regions within the given region.
226
+
227
+ """
228
+ return regions.get_regions(self.api_key, rtype, region)
229
+
230
+ def get_adjacent_regions(self, region):
231
+ """Get the regions adjacent to a given region.
232
+
233
+ :param region: the name of the region, either a country, a subnational1
234
+ or a subnational2 code.
235
+
236
+ :return: the list of regions bordering the specified region.
237
+
238
+ """
239
+ return regions.get_adjacent_regions(self.api_key, region)
240
+
241
+ def get_region(self, region):
242
+ """Get the geographical details of a country, region or sub-region.
243
+
244
+ :param region: the code for the region, eg. US-NV.
245
+
246
+ :return: the latitude, longitude, name, region, etc. for the area.
247
+
248
+ """
249
+ return regions.get_region(self.api_key, region)
250
+
251
+ def get_visits(self, area, date=None):
252
+ """
253
+ Get the list of checklists for an area. The most recent checklists are
254
+ returned if a specific date is not given.
255
+
256
+ :param area: the code for a country, subnational1 region, subnational2
257
+ region or location.
258
+
259
+ :param date: the date, since Jan 1st 1800.
260
+
261
+ :return: the info for all the checklists submitted.
262
+
263
+ """
264
+ return checklists.get_visits(self.api_key, area, date, max_results=self.max_visits)
265
+
266
+ def get_checklist(self, sub_id):
267
+ """
268
+ Get the contents of a checklist.
269
+
270
+ :param sub_id: the unique identifier for the checklist, e.g. S22893621.
271
+
272
+ :return: the details of the checklist, including the list of observations
273
+
274
+ """
275
+ return checklists.get_checklist(self.api_key, sub_id)
276
+
277
+ def get_top_100(self, region, date, rank='spp'):
278
+ """
279
+ Get the observers who have seen the most species or submitted the
280
+ greatest number of checklists on a given date.
281
+
282
+ :param region: the code for the region, eg. US-NV.
283
+
284
+ :param date: the date, since Jan 1st 1800.
285
+
286
+ :param rank: rank the observers by species seen (spp) or number of
287
+ checklists submitted (cl).
288
+
289
+ :return: the list of observers.
290
+
291
+ """
292
+ return statistics.get_top_100(
293
+ self.api_key, region, date, rank=rank, max_results=self.max_observers)
294
+
295
+ def get_totals(self, area, date):
296
+ """
297
+ Get the number of contributors, checklists submitted and species seen on a given date.
298
+
299
+ :param area: the code for a country subnational1 , subnational2 region or location
300
+
301
+ :param date: the date, since Jan 1st 1800.
302
+
303
+ :return: the totals for the given date
304
+
305
+ """
306
+ return statistics.get_totals(self.api_key, area, date)
307
+
308
+ def get_taxonomy(self):
309
+ """Get the full or specific subset of the taxonomy used by eBird.
310
+
311
+ :return: the complete list of species used by eBird.
312
+
313
+ """
314
+ return taxonomy.get_taxonomy(self.api_key, self.category, locale=self.locale)
315
+
316
+ def get_taxonomy_forms(self, code):
317
+ """Get all the sub-specific forms of a given species.
318
+
319
+ :return: the list of codes of each sub-species.
320
+
321
+ """
322
+ return taxonomy.get_taxonomy_forms(self.api_key, code)
323
+
324
+ def get_taxonomy_groups(self):
325
+ """Get the names of the groups of species used in the taxonomy.
326
+
327
+ :return: the list of species groups.
328
+
329
+ """
330
+ return taxonomy.get_taxonomy_groups(self.api_key, locale=self.locale)
331
+
332
+ def get_taxonomy_versions(self):
333
+ """Get all versions of the taxonomy, indicating which is the latest.
334
+
335
+ :return: a list of all the taxonomy versions used by eBird.
336
+
337
+ """
338
+ return taxonomy.get_taxonomy_versions(self.api_key)
ebird/api/constants.py ADDED
@@ -0,0 +1,93 @@
1
+ """Various constants and values used in the API."""
2
+
3
+ DEFAULT_BACK = 14
4
+ DEFAULT_DETAIL = 'simple'
5
+ DEFAULT_DISTANCE = 25
6
+ DEFAULT_HOTSPOTS_ONLY = 'false'
7
+ DEFAULT_PROVISIONAL = 'false'
8
+ DEFAULT_LOCALE = 'en'
9
+ DEFAULT_OBSERVATION_ORDER = 'date'
10
+ DEFAULT_SPECIES_CATEGORY = None
11
+ DEFAULT_TAXONOMY_ORDER = 'ebird'
12
+ DEFAULT_MAX_OBSERVATIONS = None
13
+ DEFAULT_MAX_OBSERVERS = 100
14
+ DEFAULT_MAX_CHECKLISTS = 10
15
+ DEFAULT_TOP_100_RANK = 'spp'
16
+
17
+ LOCALES = {
18
+ 'Bulgarian': 'bg',
19
+ 'Chinese': 'zh',
20
+ 'Chinese (Simple)': 'zh_SIM',
21
+ 'Croatian': 'hr',
22
+ 'Czech': 'cs',
23
+ 'Dutch': 'nl',
24
+ 'Danish': 'da',
25
+ 'English': 'en',
26
+ 'English (Australia)': 'en_AU',
27
+ 'English (India)': 'en_IN',
28
+ 'English (IOC)': 'en_IOC',
29
+ 'English (Hawaii)': 'en_HAW',
30
+ 'English (Kenya)': 'en_KE',
31
+ 'English (Malaysia)': 'en_MY',
32
+ 'English (New Zealand)': 'en_NZ',
33
+ 'English (Philippines)': 'en_PH',
34
+ 'English (South Africa)': 'en_ZA',
35
+ 'English (United Arab Emirates)': 'en_AE',
36
+ 'English (Great Britain)': 'en_UK',
37
+ 'English (United States)': 'en_US',
38
+ 'Faroese': 'fo',
39
+ 'Finnish': 'fi',
40
+ 'French': 'fr',
41
+ 'French (AOU)': 'fr_AOU',
42
+ 'French (Canada)': 'fr_CA',
43
+ 'German': 'de',
44
+ 'French (Guadeloupe)': 'fr_GP',
45
+ 'French (Haiti)': 'fr_HT',
46
+ 'Haitian': 'ht_HT',
47
+ 'Hebrew': 'iw',
48
+ 'Hungarian': 'hu',
49
+ 'Indonesian': 'id',
50
+ 'Icelandic': 'is',
51
+ 'Italian': 'it',
52
+ 'Japanese': 'ja',
53
+ 'Korean': 'ko',
54
+ 'Latvian': 'lv',
55
+ 'Lithuanian': 'lt',
56
+ 'Malayalam': 'ml',
57
+ 'Mongolian': 'mn',
58
+ 'Norwegian': 'no',
59
+ 'Polish': 'pl',
60
+ 'Portuguese (Portugal)': 'pt_PT',
61
+ 'Portuguese (Brasil)': 'pt_BR',
62
+ 'Russian': 'ru',
63
+ 'Serbian': 'sr',
64
+ 'Slovenian': 'sl',
65
+ 'Spanish': 'es',
66
+ 'Spanish (Argentina)': 'es_AR',
67
+ 'Spanish (Chile)': 'es_CL',
68
+ 'Spanish (Costa Rica)': 'es_CR',
69
+ 'Spanish (Cuba)': 'es_CU',
70
+ 'Spanish (Dominican Republic)': 'es_DO',
71
+ 'Spanish (Ecuador)': 'es_EC',
72
+ 'Spanish (Spain)': 'es_ES',
73
+ 'Spanish (Mexico)': 'es_MX',
74
+ 'Spanish (Panama)': 'es_PA',
75
+ 'Spanish (Puerto Rico)': 'es_PR',
76
+ 'Spanish (Uruguay)': 'es_UY',
77
+ 'Spanish (Venezuela)': 'es_VE',
78
+ 'Swedish': 'sv',
79
+ 'Thai': 'th',
80
+ 'Turkish': 'tr',
81
+ 'Ukrainian': 'uk',
82
+ }
83
+
84
+ SPECIES_CATEGORIES = [
85
+ 'domestic', 'form', 'hybrid', 'intergrade', 'issf', 'slash', 'species', 'spuh']
86
+
87
+ SPECIES_ORDERING = ['ebird', 'merlin']
88
+
89
+ SPECIES_SORT = ['date', 'species']
90
+
91
+ REGION_TYPES = ['country', 'subnational1', 'subnational2']
92
+
93
+ TOP_100_RANK = ['spp', 'cl']