[grilo-plugins] [flickr] Add function to get list of hot tags



commit 74cdb753def0f63a803aed6524865705f7eb55db
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Fri Jun 11 22:30:28 2010 +0200

    [flickr] Add function to get list of hot tags
    
    Function added to help gflickr library.

 src/flickr/gflickr.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/flickr/gflickr.h |    8 ++++
 2 files changed, 111 insertions(+), 0 deletions(-)
---
diff --git a/src/flickr/gflickr.c b/src/flickr/gflickr.c
index 588ae27..4171174 100644
--- a/src/flickr/gflickr.c
+++ b/src/flickr/gflickr.c
@@ -17,6 +17,7 @@
 
 #define FLICKR_PHOTOS_SEARCH_METHOD "flickr.photos.search"
 #define FLICKR_PHOTOS_GETINFO_METHOD "flickr.photos.getInfo"
+#define FLICKR_TAGS_GETHOTLIST_METHOD "flickr.tags.getHotList"
 
 #define FLICKR_PHOTOS_SEARCH                            \
   FLICKR_ENDPOINT                                       \
@@ -30,6 +31,14 @@
   "&tags=%s"                                            \
   "&text=%s"
 
+#define FLICKR_TAGS_GETHOTLIST                          \
+  FLICKR_ENDPOINT                                       \
+  "api_key=%s"                                          \
+  "&auth_token=%s"                                      \
+  "&api_sig=%s"                                         \
+  "&method=" FLICKR_TAGS_GETHOTLIST_METHOD              \
+  "&count=%d"
+
 #define FLICKR_PHOTOS_GETINFO                   \
   FLICKR_ENDPOINT                               \
   "api_key=%s"                                  \
@@ -44,6 +53,7 @@ typedef struct {
   ParseXML parse_xml;
   GFlickrPhotoCb get_info_cb;
   GFlickrPhotoListCb search_cb;
+  GFlickrTagListCb gethotlist_cb;
   gpointer user_data;
 } GFlickrData;
 
@@ -131,6 +141,28 @@ get_api_sig_photos_search (GFlickr *f,
 }
 
 static gchar *
+get_api_sig_tags_gethotlist (GFlickr *f,
+                             gint count)
+{
+  gchar *signature;
+  gchar *text_to_sign;
+
+  text_to_sign = g_strdup_printf ("%s"
+                                  "api_key%s"
+                                  "auth_token%s"
+                                  "count%d"
+                                  "method" FLICKR_TAGS_GETHOTLIST_METHOD,
+                                  f->priv->auth_secret,
+                                  f->priv->api_key,
+                                  f->priv->auth_token,
+                                  count);
+  signature = g_compute_checksum_for_string (G_CHECKSUM_MD5, text_to_sign, -1);
+  g_free (text_to_sign);
+
+  return signature;
+}
+
+static gchar *
 get_api_sig_photos_getInfo (GFlickr *f, glong photo_id)
 {
   gchar *signature;
@@ -215,6 +247,16 @@ get_photo (xmlNodePtr node)
   return photo;
 }
 
+static gchar *
+get_tag (xmlNodePtr node)
+{
+  if (xmlStrcmp (node->name, (const xmlChar *) "tag") == 0) {
+    return (gchar *) xmlNodeGetContent (node);
+  } else {
+    return NULL;
+  }
+}
+
 static void
 process_photo_result (const gchar *xml_result, gpointer user_data)
 {
@@ -275,6 +317,39 @@ process_photolist_result (const gchar *xml_result, gpointer user_data)
 }
 
 static void
+process_taglist_result (const gchar *xml_result, gpointer user_data)
+{
+  GFlickrData *data = (GFlickrData *) user_data;
+  GList *taglist = NULL;
+  xmlDocPtr doc;
+  xmlNodePtr node;
+
+  doc = xmlReadMemory (xml_result, xmlStrlen ((xmlChar*) xml_result), NULL,
+                       NULL, XML_PARSE_RECOVER | XML_PARSE_NOBLANKS);
+  node = xmlDocGetRootElement (doc);
+
+  /* Check if result is OK */
+  if (!node || !result_is_correct (node)) {
+    data->gethotlist_cb (NULL, NULL, data->user_data);
+  } else {
+    node = node->xmlChildrenNode;
+
+    /* Now we're at "hot tags" node */
+    node = node->xmlChildrenNode;
+    while (node) {
+      taglist = g_list_prepend (taglist, get_tag (node));
+      node = node->next;
+    }
+
+    data->gethotlist_cb (NULL, g_list_reverse (taglist), data->user_data);
+    g_list_foreach (taglist, (GFunc) g_free, NULL);
+    g_list_free (taglist);
+  }
+  g_slice_free (GFlickrData, data);
+  xmlFreeDoc (doc);
+}
+
+static void
 read_done_cb (GObject *source_object,
               GAsyncResult *res,
               gpointer user_data)
@@ -443,3 +518,31 @@ g_flickr_photo_url_thumbnail (GFlickr *f, GHashTable *photo)
                             secret);
   }
 }
+
+void
+g_flickr_tags_getHotList (GFlickr *f,
+                          gint count,
+                          GFlickrTagListCb callback,
+                          gpointer user_data)
+{
+  g_return_if_fail (G_IS_FLICKR (f));
+
+  gchar *api_sig = get_api_sig_tags_gethotlist (f, count);
+
+  /* Build the request */
+  gchar *request = g_strdup_printf (FLICKR_TAGS_GETHOTLIST,
+                                    f->priv->api_key,
+                                    f->priv->auth_token,
+                                    api_sig,
+                                    count);
+
+  g_free (api_sig);
+
+  GFlickrData *gfd = g_slice_new (GFlickrData);
+  gfd->parse_xml = process_taglist_result;
+  gfd->gethotlist_cb = callback;
+  gfd->user_data = user_data;
+
+  read_url_async (request, gfd);
+  g_free (request);
+}
diff --git a/src/flickr/gflickr.h b/src/flickr/gflickr.h
index 6a9dbdc..428f8ef 100644
--- a/src/flickr/gflickr.h
+++ b/src/flickr/gflickr.h
@@ -76,6 +76,8 @@ typedef void (*GFlickrPhotoCb) (GFlickr *f, GHashTable *photo, gpointer user_dat
 
 typedef void (*GFlickrPhotoListCb) (GFlickr *f, GList *photolist, gpointer user_data);
 
+typedef void (*GFlickrTagListCb) (GFlickr *f, GList *taglist, gpointer user_data);
+
 GType g_flickr_get_type (void);
 
 GFlickr *g_flickr_new (const gchar *api_key, const gchar *auth_token, const gchar *auth_secret);
@@ -102,4 +104,10 @@ g_flickr_photo_url_original (GFlickr *f, GHashTable *photo);
 gchar *
 g_flickr_photo_url_thumbnail (GFlickr *f, GHashTable *photo);
 
+void
+g_flickr_tags_getHotList (GFlickr *f,
+                          gint count,
+                          GFlickrTagListCb callback,
+                          gpointer user_data);
+
 #endif /* _G_FLICKR_H_ */



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