summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean Felder <jfelder@src.gnome.org>2018-08-02 20:20:21 +0200
committerJean Felder <jfelder@src.gnome.org>2018-08-16 08:25:44 +0200
commit7a8cf59d6f529b1e1fa5ac54ab9382bf2324a90d (patch)
treede0fedeabfa8fa59cc7ce15592ee40295fda96f9
parent899538635dafd5e8b4ba89bf7bbb1d65752fa21f (diff)
downloadgrilo-plugins-wip/jfelder/theaudiodb-cover.tar.gz
lua-factory: Add TheAudioDB cover sourcewip/jfelder/theaudiodb-cover
This source allows to download cover art albums from theaudiodb.com website. Api documentation at https://theaudiodb.com/api_guide.php
-rw-r--r--src/lua-factory/sources/grl-theaudiodb-cover.lua109
-rw-r--r--src/lua-factory/sources/meson.build1
2 files changed, 110 insertions, 0 deletions
diff --git a/src/lua-factory/sources/grl-theaudiodb-cover.lua b/src/lua-factory/sources/grl-theaudiodb-cover.lua
new file mode 100644
index 0000000..559b9b9
--- /dev/null
+++ b/src/lua-factory/sources/grl-theaudiodb-cover.lua
@@ -0,0 +1,109 @@
+--[[
+ * Copyright © 2018 The Grilo Developers.
+ *
+ * Contact: Jean Felder <jfelder@gnome.org>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+--]]
+
+---------------------------
+-- Source initialization --
+---------------------------
+
+source = {
+ id = "grl-theaudiodb-cover",
+ name = "TheAudioDB Cover",
+ description = "a source for music covers",
+ supported_keys = { 'thumbnail' },
+ supported_media = { 'audio' },
+ config_keys = {
+ required = { "api-key" },
+ },
+ resolve_keys = {
+ ["type"] = "audio",
+ required = { "artist", "album" },
+ },
+ tags = { 'music', 'net:internet' },
+}
+
+netopts = {
+ user_agent = "Grilo Source TheAudioDB/0.3.8",
+}
+
+------------------
+-- Source utils --
+------------------
+theaudiodb = {}
+covers_fields = {"strAlbumThumb", "strAlbumThumbBack", "strAlbumCDart", "strAlbumSpine"}
+
+THEAUDIODB_SEARCH_ALBUM = 'https://theaudiodb.com/api/v1/json/%s/searchalbum.php?s=%s&a=%s'
+
+---------------------------------
+-- Handlers of Grilo functions --
+---------------------------------
+
+function grl_source_init (configs)
+ theaudiodb.api_key = configs.api_key
+ return true
+end
+
+function grl_source_resolve()
+ local url, keys
+ local artist, title
+
+ keys = grl.get_media_keys()
+ if not keys or not keys.artist or not keys.album
+ or #keys.artist == 0 or #keys.album == 0 then
+ grl.callback()
+ return
+ end
+
+ -- Prepare artist and title strings to the url
+ artist = grl.encode(keys.artist)
+ album = grl.encode(keys.album)
+ url = string.format(THEAUDIODB_SEARCH_ALBUM, theaudiodb.api_key, artist, album)
+ grl.fetch(url, fetch_cb, netopts)
+end
+
+---------------
+-- Utilities --
+---------------
+
+function fetch_cb(result)
+ local json = {}
+
+ if not result then
+ grl.callback()
+ return
+ end
+
+ json = grl.lua.json.string_to_table(result)
+ if not json or not json.album or #json.album == 0 then
+ grl.callback()
+ return
+ end
+
+ local media = {}
+ local thumb = {}
+ for _, val in ipairs(covers_fields) do
+ thumb[#thumb + 1] = json.album[val] or nil
+ end
+
+ media.thumbnail = thumb
+
+ grl.callback(media)
+end
diff --git a/src/lua-factory/sources/meson.build b/src/lua-factory/sources/meson.build
index 3c09db0..2da3c3a 100644
--- a/src/lua-factory/sources/meson.build
+++ b/src/lua-factory/sources/meson.build
@@ -17,6 +17,7 @@ sources = [
'grl-pocket',
'grl-radiofrance',
'grl-spotify-cover',
+ 'grl-theaudiodb-cover',
'grl-thegamesdb',
'grl-video-title-parsing',
]