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/hotspots.py ADDED
@@ -0,0 +1,143 @@
1
+ """Functions for fetching information about hotspots."""
2
+
3
+ from ebird.api.utils import call
4
+
5
+ from ebird.api.validation import clean_lat, clean_lng, clean_dist, \
6
+ clean_back, clean_region, clean_location
7
+
8
+ REGION_HOTSPOTS_URL = 'https://ebird.org/ws2.0/ref/hotspot/%s'
9
+ NEARBY_HOTSPOTS_URL = 'https://ebird.org/ws2.0/ref/hotspot/geo'
10
+ HOTSPOT_INFO_URL = 'https://ebird.org/ws2.0/ref/hotspot/info/%s'
11
+
12
+
13
+ def get_hotspots(token, region, back=None):
14
+ """List all hotspots within a region.
15
+
16
+ If back is specified then only the hotspots visited in the given number
17
+ of days are returned.
18
+
19
+ Fetch all the hotspots visited in country, subnational1 or subnational2
20
+ area recently (up to 30 days ago).
21
+
22
+ This maps to the end point in the eBird API 2.0,
23
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#f4f59f90-854e-4ba6-8207-323a8cf0bfe0
24
+
25
+ The API supports the option of returning records either in JSON or
26
+ CSV format. Currently results are always returned in JSON format
27
+ since that is consistent with the other functions.
28
+
29
+ :param token: the token needed to access the API.
30
+
31
+ :param region: the code for a country, subnational1 or subnational2 region.
32
+
33
+ :param back: the past number of days to check, default is 14.
34
+
35
+ :return: the list of hotspots.
36
+
37
+ :raises ValueError: if an invalid region code is given or if the value for
38
+ 'back', if given, is not in the range 1..30.
39
+
40
+ :raises URLError if there is an error with the connection to the
41
+ eBird site.
42
+
43
+ :raises HTTPError if the eBird API returns an error.
44
+
45
+ """
46
+ url = REGION_HOTSPOTS_URL % clean_region(region)
47
+
48
+ params = {
49
+ 'fmt': 'json'
50
+ }
51
+
52
+ if back is not None:
53
+ params['back'] = clean_back(back)
54
+
55
+ headers = {
56
+ 'X-eBirdApiToken': token,
57
+ }
58
+
59
+ return call(url, params, headers)
60
+
61
+
62
+ def get_nearby_hotspots(token, lat, lng, dist=25, back=None):
63
+ """Get the list of nearby hotspots.
64
+
65
+ Get the list of hotspots closest to a set of coordinates (latitude,
66
+ longitude) up to a distance of 50km. If back is specified then only
67
+ the hotspots visited in the given number of days are returned.
68
+
69
+ The maps to the end point in the eBird API 2.0,
70
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#674e81c1-6a0c-4836-8a7e-6ea1fe8e6677
71
+
72
+ The API supports the option of returning records either in JSON or
73
+ CSV format. Currently results are always returned in JSON format
74
+ since that is consistent with the other functions.
75
+
76
+ :param token: the token needed to access the API.
77
+
78
+ :param lat: the latitude, which will be rounded to 2 decimal places.
79
+
80
+ :param lng: the longitude, which will be rounded to 2 decimal places.
81
+
82
+ :param dist: include all sites within this distance, from 0 to 50km
83
+ with a default of 25km.
84
+
85
+ :param back: include only visits to the hotspots from 1 to 30 days.
86
+ The default value of None will include all hotspots.
87
+
88
+ :return: the list of hotspots nearest to the given set of coordinates.
89
+
90
+ :raises ValueError: if the coordinates are out of range, if the value
91
+ for 'dist' is not in the range 1..50 or if the value for 'back', if
92
+ specified, is not in the range 1..30.
93
+
94
+ :raises URLError if there is an error with the connection to the
95
+ eBird site.
96
+
97
+ :raises HTTPError if the eBird API returns an error.
98
+
99
+ """
100
+ params = {
101
+ 'lat': clean_lat(lat),
102
+ 'lng': clean_lng(lng),
103
+ 'dist': clean_dist(dist),
104
+ 'fmt': 'json'
105
+ }
106
+
107
+ if back is not None:
108
+ params['back'] = clean_back(back)
109
+
110
+ headers = {
111
+ 'X-eBirdApiToken': token,
112
+ }
113
+
114
+ return call(NEARBY_HOTSPOTS_URL, params, headers)
115
+
116
+
117
+ def get_hotspot(token, loc_id):
118
+ """Get the geographical details of a hotspot.
119
+
120
+ This maps to the end point in the eBird API 2.0,
121
+ https://documenter.getpostman.com/view/664302/S1ENwy59?version=latest#e25218db-566b-4d8b-81ca-e79a8f68c599
122
+
123
+ :param token: the token needed to access the API.
124
+
125
+ :param loc_id: the location code for a hotspot, eg. L374326.
126
+
127
+ :return: the latitude, longitude, name, region, etc. for the hotspot.
128
+
129
+ :raises ValueError: if an invalid location code is given.
130
+
131
+ :raises URLError if there is an error with the connection to the
132
+ eBird site.
133
+
134
+ :raises HTTPError if the eBird API returns an error.
135
+
136
+ """
137
+ url = HOTSPOT_INFO_URL % clean_location(loc_id)
138
+
139
+ headers = {
140
+ 'X-eBirdApiToken': token,
141
+ }
142
+
143
+ return call(url, {}, headers)