Music

List all files

SELECT ?url
WHERE { 
  ?song a nmm:MusicPiece ;
        nie:isStoredAs ?as .
  ?as nie:url ?url .
}

List all files and their properties

This uses the OPTIONAL SPARQL keyword. This means that the data may or may not exist. Without this, results are not listed if it is just requested.

SELECT * 
WHERE {
  ?song a nmm:MusicPiece ;
        nie:title ?title ;
        nmm:performer [ nmm:artistName ?performer ] ;
        nmm:musicAlbum [ nie:title ?album ] .
  OPTIONAL { 
    ?song nmm:genre ?genre 
  } .
  OPTIONAL { 
    ?song nmm:trackNumber ?track 
  } .
  OPTIONAL { 
    ?song nmm:composer [ nmm:artistName ?composer ] 
  }
}

List all albums

This query includes the number of songs and total duration of the music:

SELECT ?album ?title COUNT(?song) AS songs SUM(?length) AS totallength 
WHERE {
  ?album a nmm:MusicAlbum ;
           nie:title ?title .
  ?song nmm:musicAlbum ?album ;
        nmm:length ?length
} 
GROUP BY ?album

List all album and artist combinations

SELECT ?album ?performer ?url
WHERE {
  ?song nmm:musicAlbum ?album ; 
        nmm:performer ?performer ;
        nie:isStoredAs ?as .
  ?as nie:url ?url .
} GROUP BY ?album ?performer

List everything from a single artist

SELECT ?song ?title ?url
WHERE {
  ?song nmm:performer [ nmm:artistName 'Artist Name' ] ;
        nie:title ?title ;
        nie:isStoredAs ?as .
  ?as nie:url ?url .
}

List all artists, album count, song count and total song duration

SELECT ?performer COUNT(?album) AS albumcount SUM(?length) AS totallength 
WHERE {
  ?song nmm:performer ?performer ;
        nmm:length ?length ;
        nmm:musicAlbum ?album
} GROUP BY ?performer

Set last played to a item

DELETE {
  ?unknown nie:contentAccessed ?time
} WHERE {
  ?unknown nie:contentAccessed ?time ;
           nie:isStoredAs ?as .
  ?as nie:url 'file:///home/user/Music/song.mp3' .
}
INSERT {
  ?unknown nie:contentAccessed '2009-05-15T11:30:00' ;
} WHERE {
  ?unknown nie:contentAccessed ?time ;
           nie:isStoredAs ?as .
  ?as nie:url 'file:///home/user/Music/song.mp3' .
}

Set play count of a song

DELETE {
  ?unknown nie:usageCounter ?count
} WHERE {
  ?unknown nie:usageCounter ?count ;
           nie:isStoredAs ?as .
  ?as nie:url 'file:///home/user/Music/song.mp3' .
}
INSERT {
  ?unknown nie:usageCounter 42
} WHERE {
  ?unknown nie:contentAccessed ?time ;
           nie:isStoredAs ?as .
  ?as nie:url 'file:///home/user/Music/song.mp3' .
}

List most listened songs

SELECT ?song ?title ?count ?url
WHERE {
  ?song nie:usageCounter ?count ;
        nie:title ?title ;
        nie:isStoredAs ?as .
  ?as nie:url ?url .
} ORDER BY DESC(?count) LIMIT 10

List most recently listened to songs

SELECT ?song ?title ?time ?url
WHERE {
  ?song nie:contentAccessed ?time ;
        nie:title ?title ;
        nie:isStoredAs ?as .
  ?as nie:url ?url .
} ORDER BY DESC(?time) LIMIT 10

Projects/Tracker/Documentation/Examples/SPARQL/Music (last edited 2013-11-25 12:54:25 by WilliamJonMcCann)