Daniel@0
|
1 /* Part of DML (Digital Music Laboratory)
|
Daniel@0
|
2 Copyright 2014-2015 Samer Abdallah, University of London
|
Daniel@0
|
3
|
Daniel@0
|
4 This program is free software; you can redistribute it and/or
|
Daniel@0
|
5 modify it under the terms of the GNU General Public License
|
Daniel@0
|
6 as published by the Free Software Foundation; either version 2
|
Daniel@0
|
7 of the License, or (at your option) any later version.
|
Daniel@0
|
8
|
Daniel@0
|
9 This program is distributed in the hope that it will be useful,
|
Daniel@0
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Daniel@0
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Daniel@0
|
12 GNU General Public License for more details.
|
Daniel@0
|
13
|
Daniel@0
|
14 You should have received a copy of the GNU General Public
|
Daniel@0
|
15 License along with this library; if not, write to the Free Software
|
Daniel@0
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
Daniel@0
|
17 */
|
Daniel@0
|
18
|
Daniel@0
|
19 :- module(echonest,
|
Daniel@0
|
20 [ echocall/4
|
Daniel@0
|
21 ]).
|
Daniel@0
|
22
|
Daniel@0
|
23 /** <module> Interface to echonest Web API
|
Daniel@0
|
24 */
|
Daniel@0
|
25
|
Daniel@0
|
26 :- use_module(library(fileutils)).
|
Daniel@0
|
27 :- use_module(library(http/json)).
|
Daniel@0
|
28 :- use_module(library(jpath)).
|
Daniel@0
|
29 :- use_module(library(webby)).
|
Daniel@0
|
30 :- use_module(library(state)).
|
Daniel@0
|
31
|
Daniel@0
|
32 :- set_prolog_flag(double_quotes,string).
|
Daniel@0
|
33
|
Daniel@0
|
34 :- setting(min_wait,number,3,"Minimum time between echonest requests").
|
Daniel@0
|
35 :- initialization set_state(next_request_time,0).
|
Daniel@0
|
36
|
Daniel@0
|
37 wait_respectfully :-
|
Daniel@0
|
38 get_time(Now),
|
Daniel@0
|
39 setting(min_wait,TMin),
|
Daniel@0
|
40 get_state(next_request_time,T0), T1 is max(Now,T0) + TMin,
|
Daniel@0
|
41 set_state(next_request_time,T1),
|
Daniel@0
|
42 ( Now>=T0 -> true
|
Daniel@0
|
43 ; DT is T0-Now,
|
Daniel@0
|
44 debug(echonest,"Sleeping for ~f seconds to respect rate limit",[DT]),
|
Daniel@0
|
45 sleep(DT)
|
Daniel@0
|
46 ).
|
Daniel@0
|
47
|
Daniel@0
|
48 echocall(Key,PathParts,Params,Response) :-
|
Daniel@0
|
49 debug(echonest,"Echonest call: ~q, ~q...",[PathParts,Params]),
|
Daniel@0
|
50 parts_path([api,v4|PathParts],Path),
|
Daniel@0
|
51 wait_respectfully,
|
Daniel@0
|
52 catch( restcall(get([api_key=Key, format=json | Params]), json(Response),
|
Daniel@0
|
53 [protocol(http), host('developer.echonest.com'), path(Path)], []),
|
Daniel@0
|
54 Ex, handle(Ex)).
|
Daniel@0
|
55
|
Daniel@0
|
56 handle(http_bad_status(SC,codes(Doc))) :- !,
|
Daniel@0
|
57 with_input_from(codes(Doc),json_read_dict(current_input,Dict)),
|
Daniel@0
|
58 jpath(response:status:(code:Code,message:Msg),Dict),
|
Daniel@0
|
59 throw(echonest_error(SC,Code,Msg)).
|
Daniel@0
|
60 handle(Ex) :- throw(Ex).
|
Daniel@0
|
61
|
Daniel@0
|
62
|
Daniel@0
|
63 prolog:message(echonest_error(SC,Code,Msg)) -->
|
Daniel@0
|
64 {http_status(SC,Meaning)},
|
Daniel@0
|
65 ["(HTTP ~w: ~w) Echonest error ~w: '~s'."-[SC,Meaning,Code,Msg]].
|