Last active
July 7, 2016 10:32
-
-
Save saifulbkhan/50fab7364273b9bca9f6d1e633f5ee38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--------------------------- | |
-- Source initialization -- | |
--------------------------- | |
source = { | |
id = "grl-musicbrainz", | |
name = "MusicBrainz", | |
description = "A plugin for fetching music metadata", | |
supported_keys = { | |
"title", | |
"artist", | |
"album", | |
"duration", | |
"mb-artist-id", | |
"mb-release-id", | |
"mb-release-date" | |
}, | |
supported_media = { "audio" }, | |
resolve_keys = { | |
["type"] = "audio", | |
required = { | |
"mb-recording-id" | |
} | |
}, | |
tags = { 'music', 'net:internet', 'net:plaintext' }, | |
} | |
------------------ | |
-- Source utils -- | |
------------------ | |
MUSICBRAINZ_BASE = "http://musicbrainz.org/ws/2/" | |
MUSICBRAINZ_LOOKUP_ARTIST = MUSICBRAINZ_BASE .. "artist/%s" | |
MUSICBRAINZ_LOOKUP_RECORDING = MUSICBRAINZ_BASE .. "recording/%s?inc=artists+releases" | |
MUSICBRAINZ_LOOKUP_RELEASE = MUSICBRAINZ_BASE .. "release/%s" | |
media_table = {} | |
--------------------------------- | |
-- Handlers of Grilo functions -- | |
--------------------------------- | |
function grl_source_resolve(media, options, callback) | |
local media = grl.get_media_keys() | |
if not media or | |
not media.mb_recording_id or | |
#media.mb_recording_id == 0 then | |
grl.callback() | |
return | |
end | |
media_table['mb_recording_id'] = media.mb_recording_id | |
media_table['mb_artist_id'] = media.mb_artist_id | |
media_table['mb_release_id'] = media.mb_release_id | |
musicbrainz_execute_resolve() | |
end | |
--------------- | |
-- Utilities -- | |
--------------- | |
function musicbrainz_execute_resolve() | |
--[[Resolve recording related keys first as it can provide artist and release | |
related metadata if it were requested]] | |
rec_id = media_table['mb_recording_id'] | |
if rec_id then | |
recording_url = string.format (MUSICBRAINZ_LOOKUP_RECORDING, rec_id) | |
debug_str = string.format ("Resolving metadata for recording ID: %s", rec_id) | |
grl.debug(debug_str) | |
grl.fetch(recording_url, fetch_recording_cb) | |
end | |
-- TODO: add metadata resolution for mb_artist_id and mb_release_id | |
end | |
--[[These functions append fetched values to the table 'media_table' which is then returned to grl.callback]] | |
function fetch_recording_cb(results) | |
recording = get_recording(results) | |
if not recording then | |
grl.callback() | |
else | |
if recording.title then | |
media_table['title'] = recording.title.xml | |
end | |
if recording.artist-credit and | |
recording.artist-credit.name-credit and | |
recording.artist-credit.name-credit.artist then | |
artist = recording.artist-credit.name-credit.artist | |
media_table['artist'] = artist.name.xml | |
media_table['mb-artist-id'] = artist.id.xml | |
end | |
if recording.release-list and | |
#recording.release-list ~= 0 then | |
local release = nil | |
for index, rel in pairs(recording.release-list) do | |
if rel.artist-credit and rel.artist-credit.name-credit and | |
rel.artist-credit.name-credit.artist and | |
rel.artist-credit.name-credit.artist.id.xml == media_table['mb-artist-id'] then | |
release = rel | |
end | |
end | |
--if no release with given artist-id then consider only the first result | |
if release == nil then | |
release = recording.release-list[1] | |
end | |
media_table['album'] = release.title.xml | |
media_table['mb-release-id'] = release.id.xml | |
end | |
--[[TODO: media_table needs to be updated by other resolve functions as well | |
before its finally sent to grl.callback]] | |
grl.callback(media_table) | |
end | |
end | |
function get_recording(results) | |
if not results then return nil end | |
local results_table = grl.lua.xml.string_to_table(results) | |
if not results_table then return nil end | |
if results_table.metadata and | |
results_table.metadata.recording then | |
return results_table.metadata.recording | |
end | |
return nil | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment