[grilo-plugins] flickr: reuse get_api_sig() function



commit d70436a476513a4cef3433e11c109087e6d91b57
Author: Juan A. Suarez Romero <jasuarez igalia com>
Date:   Tue Jul 6 12:29:25 2010 +0200

    flickr: reuse get_api_sig() function
    
    Old functions were using a custom function instead this generic one.

 src/flickr/Makefile.am       |    4 +-
 src/flickr/gflickr.c         |  115 +++++++++++++-----------------------------
 src/flickr/grl-flickr-auth.c |   90 ---------------------------------
 3 files changed, 39 insertions(+), 170 deletions(-)
---
diff --git a/src/flickr/Makefile.am b/src/flickr/Makefile.am
index 94a6680..96beb28 100644
--- a/src/flickr/Makefile.am
+++ b/src/flickr/Makefile.am
@@ -33,7 +33,9 @@ libgrlflickrauth_la_LIBADD =	\
 
 libgrlflickrauth_la_SOURCES =	\
 	grl-flickr-auth.c	\
-	grl-flickr-auth.h
+	grl-flickr-auth.h	\
+	gflickr.c		\
+	gflickr.h
 
 libplugindir		= $(GRL_PLUGINS_DIR)
 libauthdir			= $(libdir)
diff --git a/src/flickr/gflickr.c b/src/flickr/gflickr.c
index 06b682e..b4cbc10 100644
--- a/src/flickr/gflickr.c
+++ b/src/flickr/gflickr.c
@@ -220,82 +220,6 @@ get_xpath_element (const gchar *content,
   return element;
 }
 
