-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbc-api.pl
executable file
·128 lines (83 loc) · 2.83 KB
/
bc-api.pl
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
#!/bin/perl
require "/usr/local/lib/bclib.pl";
require "$bclib{githome}/API/bc-api-functions.pl";
print "Access-Control-Allow-Origin: *\n";
# NOTE: job of bcapi_* functions is to change the %output hash, not
# return anything
# TODO: restore to application/json
# print "Content-type: application/json\n\n";
print "Content-type: text/plain\n\n";
# NOTE: for testing, setenv QUERY_STRING ...
# TODO: restrict which characters allowed in QUERY_STRING, return
# fixed error if bad
# get the query string (can be used globally, so use globopts)
# my(%globopts) = str2hash($ENV{QUERY_STRING});
# QUERY_STRING can be overwritten by options in the command-line version
defaults($ENV{QUERY_STRING});
# the output of the function (can be used globally)
my(%output);
# the result will always contain a copy of the input...
my(%result);
$result{input} = \%globopts;
# figure out what function to call and if it exists and then call it
# debug("D", defined(&{"bcapi_time"}), defined(&{"nossdfada"}), "/D");
my($f) = "bcapi_$globopts{f}";
# now that we know the function, remove it from hash (TODO: bad idea?)
# so it won't confuse called function
delete($globopts{f});
unless (defined(&{$f})) {
# TODO: cleaner exit for functions that dont exist
die("$f not defined");
}
# call the function, it will change %output
&$f;
$result{output} = \%output;
print JSON::to_json(\%result),"\n";
=item bcapi_terminator(%hash)
Given the following, return a terminator of a planet assuming the
light source is the Sun:
i: naif_id
t: time in unix seconds
n: number of points
u: if 0, penumbral terminator
=cut
sub bcapi_terminator {
# remove bad characters
for $i (keys %globopts) {
if ($globopts{$i}=~m/\D/) {
$output{error} .= "All arguments to $globopts{f} must be numeric, but $i -> $globopts{$i} is not";
return;
}
}
my($time) = time();
defaults("i=301&t=$time&n=100&u=1");
if ($i == 399) {
$output{warning} .= "Refraction not computed, results may be especially inaccurate for Earth";
}
my($out, $err, $res) = cache_command("/home/user/bin/bc-terminator -i $globopts{i} -t $globopts{t} -n $globopts{n} -u $globopts{u}");
$output{order} = "lng,lat";
for $i (split(/\n/, $out)) {
if ($i=~/lng(\d+)=(.*?)\&lat(\d+)=(.*?)\&/) {
@{$output{points}}[$1] = [$2, $4];
} else {
my(%hash) = str2hash($i);
$output{data} = \%hash;
}
}
}
# TODO: properly document this
# gives time in a given timezone
# tz = time zone
sub bcapi_time {
if ($globopts{tz}=~s/[^a-z0-9\/]//isg) {
$output{error} .= "Invalid characters in your timezone were removed";
}
# check if file exists
unless (-f "/usr/share/zoneinfo/$globopts{tz}") {
$output{error} .= "Timezone $globopts{tz} does not exist";
return;
}
$ENV{TZ} = $globopts{tz};
$output{date} = `date`;
chomp($output{date});
}