Mercurial > hg > dml-open-cliopatria
comparison cpack/dml/lib/beets_p2r.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(beets_p2r, []). | |
20 | |
21 /** <module> Access to beets database | |
22 */ | |
23 | |
24 | |
25 :- use_module(library(prosqlite)). | |
26 :- use_module(library(musicbrainz)). | |
27 :- use_module(library(semweb/rdf_db)). | |
28 :- use_module(library(rdfutils)). | |
29 :- use_module(entailment(p2r)). | |
30 | |
31 :- set_prolog_flag(double_quotes,string). | |
32 | |
33 :- rdf_register_prefix(beets,'http://dml.org/beets/props/item/'). | |
34 :- rdf_register_prefix(beets_album,'http://dml.org/beets/props/album/'). | |
35 :- rdf_register_prefix(audio,'audio:'). | |
36 :- rdf_register_prefix(dml,'http://dml.org/dml/'). | |
37 :- rdf_register_prefix(mb,'http://musicbrainz.org/'). | |
38 | |
39 :- setting(db_file,string,"~/lib/beets/music-ro.db","Location of beets database"). | |
40 :- setting(audio_root,string,"/usr/local/share/Music/","Root of audio file directory tree"). | |
41 | |
42 | |
43 rdf(audio:tail(Rel), rdf:type, mo:'AudioFile'), | |
44 rdf(audio:tail(Rel), mo:encoding, literal(Enc)) <== | |
45 setting(audio_root,Root), | |
46 expand_file_name(Root,[Root1]), | |
47 item(Id,path,Path), | |
48 item(Id,format,Enc), | |
49 atom_concat(Root1,Rel,Path). | |
50 | |
51 rdf(audio:tail(Rel), beets:enc(Prop), literal(Val)) <== | |
52 audio_item_path(Id,Rel), | |
53 item(Id,Prop,Val1), | |
54 ( Prop=path -> exists_file(Val1); true), | |
55 as_typed_literal(Val1,Val). | |
56 | |
57 rdf(Signal,rdf:type,mo:'Signal') <== | |
58 item(_,mb_trackid,ID), % track id is actually a Recording. | |
59 mb_id_uri(recording,ID,Signal). | |
60 | |
61 rdf(Signal,mo:available_as,audio:tail(Rel)) <== | |
62 setting(audio_root,Root), | |
63 expand_file_name(Root,[Root1]), | |
64 item(Id,path,Path), | |
65 exists_file(Path), | |
66 atom_concat(Root1,Rel,Path), | |
67 item(Id,mb_trackid,ID), % track id is actually a Recording. | |
68 % need to ask Linkedbrainz to get Signal URI | |
69 mb_id_uri(recording,ID,Signal). | |
70 | |
71 rdf(audio:tail(Rel), dml:file_artist, URI) <== | |
72 audio_item_path(Id,Rel), | |
73 item(Id,mb_artistid,ID), | |
74 mb_id_uri(artist,ID,URI). | |
75 | |
76 rdf(audio:tail(Rel), dml:file_release, URI) <== | |
77 audio_item_path(Id,Rel), | |
78 item(Id,mb_albumid,ID), | |
79 mb_id_uri(release,ID,URI). | |
80 | |
81 rdf(audio:tail(Rel), dml:file_recording, URI) <== | |
82 audio_item_path(Id,Rel), | |
83 item(Id,mb_trackid,ID), | |
84 mb_id_uri(recording,ID,URI). | |
85 | |
86 % rdf(URI,mo:release_type,mo: | |
87 rdf(URI,rdf:type,mo:'Release') <== | |
88 item(_,mb_albumid,ID), | |
89 mb_id_uri(release,ID,URI). | |
90 | |
91 audio_item_path(Id,Rel) :- | |
92 setting(audio_root,Root), | |
93 expand_file_name(Root,[Root1]), | |
94 item(Id,path,Path), | |
95 atom_concat(Root1,Rel,Path). | |
96 | |
97 :- public import/0. | |
98 import :- with_beets_db(assert_all(beets_p2r)). | |
99 | |
100 with_beets_db(Goal) :- | |
101 setting(db_file,DBFile), | |
102 expand_file_name(DBFile,[Path|_]), | |
103 setup_call_cleanup( | |
104 sqlite_connect(Path,Con,[alias(beets),ext(''),as_predicates(true),arity(unary),at_module(beets)]), | |
105 Goal, | |
106 sqlite_disconnect(Con)). | |
107 | |
108 :- dynamic beets:items/1. | |
109 item(Id) :- beets:items([id=Id]). | |
110 | |
111 item(Id,path,Path) :- | |
112 ( var(Path) -> beets:items([id=Id,path=Path]) | |
113 ; var(Id) -> sqlite_format_query(beets,"select id from items where path like '~s'"-[Path],row(Id)) | |
114 ; sqlite_format_query(beets,"select null from items where id=~w and path like '~s'"-[Id,Path],row(_)) | |
115 ). | |
116 | |
117 item(Id,Prop,Val) :- | |
118 table_column(items,Prop), Prop\=path, Prop\=comp, | |
119 beets:items([id=Id,Prop=Val]), | |
120 \+invalid(Prop,Val). | |
121 | |
122 | |
123 % album(Id) :- beets:albums([id=Id]). | |
124 % album(Id,Prop,Val) :- | |
125 % table_column(albums,Prop), | |
126 % beets:albums([id=Id,Prop=Val]), Val\=''. | |
127 | |
128 table_column(T,C) :- | |
129 sqlite_table_column(beets,T,C1), C=C1. | |
130 | |
131 invalid(_,''). | |
132 invalid(Prop,0) :- nonzero(Prop). | |
133 invalid(mtime,0.0). | |
134 | |
135 nonzero(bitdepth). | |
136 nonzero(bitrate). | |
137 nonzero(samplerate). | |
138 nonzero(track). | |
139 nonzero(tracktotal). | |
140 nonzero(disc). | |
141 nonzero(disctotal). | |
142 nonzero(bpm). | |
143 nonzero(day). | |
144 nonzero(month). | |
145 nonzero(year). | |
146 nonzero(original_day). | |
147 nonzero(original_month). | |
148 nonzero(original_year). | |
149 | |
150 :- public audio_file/3. | |
151 | |
152 audio_file(URI,File,just(T0)) :- | |
153 rdf(URI,beets:path,literal(File)), !, | |
154 rdf(URI,beets:format,literal(F0)), | |
155 format_type(F0,T0). | |
156 | |
157 format_type('MP3',mp3). | |
158 format_type('OGG',ogg). | |
159 format_type('AAC',aac). | |
160 format_type('ALAC',mp4). % !!! ?? |