-static gchar *
-get_api_sig_photos_search (GFlickr *f,
-                           const gchar *text,
-                           const gchar *tags,
-                           gint page) {
-  gchar *signature;
-  gchar *text_to_sign;
-
-  text_to_sign = g_strdup_printf ("%s"
-                                  "api_key%s"
-                                  "%s""%s"
-                                  "extrasmedia,date_taken,owner_name,url_o,url_t"
-                                  "method" FLICKR_PHOTOS_SEARCH_METHOD
-                                  "page%d"
-                                  "per_page%d"
-                                  "tags%s"
-                                  "text%s",
-                                  f->priv->auth_secret,
-                                  f->priv->api_key,
-                                  f->priv->auth_token? "auth_token": "",
-                                  f->priv->auth_token? f->priv->auth_token: "",
-                                  page,
-                                  f->priv->per_page,
-                                  tags,
-                                  text);
-  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_tags_gethotlist (GFlickr *f,
-                             gint count)
-{
-  gchar *signature;
-  gchar *text_to_sign;
-
-  text_to_sign = g_strdup_printf ("%s"
-                                  "api_key%s"
-                                  "%s""%s"
-                                  "count%d"
-                                  "method" FLICKR_TAGS_GETHOTLIST_METHOD,
-                                  f->priv->auth_secret,
-                                  f->priv->api_key,
-                                  f->priv->auth_token? "auth_token": "",
-                                  f->priv->auth_token? 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;
-  gchar *text_to_sign;
-
-  text_to_sign = g_strdup_printf ("%s"
-                                  "api_key%s"
-                                  "%s""%s"
-                                  "method" FLICKR_PHOTOS_GETINFO_METHOD
-                                  "photo_id%ld",
-                                  f->priv->auth_secret,
-                                  f->priv->api_key,
-                                  f->priv->auth_token? "auth_token": "",
-                                  f->priv->auth_token? f->priv->auth_token: "",
-                                  photo_id);
-  signature = g_compute_checksum_for_string (G_CHECKSUM_MD5, text_to_sign, -1);
-  g_free (text_to_sign);
-
-  return signature;
-}
-
 static gboolean
 result_is_correct (xmlNodePtr node)
 {
@@ -515,7 +439,15 @@ g_flickr_photos_getInfo (GFlickr *f,
 
   g_return_if_fail (G_IS_FLICKR (f));
 
-  gchar *api_sig = get_api_sig_photos_getInfo (f, photo_id);
+  gchar *str_photo_id = g_strdup_printf ("%ld", photo_id);
+  gchar *api_sig = get_api_sig (f->priv->auth_secret,
+                                "api_key", f->priv->api_key,
+                                "method", FLICKR_PHOTOS_GETINFO_METHOD,
+                                "photo_id", str_photo_id,
+                                f->priv->auth_token? "auth_token": "",
+                                f->priv->auth_token? f->priv->auth_token: "",
+                                NULL);
+  g_free (str_photo_id);
 
   /* Build the request */
   if (f->priv->auth_token) {
@@ -560,7 +492,23 @@ g_flickr_photos_search (GFlickr *f,
     tags = "";
   }
 
-  gchar *api_sig = get_api_sig_photos_search (f, text, tags, page);
+  gchar *strpage = g_strdup_printf ("%d", page);
+  gchar *strperpage = g_strdup_printf ("%d", f->priv->per_page);
+
+  gchar *api_sig =
+    get_api_sig (f->priv->auth_secret,
+                 "api_key", f->priv->api_key,
+                 "extras", "media,date_taken,owner_name,url_o,url_t",
+                 "method", FLICKR_PHOTOS_SEARCH_METHOD,
+                 "page", strpage,
+                 "per_page", strperpage,
+                 "tags", tags,
+                 "text", text,
+                 f->priv->auth_token? "auth_token": "",
+                 f->priv->auth_token? f->priv->auth_token: "",
+                 NULL);
+  g_free (strpage);
+  g_free (strperpage);
 
   /* Build the request */
   if (f->priv->auth_token) {
@@ -658,7 +606,16 @@ g_flickr_tags_getHotList (GFlickr *f,
 
   g_return_if_fail (G_IS_FLICKR (f));
 
-  gchar *api_sig = get_api_sig_tags_gethotlist (f, count);
+  gchar *strcount = g_strdup_printf ("%d", count);
+
+  gchar *api_sig = get_api_sig (f->priv->auth_secret,
+                                "api_key", f->priv->api_key,
+                                "count", strcount,
+                                "method", FLICKR_TAGS_GETHOTLIST_METHOD,
+                                f->priv->auth_token? "auth_token": "",
+                                f->priv->auth_token? f->priv->auth_token: "",
+                                NULL);
+  g_free (strcount);
 
   /* Build the request */
   if (f->priv->auth_token) {
diff --git a/src/flickr/grl-flickr-auth.c b/src/flickr/grl-flickr-auth.c
index d1f970c..a197719 100644
--- a/src/flickr/grl-flickr-auth.c
+++ b/src/flickr/grl-flickr-auth.c
@@ -1,96 +1,6 @@
 #include "grl-flickr-auth.h"
 #include "gflickr.h"
 
-#include <gio/gio.h>
-#include <libxml/parser.h>
-#include <libxml/xpath.h>
-#include <string.h>
-
-#define FLICKR_ENTRYPOINT "http://api.flickr.com/services/rest/?";
-#define FLICKR_AUTH       "http://flickr.com/services/auth/?";
-
-static gchar *
-get_api_sig (const gchar *secret, ...)
-{
-  GHashTable *hash;
-  GList *key_iter;
-  GList *keys;
-  GString *to_sign;
-  gchar *api_sig;
-  gchar *key;
-  gchar *value;
-  gint text_size = strlen (secret);
-  va_list va_params;
-
-  hash = g_hash_table_new (g_str_hash, g_str_equal);
-
-  va_start (va_params, secret);
-  while ((key = va_arg (va_params, gchar *))) {
-    text_size += strlen (key);
-    value = va_arg (va_params, gchar *);
-    text_size += strlen (value);
-    g_hash_table_insert (hash, key, value);
-  }
-  va_end (va_params);
-
-  to_sign = g_string_sized_new (text_size);
-  g_string_append (to_sign, secret);
-
-  keys = g_hash_table_get_keys (hash);
-  keys = g_list_sort (keys, (GCompareFunc) g_strcmp0);
-  for (key_iter = keys; key_iter; key_iter = g_list_next (key_iter)) {
-    g_string_append (to_sign, key_iter->data);
-    g_string_append (to_sign, g_hash_table_lookup (hash, key_iter->data));
-  }
-
-  api_sig = g_compute_checksum_for_string (G_CHECKSUM_MD5, to_sign->str, -1);
-  g_hash_table_unref (hash);
-  g_list_free (keys);
-  g_string_free (to_sign, TRUE);
-
-  return api_sig;
-}
-
-static gchar *
-get_xpath_element (const gchar *content,
-                   const gchar *xpath_element)
-{
-  gchar *element = NULL;
-  xmlDocPtr xmldoc = NULL;
-  xmlXPathContextPtr xpath_ctx = NULL;
-  xmlXPathObjectPtr xpath_res = NULL;
-
-  xmldoc = xmlReadMemory (content, xmlStrlen ((xmlChar *) content), NULL, NULL,
-                          XML_PARSE_RECOVER | XML_PARSE_NOBLANKS);
-  if (xmldoc) {
-    xpath_ctx = xmlXPathNewContext (xmldoc);
-    if (xpath_ctx) {
-      xpath_res = xmlXPathEvalExpression ((xmlChar *) xpath_element, xpath_ctx);
-      if (xpath_res && xpath_res->nodesetval->nodeTab) {
-        element =
-          (gchar *) xmlNodeListGetString (xmldoc,
-                                          xpath_res->nodesetval->nodeTab[0]->xmlChildrenNode,
-                                          1);
-      }
-    }
-  }
-
-  /* Free data */
-  if (xmldoc) {
-    xmlFreeDoc (xmldoc);
-  }
-
-  if (xpath_ctx) {
-    xmlXPathFreeContext (xpath_ctx);
-  }
-
-  if (xpath_res) {
-    xmlXPathFreeObject (xpath_res);
-  }
-
-  return element;
-}
-
 gchar *
 grl_flickr_get_frob (const gchar *api_key,
                      const gchar *secret)



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