-
Notifications
You must be signed in to change notification settings - Fork 0
/
ebird.py
269 lines (240 loc) · 10.3 KB
/
ebird.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
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
import requests
import json
import time
import threading
import multiprocessing
# url = "https://api.ebird.org/v2/data/obs/CN-11/recent"
# url = "https://api.ebird.org/v2/data/obs/geo/recent?lat=39.85933&lng=116.68708&sort=species&dist=50&back=3"
# url = "https://api.ebird.org/v2/product/spplist/CN-11"
class ebird:
def __init__(self, token, locale='zh_SIM'):
self.token = token
self.locale = locale
spp_dict = {}
spp_trans = {}
with open("./ebird-CN.json", "r", encoding="utf-8") as f:
spp_db = json.loads(f.read())
for _spp in spp_db:
# 消除 例如 灰喜鹊、灰喜鹊(东亚)这样的重复
if 'reportAs' in _spp:
spp_dict[_spp['speciesCode']] = spp_dict[_spp['reportAs']]
else:
spp_dict[_spp['speciesCode']] = (_spp['sciName'], _spp['comName'])
spp_trans[_spp['sciName']] = (_spp['comName'], _spp['speciesCode'])
def update_spp(self, speciesCode):
if speciesCode not in self.spp_dict:
spp = self.query_species(speciesCode)
self.spp_db.append(spp[0])
f = open('./ebird-CN.json','w+')
ll = json.dumps(self.spp_db,sort_keys=True, indent=4, separators=(',', ': '))
f.write(ll)
f.close()
self.spp_dict[speciesCode] = (spp[0]['sciName'], spp[0]['comName'])
self.spp_trans[spp[0]['sciName']] = (spp[0]['comName'], spp[0]['speciesCode'])
def query_species(self, specie):
url = f"https://api.ebird.org/v2/ref/taxonomy/ebird?species={specie}&locale=zh_SIM&fmt=json"
payload={}
headers = {
'X-eBirdApiToken': self.token
}
response = requests.request("GET", url, headers=headers, data=payload)
res = response.text
return json.loads(res)
def get_sciName_from_speciesCode(self, speciesCode):
self.update_spp(speciesCode)
return self.spp_dict[speciesCode][0]
def get_comName_from_speciesCode(self, speciesCode):
self.update_spp(speciesCode)
return self.spp_dict[speciesCode][1]
def get_comName_from_sciName(self, sciName):
return self.spp_trans[sciName][0]
def get_speciesCode_from_sciName(self, sciName):
return self.spp_trans[sciName][1]
def get_recent_obs(self, regionCode='CN-11',back=7):
url = f"https://api.ebird.org/v2/data/obs/{regionCode}/recent?back={back}&sppLocale={self.locale}"
payload={}
headers = {
'X-eBirdApiToken': self.token
}
response = requests.request("GET", url, headers=headers, data=payload)
res = response.text
return json.loads(res)
def get_historic_obs(self, regionCode='CN-11', date=''):
url = f"https://api.ebird.org/v2/data/obs/{regionCode}/historic/{date}?sppLocale={self.locale}"
payload={}
headers = {
'X-eBirdApiToken': self.token
}
response = requests.request("GET", url, headers=headers, data=payload)
res = response.text
return json.loads(res)
def get_historic_list(self, regionCode='CN-11', date=''):
url = f"https://api.ebird.org/v2/product/lists/{regionCode}/{date}"
payload={}
headers = {
'X-eBirdApiToken': self.token
}
response = requests.request("GET", url, headers=headers, data=payload)
res = response.text
return json.loads(res)
def get_report_detail(self, subId):
url = f"https://api.ebird.org/v2/product/checklist/view/{subId}"
payload={}
headers = {
'X-eBirdApiToken': self.token
}
response = requests.request("GET", url, headers=headers, data=payload)
res = response.text
return json.loads(res)
# detail = get_report_detail(token=token, subId='S148184150')
# taxons = detail['obs']
# for taxon in taxons:
# print(detail["subId"],
# detail["locId"],
# taxon["speciesCode"],
# taxon["howManyStr"])
# res = get_historic_obs(date='2023/8/27')
# # res = get_recent_obs()
# # # print(json.dumps(res,sort_keys=True, indent=4, separators=(',', ': ')))
# for _list in res:
# try:
# if "howMany" in _list:
# howMany = _list["howMany"]
# else:
# howMany = 'X'
# # print(_list)
# print(_list["subId"],
# _list["speciesCode"],
# _list["comName"],
# howMany,
# _list["lat"],
# _list["lng"],
# _list["locName"]
# )
# except Exception as e:
# print(f"{_list} error")
# print(e)
# def get_all_report_url_list():
# pass
# res = get_recent_obs()
# print(json.dumps(res,sort_keys=True, indent=4, separators=(',', ': ')))
# print(json.dumps(res,sort_keys=True, indent=4, separators=(',', ': ')))
def get_back_date(self, n):
t = int(time.time()) - n*60*60*24
ta = time.localtime(t)
return time.strftime("%Y/%m/%d", ta)
def date_to_ta(self, date):
return int(time.mktime(time.strptime(date,"%Y/%m/%d")))
def ta_to_date(self, ta):
return time.strftime("%Y/%m/%d", time.localtime(ta))
def search(self, taxonid='', startTime='', endTime='', province='', city='', district='', pointname='', username='', serial_id='', ctime='', taxonname='', state=''):
id_list = []
id_detail = {}
ta_s = self.date_to_ta(startTime)
ta_e = self.date_to_ta(endTime)
# print(ta_s, ta_e)
back = int((ta_e - ta_s) / 86400)
# print(back)
date_list = [self.ta_to_date(ta_s+86400*i) for i in range(back+1)]
print('查询日期\n',date_list)
print('正在获取日期范围内的checklist id...')
lock1 = threading.Lock()
def loop1():
while len(date_list):
lock1.acquire()
_date = date_list.pop()
lock1.release()
res = self.get_historic_list(date=_date)
# print(json.dumps(res,sort_keys=True, indent=4, separators=(',', ': ')))
print('thread %s >>> %s' % (threading.current_thread().name, _date))
for item in res:
try:
id_detail[item["subId"]] = item
# {
# 'lat':item["loc"]["lat"],
# 'lng':item["loc"]["lng"],
# 'locName':item["loc"]["locName"],
# 'userDisplayName':item["userDisplayName"]
# }
id_list.append(item["subId"])
except Exception as e:
print(f"{item} error")
print(e)
t1 = []
for i in range(multiprocessing.cpu_count()):
t1.append(threading.Thread(target=loop1))
t1[i].start()
for i in range(multiprocessing.cpu_count()):
t1[i].join()
print(id_list)
checklists = []
print(f'共计{len(id_list)}份报告')
lock2 = threading.Lock()
def loop2():
while len(id_list):
lock2.acquire()
_id = id_list.pop()
lock2.release()
# for _id in id_list:
detail = self.get_report_detail(subId=_id)
# detail["lat"] = id_detail[_id]["lat"]
# detail["lng"] = id_detail[_id]["lng"]
# detail["locName"] = id_detail[_id]["locName"]
# detail["userDisplayName"] = id_detail[_id]["userDisplayName"]
print('thread %s >>> %s' % (threading.current_thread().name, _id))
for _ in id_detail[_id]:
if _ not in detail:
detail[_] = id_detail[_id][_]
# print(json.dumps(id_detail[_id],sort_keys=True, indent=4, separators=(',', ': ')))
# print(json.dumps(detail,sort_keys=True, indent=4, separators=(',', ': ')))
checklists.append(detail)
t2 = []
for i in range(multiprocessing.cpu_count()):
t2.append(threading.Thread(target=loop2))
t2[i].start()
for i in range(multiprocessing.cpu_count()):
t2[i].join()
print(f'已获取{len(checklists)}份报告')
return checklists
# taxons = detail['obs']
# for taxon in taxons:
# print(detail["subId"],
# detail["obsDt"],
# id_detail[_id]["lat"],
# id_detail[_id]["lng"],
# id_detail[_id]["locName"],
# taxon["speciesCode"],
# taxon["howManyStr"])
def show(self, checklists):
for item in checklists:
loc = item['loc']
obs = item['obs']
print(loc['lat'], loc['lng'], loc['locName'])
for taxon in obs:
speciesCode = taxon['speciesCode']
# # debug
# if speciesCode == 'litgre3':
# print(json.dumps(taxon,sort_keys=True, indent=4, separators=(',', ': ')))
# input('wait')
sciName = self.get_sciName_from_speciesCode(speciesCode)
comName = self.get_comName_from_sciName(sciName)
howManyStr = taxon["howManyStr"]
print(speciesCode, sciName, comName, howManyStr)
def spp_info(self, checklists):
info = {}
for item in checklists:
loc = item['loc']
obs = item['obs']
obsDt = item['obsDt']
# .split(' ')[0]
for taxon in obs:
speciesCode = taxon['speciesCode']
# print(json.dumps(taxon,sort_keys=True, indent=4, separators=(',', ': ')))
# input('wait...')
sciName = self.get_sciName_from_speciesCode(speciesCode)
comName = self.get_comName_from_sciName(sciName)
howManyStr = taxon["howManyStr"]
if comName not in info:
info[comName] = []
info[comName].append((obsDt, howManyStr, loc['lat'], loc['lng'], loc['locName'],0))
return info