comparison cpack/dml/lib/spotify/echotools.pl @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:718306e29690
1 /* Part of DML (Digital Music Laboratory)
2 Copyright 2014-2015 Samer Abdallah, University of London
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 :- module(echotools, [ echo/1, mb_to_echo/2, mb_to_spotify/2 ]).
20
21 :- use_module(echonest).
22 :- use_module(library(jpath)).
23 :- use_module(library(dictutils)).
24 :- use_module(library(insist)).
25 :- use_module(library(dcg_core)).
26 :- use_module(library(dcg_pair)).
27
28 echonest_key('NPIODUIKW9DJN1UCI').
29
30 echo(Path,Params,Decoder) :-
31 echonest_key(K), echocall(K,Path,Params,Resp),
32 decode(response:Decoder,Resp).
33
34 decode(A,X) :- jpath(A,X).
35
36 echo(mood(X)) :- echo([artist,list_terms],[type=mood],terms:_#name:X).
37 echo(style(X)) :- echo([artist,list_terms],[type=style],terms:_#name:X).
38 echo(genre(X)) :- echo([genre,list],[],genres:_#name:X).
39 echo(genre_description(X,D)) :- var(X), !, echo_facets([genre,list],[],genres,[name:X,description:D]).
40 echo(genre_description(X,D)) :- must_be(text,X), !, echo_facets([genre,profile],[name=X],genres,[description:D]).
41 echo(search(genre,Term,X,D)) :- echo([genre,search],[name=Term,bucket=description],genres:_#(name:X,description(D))).
42
43 echo(genre_artist(Genre,Facets)) :- echo_facets([genre,artists],[name=Genre],artists,Facets).
44 echo(artist_genre(ArtistID,G)) :- echo([artist,profile],[id=ArtistID,bucket=genre],artist:genres:_#name:G).
45 echo(genre_facets(Facets)) :- echo_facets([genre,list],[],genres,Facets).
46 echo(songs(Ids,Facets,Opts)) :-
47 seqmap(param(id),Ids,Params,Opts),
48 echo_facets([song,profile],Params,songs,Facets).
49
50 echo(search(song,Conditions,Facets)) :-
51 echo_facets([song,search],Conditions,songs,Facets).
52
53 echo(genre_song(Genre,Artist,Title,Id)) :-
54 echo(genre_artist(Genre,[name:Artist,songs:_#(id:Id,title:Title)])).
55
56 echo(genre_songs_by_title(Genre,Artist,Title,Ids)) :-
57 setof(Id,echo(genre_song(Genre,Artist,Title,Id)),Ids).
58
59 param(P,X) --> [P=X].
60
61 echo_facets(Path,Params0,Field,Facets) :-
62 included(Path,Incs),
63 seqmap(facet(Incs),Facets,Params-Decs,Params0-[]),
64 list_to_conj(Decs,Decoder),
65 echo(Path,Params,\Response),
66 catch( jpath(Field:_#Decoder,Response),
67 Ex, ( print_message(warning,Ex),
68 format(user_error,"Response was:\n",[]),
69 print_dict(user_error,Response,[]),
70 fail)).
71
72 list_to_conj([D],D).
73 list_to_conj([D1,D2|Ds],(D1,D2s)) :- list_to_conj([D2|Ds],D2s).
74
75 facet(_,\X) --> !, \> [\X].
76 facet(Included,Name:Decoder) --> !,
77 if(\+memberchk(Name,Included), \< [bucket=Name]),
78 \> [Name:Decoder].
79
80 included(Path,Included) :- insist(included_db(Path,Included),unknown_includes(Path)).
81
82 included_db([genre,list],[name]).
83 included_db([genre,artists],[id,name]).
84 included_db([song,profile],[id,title,artist_id,artist_name]).
85 included_db([song,search],[id,title,artist_id,artist_name]).
86 included_db([genre,profile],[name]).
87
88 mb_to_echo(ID,Songs) :-
89 atom_concat('musicbrainz:song:',ID,FID),
90 findall(Song,
91 echo(songs([FID],[\Song, tracks:1# \_], [ bucket='id:spotify'])),
92 Songs).
93
94 %% mb_to_spotify(+MBId:atom,-SpURI:atom) is nondet.
95 % Finds Spotify URIs corresponding to MusicBrainz recording IDs using
96 % the Echonest Rosetta web service.
97 mb_to_spotify(ID,SpID) :-
98 mb_to_echo(ID,Songs),
99 member(Song,Songs),
100 jpath(tracks:_#foreign_id:SpID,Song).
101