This repository has been archived by the owner on Jun 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebapi.pl
80 lines (71 loc) · 2.65 KB
/
webapi.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
:- use_module(library('http/http_client')).
:- use_module(library('http/http_open')).
:- use_module(library('http/json')).
% For xml functions
:- use_module(library('xpath')).
:- use_module(library('sgml')).
username('[email protected]').
password('EXObqXHVjyB_7I9awF3zyWQSvRmfuERW_3kqDuS2oSOiG5YToO-uQg').
% http://openweathermap.org/
temperature(City,Temp) :-
First = 'http://api.openweathermap.org/data/2.5/find?q=',
Second = City,
Third = '&mode=json&appid=b4cf58a66894a114580988cc65d8bdcb',
atomic_concat(First,Second,Buffer),
atomic_concat(Buffer,Third,HREF),
http_get(HREF,json(R),[]),
member(list=L,R),
member(json(M),L),
member(main=json(Main),M),
member(temp=T,Main),
Temp is round(T - 273.15).
% http://www.ns.nl/api/api
% http://webservices.ns.nl/ns-api-avt?station=${Naam of afkorting Station}
trains_from(City, Out) :-
username(U), password(P),
format(atom(HREF),'http://webservices.ns.nl/ns-api-avt?station=~s',[City]),
http_open(HREF,Xml,[authorization(basic(U,P))]),
load_xml(stream(Xml), Out, []),
close(Xml).
next_train_from_to(From,To,Time) :-
trains_from(From,O),
xpath(O,//'VertrekkendeTrein',P),
xpath(P,//'EindBestemming',Q),
Q = element('EindBestemming',[],[City]),
downcase_atom(City,LCase),
LCase = To,
xpath(P,//'VertrekTijd',element('VertrekTijd', [], [Time])).
% http://www.omdbapi.com/
director(Name, Result) :-
format(atom(HREF), 'http://www.omdbapi.com?t=~s', [Name]),
http_get(HREF,Json,[]),
atom_json_term(Json, json(R),[]),
member('Director'=Result,R).
% http://fixer.io/
exchange(From,To,Amount,Result) :-
upcase_atom(From,UFrom), upcase_atom(To,UTo),
format(atom(HREF), 'http://api.fixer.io/latest?base=~s&symbols=~s', [UFrom,UTo]),
http_get(HREF,Json,[]),
atom_json_term(Json,json(R),[]),
member(rates=json([_W=Value]),R),
Result is Value * Amount.
% http://ip-api.com/docs/
geoIP(Country,City,Region,Zip,Lat,Lon) :-
http_get('http://ip-api.com/json',Json,[]),
atom_json_term(Json,json(R),[]),
member('city'=City,R),
member('country'=Country,R),
member('regionName'=Region,R),
member('zip'=Zip,R),
member('lat'=Lat,R),
member('lon'=Lon,R).
geoIP(IP,Country,City,Region,Zip,Lat,Lon) :-
format(atom(HREF),'http://ip-api.com/json/~s',[IP]),
http_get(HREF,Json,[]),
atom_json_term(Json,json(R),[]),
member('city'=City,R),
member('country'=Country,R),
member('regionName'=Region,R),
member('zip'=Zip,R),
member('lat'=Lat,R),
member('lon'=Lon,R).