-
Notifications
You must be signed in to change notification settings - Fork 6
/
iNatAPIv1_observations_observers.html
187 lines (178 loc) · 9.32 KB
/
iNatAPIv1_observations_observers.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, minimum-scale=1.0" />
<meta name="description" content="iNaturalist API Get Observations Observers" />
<title>iNaturalist API Get Observations Observers</title>
<style>
:root {
background: var(--color-base);
color: var(--color-text);
font: 14px Sans-Serif;
--color-base: white;
--color-alt: whitesmoke;
--color-brand: forestgreen;
--color-text: black;
--color-text-invert: white;
--color-text-link: royalblue;
--color-border: lightgray;
--color-hover: lightgray;
--color-base-translucent: rgba(255,255,255,0.85);
}
@media (prefers-color-scheme: dark) {
:root {
--color-base: black;
--color-alt: #171717;
--color-brand: forestgreen;
--color-text: #bababa;
--color-text-invert: black;
--color-text-link: cornflowerblue;
--color-border: #444;
--color-hover: #444;
--color-base-translucent: rgba(0,0,0,0.85);
}
}
#main { width:100%; }
table, td, th { border-collapse:collapse; margin:0; padding:4px; }
th { position:-webkit-sticky /*Safari*/; position:sticky; top:0; font-weight:600; background:var(--color-brand); color:var(--color-text-invert); text-align:left; vertical-align:bottom; }
tbody>tr { border-width:1px 0px; border-style:solid; border-color:var(--color-border); background:var(--color-alt);}
tr:nth-child(even) { background:var(--color-base); }
.tar { text-align:right; }
.icon { height:48px; width:48px; border-radius:50%; }
.photo { height:64px; width:64px; }
img { margin:0; padding:0; border:0; }
a { text-decoration:none; color:var(--color-text-link); }
a:hover { background:var(--color-hover); }
#nav { margin-left:-8px; position:-webkit-sticky /*Safari*/; position:sticky; left:0; bottom:0; background:var(--color-base-translucent); width:188px; height:52px; border-radius:14px; }
@media print { #nav { display:none; } }
.button, .button_inactive { display:inline-block; margin-left:10px; margin-top:10px; height:32px; width:32px; border:1px solid var(--color-border); border-radius:35%; font-size:24px; vertical-align:middle; text-align:center; }
.button { background:var(--color-alt); }
.button_inactive { background:none; color:var(--color-border); }
</style>
</head>
<body>
<script>
let winurlstr = window.location.href;
let winurlsearchstr = window.location.search;
let winurlexsearchstr = winurlstr.replace(winurlsearchstr,'');
let winurlparams = new URLSearchParams(winurlsearchstr.substring(1));
//let p_per_page = winurlparams.get('per_page') || 200;
//let p_page = winurlparams.get('page') || 1;
function furl(url,txt=url) { return '<a href="'+url+'">'+txt+'</a>'; };
function famp(str) { return str.replace(/&/g,'&'); };
function fcomnum(n) { return n.toString().replace(/\B(?=(\d{3})+(?!\d))/g,',') };
function fdate(str,dateonly=false) {
str = str.replace(/t/i,' '); //replaces T (case insensitive) with a space
if (dateonly) { str = str.split(' ')[0]; }
else {
str = str.replace(/([+-]\d{2}\:?\d{2})/,' ($1)'); //puts parenthesis around time zone offset
str = str.replace(/z/i,' (+00:00)'); //replaces Z (case insensitve) with UTC
str = str.replace('+00:00','±00:00');
};
return str;
};
function faddelem(etype,eparent=null,eattributes={}) {
let eobj = document.createElement(etype);
for (let [key,value] of Object.entries(eattributes)) {
if ( typeof value === 'object' && value !== null ) {
for (let [subkey,subvalue] of Object.entries(value)) { eobj[key][subkey] = subvalue; };
}
else { eobj[key] = value; };
};
if (eparent) { eparent.appendChild(eobj); };
return eobj;
};
function faddelems(etype,eparent=null,eattributes=[]) { for (let e of eattributes) { faddelem(etype,eparent,e); }; };
function fpageurl(urlbase,urlparams,per_page,page) {
let params = new URLSearchParams(urlparams);
let url_per_page = params.get('per_page');
let url_page = params.get('page');
(url_per_page===null) ? params.append('per_page',per_page) : params.set('per_page',per_page);
(url_page===null) ? params.append('page',page) : params.set('page',page);
return urlbase+'?'+params;
};
function fresults(xobj) {
//faddelem('p',document.body,{innerHTML:furl(famp(apiurl))});
let results = xobj.results;
if (results) {
let total_results = xobj.total_results;
let per_page = xobj.per_page;
let page_curr = xobj.page;
let page_max = Math.ceil(total_results/per_page);
let page_prev = ((page_curr>1)?page_curr-1:null);
let page_next = ((page_curr<page_max)?page_curr+1:null);
faddelem('p',document.body,{innerHTML:'total observers: '+fcomnum(total_results)+'<br />'
+'per page: '+fcomnum(per_page)+'<br />'
+'page: '+fcomnum(page_curr)+' of '+fcomnum(page_max)+'<br />'
});
let table = faddelem('table',document.body,{id:'main'});
let thead = faddelem('thead',table);
let hrow = faddelem('tr',thead);
let labels = [
{innerText:'Rank'},
{innerText:'Icon'},
{innerText:'User ID'},
{innerText:'Login ID'},
{innerText:'User Name'},
{innerText:'Joined Date'},
{classList:'tar',innerText:'Observation Count'},
{classList:'tar',innerText:'Species Count'},
// {classList:'tar',innerText:'IDs'},
// {classList:'tar',innerText:'All IDs (For Others Only)'},
];
faddelems('th',hrow,labels);
let tbody = faddelem('tbody',table);
for (let i=0; i<results.length; i++) {
let brow = faddelem('tr',tbody);
let rec = results[i];
let values = [
{innerText:i+1},
{innerHTML:'<img class="icon" src="'+rec.user.icon+'" />'},
{innerHTML:furl('https://www.inaturalist.org/users/'+rec.user_id,rec.user_id)},
{innerText:rec.user.login||''},
{innerText:rec.user.name||''},
{innerText:fdate(rec.user.created_at,true)},
{classList:'tar',innerText:fcomnum(rec.observation_count)},
{classList:'tar',innerText:fcomnum(rec.species_count)},
];
faddelems('td',brow,values);
};
// buttons to go to prev or next page
let nav = faddelem('div',document.body,{id:'nav'});
(page_curr<=1)?faddelem('span',nav,{classList:'button_inactive',title:'already on first page',innerHTML:'«'}):faddelem('a',nav,{classList:'button',title:'first page',id:'button_first',innerHTML:'«',href:fpageurl(winurlexsearchstr,winurlparams,per_page,1)});
(page_prev===null)?faddelem('span',nav,{classList:'button_inactive',title:'no previous page',innerHTML:'‹'}):faddelem('a',nav,{classList:'button',title:'previous page',id:'button_prev',innerHTML:'‹',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_prev)});
(page_next===null)?faddelem('span',nav,{classList:'button_inactive',title:'no next page',innerHTML:'›'}):faddelem('a',nav,{classList:'button',title:'next page',id:'button_next',innerHTML:'›',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_next)});
(page_curr>=page_max)?faddelem('span',nav,{classList:'button_inactive',title:'already on last page',innerHTML:'»'}):faddelem('a',nav,{classList:'button',title:'last page',id:'button_last',innerHTML:'»',href:fpageurl(winurlexsearchstr,winurlparams,per_page,page_max)});
}
else { faddelem('p',document.body,{innerText:'No results returned.'}); };
};
let apibase = 'https://api.inaturalist.org/v1/observations/observers';
let apiurl = apibase+((winurlparams!='')?('?'+winurlparams):'');
let apirefurl = 'https://api.inaturalist.org/v1/docs/#!/Observations/get_observations_observers';
let apirefname = 'iNaturalist Observations Observers';
let apiref = furl(apirefurl,apirefname);
faddelem('h1',document.body,{innerText:apirefname});
if (winurlsearchstr==='') {
let instructions = [
{innerHTML:'This page displays the response from the '+apiref+' API endpoint in a human-readable format. To use this page, add at least one query parameter to the URL. See '+furl(apirefurl)+' for available query parameters.'},
{innerHTML:'Suppose the address of this page is '+winurlexsearchstr+', <br />and you want to see the results of '+furl(famp(apibase+'?year=2023&order_by=species_count'))+' in a friendly format, <br />then you would open '+furl(famp(winurlexsearchstr+'?year=2023&order_by=species_count'))+' in your browser.'},
];
faddelems('p',document.body,instructions);
}
else {
faddelem('p',document.body,{innerHTML:'This is the base query: '+furl(famp(apiurl))+'. (This page will accept most parameters from the '+apiref+' API endpoint.)'});
fetch(apiurl)
.then((response) => {
if (!response.ok) { throw new Error(response.status+' ('+response.statusText+') returned from '+response.url); };
return response.json();
})
.then((data) => { fresults(data); })
.catch((err) => {
console.error(err.message);
faddelem('p',document.body,{innerText:'There was a problem retrieving data. Error '+err.message+'.'});
});
};
</script>
</body>
</html>