[grilo-plugins/wip/gbsneto/lastfm-scrobbling-api: 2/2] lastfm-albumart: use the Scrobbling API



commit 1f2c4f53519f7065a0c8385ec4ad43555e7fa5dd
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Sun Sep 6 11:18:55 2015 -0300

    lastfm-albumart: use the Scrobbling API
    
    Last.FM old plain-XML based API was deprecated and
    stopped working some time ago.
    
    This patch fixes it by introducing the new Last.FM
    Scrobbling API. Consequently, it now requires an API
    key to be given. It is up to the consumer application
    to pass it through GrlConfig.

 src/lastfm-albumart/grl-lastfm-albumart.c    |   61 +++++++++++++++++++++-----
 src/lastfm-albumart/grl-lastfm-albumart.h    |    2 +
 tests/lastfm-albumart/test_lastfm_albumart.c |    5 ++
 3 files changed, 57 insertions(+), 11 deletions(-)
---
diff --git a/src/lastfm-albumart/grl-lastfm-albumart.c b/src/lastfm-albumart/grl-lastfm-albumart.c
index cf5bef4..4dba028 100644
--- a/src/lastfm-albumart/grl-lastfm-albumart.c
+++ b/src/lastfm-albumart/grl-lastfm-albumart.c
@@ -34,6 +34,11 @@
 
 #include "grl-lastfm-albumart.h"
 
+#define GRL_LASTFM_ALBUMART_GET_PRIVATE(object)                  \
+  (G_TYPE_INSTANCE_GET_PRIVATE((object),                         \
+                               GRL_LASTFM_ALBUMART_SOURCE_TYPE,  \
+                               GrlLastfmAlbumartSourcePrivate))
+
 /* ---------- Logging ---------- */
 
 #define GRL_LOG_DOMAIN_DEFAULT lastfm_albumart_log_domain
@@ -41,7 +46,7 @@ GRL_LOG_DOMAIN_STATIC(lastfm_albumart_log_domain);
 
 /* -------- Last.FM API -------- */
 
-#define LASTFM_GET_ALBUM 
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&api_key=7a2461fe34c9c8124fb28ac750ba12fa&artist=%s&album=%s";
+#define LASTFM_GET_ALBUM 
"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&api_key=%s&artist=%s&album=%s";
 
 #define LASTFM_DEFAULT_IMAGE "http://cdn.last.fm/flatness/catalogue/noimage/2/default_album_medium.png";
 #define LASTFM_BASE_IMAGE    "http://userserve-ak.last.fm/serve/%s/%s";
@@ -61,9 +66,13 @@ GRL_LOG_DOMAIN_STATIC(lastfm_albumart_log_domain);
 #define SOURCE_NAME _("Album art Provider from Last.FM")
 #define SOURCE_DESC _("A plugin for getting album arts using Last.FM as backend")
 
+struct _GrlLastfmAlbumartSourcePrivate {
+  gchar *api_key;
+};
+
 static GrlNetWc *wc;
 
-static GrlLastfmAlbumartSource *grl_lastfm_albumart_source_new (void);
+static GrlLastfmAlbumartSource *grl_lastfm_albumart_source_new (const gchar *api_key);
 
 static void grl_lastfm_albumart_source_finalize (GObject *object);
 
