-
Notifications
You must be signed in to change notification settings - Fork 1
/
Vocab.py
133 lines (98 loc) · 4.15 KB
/
Vocab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import RDF
ns = {
'dc': RDF.NS('http://purl.org/dc/elements/1.1/'),
'foaf': RDF.NS('http://xmlns.com/foaf/0.1/'),
'frbr': RDF.NS('http://purl.org/vocab/frbr/core#'),
'rdf': RDF.NS('http://www.w3.org/1999/02/22-rdf-syntax-ns#'),
'nao': RDF.NS('http://www.semanticdesktop.org/ontologies/2007/08/15/nao#'),
# original URL has been removed (MusicBrainz noooooo :()
# described at <http://www.schemaweb.info/schema/SchemaInfo.aspx?id=168>
'mm': RDF.NS('http://musicbrainz.org/mm/mm-2.1#'),
'mo': RDF.NS('http://purl.org/ontology/mo/'),
# need this to get xs:int track_numbers working
'xs': RDF.NS('http://www.w3.org/2001/XMLSchema#'),
}
import TripleStore
def resource_is_type(s, t):
return TripleStore.model.get_target(s, ns['rdf'].type) == t
def rating(resource):
'''a resource's nao:numericRating'''
rating = TripleStore.model.get_target(resource, ns['nao'].numericRating)
if rating is None:
return 0
return int(rating.literal_value['string'])
def rate(resource, rating):
'''give a resource a nao:numericRating (float from 1-10)'''
resource = RDF.Node(RDF.Uri(resource))
rating = RDF.Node(literal=str(rating), datatype=ns['xs'].float.uri)
# delete any existing ratings for this resource
TripleStore.forget(resource, ns['nao'].numericRating, None)
TripleStore.state(resource, ns['nao'].numericRating, rating)
def tags(resource):
'''the nao:prefLabels of a resource's nao:hasTags'''
for tag in TripleStore.model.get_targets(resource, ns['nao'].hasTag):
name = str(TripleStore.model.get_target(tag, ns['nao'].prefLabel))
yield name
def tag(resource, tags):
'''tag a resource with a space-separated string'''
resource = RDF.Node(RDF.Uri(resource))
# space-separated, remove empty
tags = [x for x in tags.strip().split(' ') if x != '']
# remove all existing tags on this resource
TripleStore.forget(resource, ns['nao'].hasTag, None)
for _tag in tags:
# XXX check if it's a nao:Tag too
tag = TripleStore.model.get_source(ns['nao'].prefLabel, RDF.Node(_tag))
if not tag:
# create a new nao:Tag with the appropriate prefLabel
tag = RDF.Node(blank=None)
TripleStore.state(tag, ns['rdf'].type, ns['nao'].Tag)
TripleStore.state(tag, ns['nao'].prefLabel, RDF.Node(_tag))
TripleStore.state(resource, ns['nao'].hasTag, tag)
def track_filename(track):
filename = str(TripleStore.model.get_source(ns['mo'].encodes, track).uri)
# strip the file:// scheme prefix
return filename[7:]
def artist_name(artist_uri):
if str(artist_uri.uri) == 'http://zitgist.com/music/artist/89ad4ac3-39f7-470e-963a-56509c546377':
return 'Various Artists'
else:
return str(TripleStore.model.get_target(artist_uri, ns['foaf'].name))
def artists_albums():
# get a dict of all albums, keyed by artist URI
albums = {}
_albums = TripleStore.model.get_sources(ns['rdf'].type, ns['mo'].Record)
for album_uri in _albums:
artist_uri = TripleStore.model.get_target(album_uri, ns['foaf'].maker)
title = str(TripleStore.model.get_target(album_uri, ns['dc'].title))
if not artist_uri in albums:
albums[artist_uri] = []
albums[artist_uri].append((title, album_uri,))
# get a list of all artists with an album in the dict
artists = []
for artist_uri in albums.keys():
name = artist_name(artist_uri)
artists.append((name, artist_uri,))
artists.sort()
return (artists, albums)
def artists_albums_tagged(_tag):
# get a dict of all albums tagged with a certain label, keyed by artist URI
albums = {}
artists = []
tag = TripleStore.model.get_source(ns['nao'].prefLabel, RDF.Node(_tag))
if tag is None:
return (artists, albums)
_tagged = TripleStore.model.get_sources(ns['nao'].hasTag, tag)
for tagged in _tagged:
if not resource_is_type(tagged, ns['mo'].MusicArtist):
continue
name = artist_name(tagged)
artists.append((name, tagged,))
albums[tagged] = []
for made in TripleStore.model.get_sources(ns['foaf'].maker, tagged):
if not resource_is_type(made, ns['mo'].Record):
continue
title = str(TripleStore.model.get_target(made, ns['dc'].title))
albums[tagged].append((title, made))
artists.sort()
return (artists, albums)