annotate 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
rev   line source
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(echotools, [ echo/1, mb_to_echo/2, mb_to_spotify/2 ]).
Daniel@0 20
Daniel@0 21 :- use_module(echonest).
Daniel@0 22 :- use_module(library(jpath)).
Daniel@0 23 :- use_module(library(dictutils)).
Daniel@0 24 :- use_module(library(insist)).
Daniel@0 25 :- use_module(library(dcg_core)).
Daniel@0 26 :- use_module(library(dcg_pair)).
Daniel@0 27
Daniel@0 28 echonest_key('NPIODUIKW9DJN1UCI').
Daniel@0 29
Daniel@0 30 echo(Path,Params,Decoder) :-
Daniel@0 31 echonest_key(K), echocall(K,Path,Params,Resp),
Daniel@0 32 decode(response:Decoder,Resp).
Daniel@0 33
Daniel@0 34 decode(A,X) :- jpath(A,X).
Daniel@0 35
Daniel@0 36 echo(mood(X)) :- echo([artist,list_terms],[type=mood],terms:_#name:X).
Daniel@0 37 echo(style(X)) :- echo([artist,list_terms],[type=style],terms:_#name:X).
Daniel@0 38 echo(genre(X)) :- echo([genre,list],[],genres:_#name:X).
Daniel@0 39 echo(genre_description(X,D)) :- var(X), !, echo_facets([genre,list],[],genres,[name:X,description:D]).
Daniel@0 40 echo(genre_description(X,D)) :- must_be(text,X), !, echo_facets([genre,profile],[name=X],genres,[description:D]).
Daniel@0 41 echo(search(genre,Term,X,D)) :- echo([genre,search],[name=Term,bucket=description],genres:_#(name:X,description(D))).
Daniel@0 42
Daniel@0 43 echo(genre_artist(Genre,Facets)) :- echo_facets([genre,artists],[name=Genre],artists,Facets).
Daniel@0 44 echo(artist_genre(ArtistID,G)) :- echo([artist,profile],[id=ArtistID,bucket=genre],artist:genres:_#name:G).
Daniel@0 45 echo(genre_facets(Facets)) :- echo_facets([genre,list],[],genres,Facets).
Daniel@0 46 echo(songs(Ids,Facets,Opts)) :-
Daniel@0 47 seqmap(param(id),Ids,Params,Opts),
Daniel@0 48 echo_facets([song,profile],Params,songs,Facets).
Daniel@0 49
Daniel@0 50 echo(search(song,Conditions,Facets)) :-
Daniel@0 51 echo_facets([song,search],Conditions,songs,Facets).
Daniel@0 52
Daniel@0 53 echo(genre_song(Genre,Artist,Title,Id)) :-
Daniel@0 54 echo(genre_artist(Genre,[name:Artist,songs:_#(id:Id,title:Title)])).
Daniel@0 55
Daniel@0 56 echo(genre_songs_by_title(Genre,Artist,Title,Ids)) :-
Daniel@0 57 setof(Id,echo(genre_song(Genre,Artist,Title,Id)),Ids).
Daniel@0 58
Daniel@0 59 param(P,X) --> [P=X].
Daniel@0 60
Daniel@0 61 echo_facets(Path,Params0,Field,Facets) :-
Daniel@0 62 included(Path,Incs),
Daniel@0 63 seqmap(facet(Incs),Facets,Params-Decs,Params0-[]),
Daniel@0 64 list_to_conj(Decs,Decoder),
Daniel@0 65 echo(Path,Params,\Response),
Daniel@0 66 catch( jpath(Field:_#Decoder,Response),
Daniel@0 67 Ex, ( print_message(warning,Ex),
Daniel@0 68 format(user_error,"Response was:\n",[]),
Daniel@0 69 print_dict(user_error,Response,[]),
Daniel@0 70 fail)).
Daniel@0 71
Daniel@0 72 list_to_conj([D],D).
Daniel@0 73 list_to_conj([D1,D2|Ds],(D1,D2s)) :- list_to_conj([D2|Ds],D2s).
Daniel@0 74
Daniel@0 75 facet(_,\X) --> !, \> [\X].
Daniel@0 76 facet(Included,Name:Decoder) --> !,
Daniel@0 77 if(\+memberchk(Name,Included), \< [bucket=Name]),
Daniel@0 78 \> [Name:Decoder].
Daniel@0 79
Daniel@0 80 included(Path,Included) :- insist(included_db(Path,Included),unknown_includes(Path)).
Daniel@0 81
Daniel@0 82 included_db([genre,list],[name]).
Daniel@0 83 included_db([genre,artists],[id,name]).
Daniel@0 84 included_db([song,profile],[id,title,artist_id,artist_name]).
Daniel@0 85 included_db([song,search],[id,title,artist_id,artist_name]).
Daniel@0 86 included_db([genre,profile],[name]).
Daniel@0 87
Daniel@0 88 mb_to_echo(ID,Songs) :-
Daniel@0 89 atom_concat('musicbrainz:song:',ID,FID),
Daniel@0 90 findall(Song,
Daniel@0 91 echo(songs([FID],[\Song, tracks:1# \_], [ bucket='id:spotify'])),
Daniel@0 92 Songs).
Daniel@0 93
Daniel@0 94 %% mb_to_spotify(+MBId:atom,-SpURI:atom) is nondet.
Daniel@0 95 % Finds Spotify URIs corresponding to MusicBrainz recording IDs using
Daniel@0 96 % the Echonest Rosetta web service.
Daniel@0 97 mb_to_spotify(ID,SpID) :-
Daniel@0 98 mb_to_echo(ID,Songs),
Daniel@0 99 member(Song,Songs),
Daniel@0 100 jpath(tracks:_#foreign_id:SpID,Song).
Daniel@0 101