@@ -92,6 +101,10 @@ grl_lastfm_albumart_source_plugin_init (GrlRegistry *registry,
                                         GrlPlugin *plugin,
                                         GList *configs)
 {
+  GrlLastfmAlbumartSource *source;
+  GrlConfig *config;
+  gchar *api_key = NULL;
+
   GRL_LOG_DOMAIN_INIT (lastfm_albumart_log_domain, "lastfm-albumart");
 
   GRL_DEBUG ("grl_lastfm_albumart_source_plugin_init");
@@ -100,12 +113,22 @@ grl_lastfm_albumart_source_plugin_init (GrlRegistry *registry,
   bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
   bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
 
-  GrlLastfmAlbumartSource *source = grl_lastfm_albumart_source_new ();
-  grl_registry_register_source (registry,
-                                plugin,
-                                GRL_SOURCE (source),
-                                NULL);
-  return TRUE;
+  if (configs) {
+    config = GRL_CONFIG (configs->data);
+    api_key = grl_config_get_api_key (config);
+
+    if (!api_key) {
+      GRL_INFO ("Cannot load plugin: missing API Key");
+      return FALSE;
+    }
+  }
+
+  source = grl_lastfm_albumart_source_new (api_key);
+
+  return grl_registry_register_source (registry,
+                                       plugin,
+                                       GRL_SOURCE (source),
+                                       NULL);
 }
 
 GRL_PLUGIN_REGISTER (grl_lastfm_albumart_source_plugin_init,
@@ -115,19 +138,26 @@ GRL_PLUGIN_REGISTER (grl_lastfm_albumart_source_plugin_init,
 /* ================== Last.FM-AlbumArt GObject ================ */
 
 static GrlLastfmAlbumartSource *
-grl_lastfm_albumart_source_new (void)
+grl_lastfm_albumart_source_new (const gchar *api_key)
 {
+  GrlLastfmAlbumartSource *source;
+
   const char *tags[] = {
     "net:internet",
     NULL
   };
   GRL_DEBUG ("grl_lastfm_albumart_source_new");
-  return g_object_new (GRL_LASTFM_ALBUMART_SOURCE_TYPE,
+
+  source = g_object_new (GRL_LASTFM_ALBUMART_SOURCE_TYPE,
                       "source-id", SOURCE_ID,
                       "source-name", SOURCE_NAME,
                       "source-desc", SOURCE_DESC,
                       "source-tags", tags,
                       NULL);
+
+  source->priv->api_key = g_strdup (api_key);
+
+  return source;
 }
 
 static void
@@ -147,6 +177,7 @@ grl_lastfm_albumart_source_class_init (GrlLastfmAlbumartSourceClass * klass)
 static void
 grl_lastfm_albumart_source_init (GrlLastfmAlbumartSource *source)
 {
+  source->priv = GRL_LASTFM_ALBUMART_GET_PRIVATE (source);
 }
 
 G_DEFINE_TYPE (GrlLastfmAlbumartSource,
@@ -156,7 +187,12 @@ G_DEFINE_TYPE (GrlLastfmAlbumartSource,
 static void
 grl_lastfm_albumart_source_finalize (GObject *object)
 {
+  GrlLastfmAlbumartSourcePrivate *priv;
+
+  priv = GRL_LASTFM_ALBUMART_SOURCE (object)->priv;
+
   g_clear_object (&wc);
+  g_clear_pointer (&priv->api_key, g_free);
 
   G_OBJECT_CLASS (grl_lastfm_albumart_source_parent_class)->finalize (object);
 }
@@ -372,6 +408,7 @@ static void
 grl_lastfm_albumart_source_resolve (GrlSource *source,
                                     GrlSourceResolveSpec *rs)
 {
+  GrlLastfmAlbumartSourcePrivate *priv;
   const gchar *artist = NULL;
   const gchar *album = NULL;
   gchar *esc_artist = NULL;
@@ -380,6 +417,8 @@ grl_lastfm_albumart_source_resolve (GrlSource *source,
 
   GRL_DEBUG (__FUNCTION__);
 
+  priv = GRL_LASTFM_ALBUMART_SOURCE (source)->priv;
+
   GList *iter;
 
   /* Check that albumart is requested */
@@ -409,7 +448,7 @@ grl_lastfm_albumart_source_resolve (GrlSource *source,
     } else {
       esc_artist = g_uri_escape_string (artist, NULL, TRUE);
       esc_album = g_uri_escape_string (album, NULL, TRUE);
-      url = g_strdup_printf (LASTFM_GET_ALBUM, esc_artist, esc_album);
+      url = g_strdup_printf (LASTFM_GET_ALBUM, priv->api_key, esc_artist, esc_album);
       read_url_async (source, url, rs);
       g_free (esc_artist);
       g_free (esc_album);
diff --git a/src/lastfm-albumart/grl-lastfm-albumart.h b/src/lastfm-albumart/grl-lastfm-albumart.h
index 7d9e4b4..5ba6636 100644
--- a/src/lastfm-albumart/grl-lastfm-albumart.h
+++ b/src/lastfm-albumart/grl-lastfm-albumart.h
@@ -53,11 +53,13 @@
                               GRL_LASTFM_ALBUMART_SOURCE_TYPE,          \
                               GrlLastfmAlbumartSourceClass))
 
+typedef struct _GrlLastfmAlbumartSourcePrivate GrlLastfmAlbumartSourcePrivate;
 typedef struct _GrlLastfmAlbumartSource GrlLastfmAlbumartSource;
 
 struct _GrlLastfmAlbumartSource {
 
   GrlSource parent;
+  GrlLastfmAlbumartSourcePrivate *priv;
 
 };
 
diff --git a/tests/lastfm-albumart/test_lastfm_albumart.c b/tests/lastfm-albumart/test_lastfm_albumart.c
index 0966068..1ec612e 100644
--- a/tests/lastfm-albumart/test_lastfm_albumart.c
+++ b/tests/lastfm-albumart/test_lastfm_albumart.c
@@ -41,9 +41,14 @@ static void
 test_setup (void)
 {
   GError *error = NULL;
+  GrlConfig *config;
   GrlRegistry *registry;
 
+  config = grl_config_new (LASTFM_ALBUMART_ID, NULL);
+  grl_config_set_api_key (config, "7a2461fe34c9c8124fb28ac750ba12fa");
+
   registry = grl_registry_get_default ();
+  grl_registry_add_config (registry, config, NULL);
   grl_registry_load_all_plugins (registry, &error);
   g_assert_no_error (error);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]