-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnemesis.py
executable file
·53 lines (38 loc) · 1.43 KB
/
nemesis.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
#!/usr/bin/python
import json
import csv
import sys
import os
import subprocess
if len(sys.argv) is not 4 :
print "Usage: " + sys.argv[0] + " <lat> <long> <sciname>"
exit(0)
DIST = '50' #search radius, in km. API defaults to 25km, capped at 50
BACK = '5' # days to search. API default is a very generous 14
LAT = sys.argv[1]
LNG = sys.argv[2]
NEMESIS = sys.argv[3]
DEBUG = False
def debug (s) :
if (DEBUG) :
print(s)
def readrecents (f) :
recents = json.loads(f);
# We only care about some fields
def filter (s) :
return {'obsDt' : s['obsDt'], 'locName' : s['locName'], 'howMany' : s['howMany']}
return map(filter, recents)
recentsUrl = 'http://ebird.org/ws1.1/data/obs/geo_spp/recent?dist=' + DIST + '&back=' + BACK + \
'&lng=' + str(LNG) + '&lat=' + str(LAT) + '&sci=' + str(NEMESIS) + '&fmt=json';
debug("*** loading URL: " + recentsUrl);
## Read the recent sightings (-s for silent mode curl)
## Sadface: check_output is python 2.7, and people.mozilla.org only has 2.6
## output = subprocess.check_output(["curl", "-s", recentsUrl])
output = subprocess.Popen(["curl", "-s", recentsUrl], stdout=subprocess.PIPE).communicate()[0]
debug("** curl output **")
debug(output);
#recents = readrecents(os.popen('curl ' + recentsUrl).read())
recents = readrecents(output)
debug("** Found {0} recent sightings".format(len(recents)))
for bird in recents :
print("{obsDt} - {locName} ({howMany})".format(**bird))