Daniel@0: /* Part of DML (Digital Music Laboratory) Daniel@0: Copyright 2014-2015 Samer Abdallah, University of London Daniel@0: Daniel@0: This program is free software; you can redistribute it and/or Daniel@0: modify it under the terms of the GNU General Public License Daniel@0: as published by the Free Software Foundation; either version 2 Daniel@0: of the License, or (at your option) any later version. Daniel@0: Daniel@0: This program is distributed in the hope that it will be useful, Daniel@0: but WITHOUT ANY WARRANTY; without even the implied warranty of Daniel@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Daniel@0: GNU General Public License for more details. Daniel@0: Daniel@0: You should have received a copy of the GNU General Public Daniel@0: License along with this library; if not, write to the Free Software Daniel@0: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Daniel@0: */ Daniel@0: Daniel@0: :- module(echotools, [ echo/1, mb_to_echo/2, mb_to_spotify/2 ]). Daniel@0: Daniel@0: :- use_module(echonest). Daniel@0: :- use_module(library(jpath)). Daniel@0: :- use_module(library(dictutils)). Daniel@0: :- use_module(library(insist)). Daniel@0: :- use_module(library(dcg_core)). Daniel@0: :- use_module(library(dcg_pair)). Daniel@0: Daniel@0: echonest_key('NPIODUIKW9DJN1UCI'). Daniel@0: Daniel@0: echo(Path,Params,Decoder) :- Daniel@0: echonest_key(K), echocall(K,Path,Params,Resp), Daniel@0: decode(response:Decoder,Resp). Daniel@0: Daniel@0: decode(A,X) :- jpath(A,X). Daniel@0: Daniel@0: echo(mood(X)) :- echo([artist,list_terms],[type=mood],terms:_#name:X). Daniel@0: echo(style(X)) :- echo([artist,list_terms],[type=style],terms:_#name:X). Daniel@0: echo(genre(X)) :- echo([genre,list],[],genres:_#name:X). Daniel@0: echo(genre_description(X,D)) :- var(X), !, echo_facets([genre,list],[],genres,[name:X,description:D]). Daniel@0: echo(genre_description(X,D)) :- must_be(text,X), !, echo_facets([genre,profile],[name=X],genres,[description:D]). Daniel@0: echo(search(genre,Term,X,D)) :- echo([genre,search],[name=Term,bucket=description],genres:_#(name:X,description(D))). Daniel@0: Daniel@0: echo(genre_artist(Genre,Facets)) :- echo_facets([genre,artists],[name=Genre],artists,Facets). Daniel@0: echo(artist_genre(ArtistID,G)) :- echo([artist,profile],[id=ArtistID,bucket=genre],artist:genres:_#name:G). Daniel@0: echo(genre_facets(Facets)) :- echo_facets([genre,list],[],genres,Facets). Daniel@0: echo(songs(Ids,Facets,Opts)) :- Daniel@0: seqmap(param(id),Ids,Params,Opts), Daniel@0: echo_facets([song,profile],Params,songs,Facets). Daniel@0: Daniel@0: echo(search(song,Conditions,Facets)) :- Daniel@0: echo_facets([song,search],Conditions,songs,Facets). Daniel@0: Daniel@0: echo(genre_song(Genre,Artist,Title,Id)) :- Daniel@0: echo(genre_artist(Genre,[name:Artist,songs:_#(id:Id,title:Title)])). Daniel@0: Daniel@0: echo(genre_songs_by_title(Genre,Artist,Title,Ids)) :- Daniel@0: setof(Id,echo(genre_song(Genre,Artist,Title,Id)),Ids). Daniel@0: Daniel@0: param(P,X) --> [P=X]. Daniel@0: Daniel@0: echo_facets(Path,Params0,Field,Facets) :- Daniel@0: included(Path,Incs), Daniel@0: seqmap(facet(Incs),Facets,Params-Decs,Params0-[]), Daniel@0: list_to_conj(Decs,Decoder), Daniel@0: echo(Path,Params,\Response), Daniel@0: catch( jpath(Field:_#Decoder,Response), Daniel@0: Ex, ( print_message(warning,Ex), Daniel@0: format(user_error,"Response was:\n",[]), Daniel@0: print_dict(user_error,Response,[]), Daniel@0: fail)). Daniel@0: Daniel@0: list_to_conj([D],D). Daniel@0: list_to_conj([D1,D2|Ds],(D1,D2s)) :- list_to_conj([D2|Ds],D2s). Daniel@0: Daniel@0: facet(_,\X) --> !, \> [\X]. Daniel@0: facet(Included,Name:Decoder) --> !, Daniel@0: if(\+memberchk(Name,Included), \< [bucket=Name]), Daniel@0: \> [Name:Decoder]. Daniel@0: Daniel@0: included(Path,Included) :- insist(included_db(Path,Included),unknown_includes(Path)). Daniel@0: Daniel@0: included_db([genre,list],[name]). Daniel@0: included_db([genre,artists],[id,name]). Daniel@0: included_db([song,profile],[id,title,artist_id,artist_name]). Daniel@0: included_db([song,search],[id,title,artist_id,artist_name]). Daniel@0: included_db([genre,profile],[name]). Daniel@0: Daniel@0: mb_to_echo(ID,Songs) :- Daniel@0: atom_concat('musicbrainz:song:',ID,FID), Daniel@0: findall(Song, Daniel@0: echo(songs([FID],[\Song, tracks:1# \_], [ bucket='id:spotify'])), Daniel@0: Songs). Daniel@0: Daniel@0: %% mb_to_spotify(+MBId:atom,-SpURI:atom) is nondet. Daniel@0: % Finds Spotify URIs corresponding to MusicBrainz recording IDs using Daniel@0: % the Echonest Rosetta web service. Daniel@0: mb_to_spotify(ID,SpID) :- Daniel@0: mb_to_echo(ID,Songs), Daniel@0: member(Song,Songs), Daniel@0: jpath(tracks:_#foreign_id:SpID,Song). Daniel@0: