[gnome-online-miners] src: add a Facebook miner based on GFBGraph



commit 0c3ef74f4ec70b8dee91aefc0c611ecbddc0ccec
Author: Álvaro Peña <alvaropg gmail com>
Date:   Wed Nov 27 19:25:31 2013 +0100

    src: add a Facebook miner based on GFBGraph
    
    https://bugzilla.gnome.org/show_bug.cgi?id=701499

 configure.ac                  |    2 +
 src/Makefile.am               |   27 +++
 src/gom-facebook-miner-main.c |   33 ++++
 src/gom-facebook-miner.c      |  382 +++++++++++++++++++++++++++++++++++++++++
 src/gom-facebook-miner.h      |   69 ++++++++
 5 files changed, 513 insertions(+), 0 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index 6478404..6118643 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,6 +20,7 @@ AC_PROG_CC_C_O
 AC_HEADER_STDC
 
 GDATA_MIN_VERSION=0.13.3
+GFBGRAPH_MIN_VERSION=0.2
 GLIB_MIN_VERSION=2.35.1
 GOA_MIN_VERSION=3.2.0
 GRILO_MIN_VERSION=0.2.6
@@ -33,6 +34,7 @@ GNOME_MAINTAINER_MODE_DEFINES
 LT_INIT
 
 PKG_CHECK_MODULES(GDATA, [libgdata >= $GDATA_MIN_VERSION])
+PKG_CHECK_MODULES(GFBGRAPH, [libgfbgraph-0.2 >= $GFBGRAPH_MIN_VERSION])
 PKG_CHECK_MODULES(GLIB, [glib-2.0 >= $GLIB_MIN_VERSION])
 PKG_CHECK_MODULES(GIO, [gio-2.0])
 
diff --git a/src/Makefile.am b/src/Makefile.am
index 5ede6cc..2741cef 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -31,12 +31,39 @@ libgom_1_0_la_LDFLAGS = \
     $(NULL)
 
 libexec_PROGRAMS = \
+    gom-facebook-miner \
     gom-flickr-miner \
     gom-gdata-miner \
     gom-owncloud-miner \
     gom-zpj-miner \
     $(NULL)
 
+gom_facebook_miner_SOURCES = \
+    gom-facebook-miner-main.c \
+    gom-facebook-miner.c \
+    gom-facebook-miner.h \
+    $(NULL)
+
+gom_facebook_miner_CPPFLAGS = \
+    -DG_LOG_DOMAIN=\"Gom\" \
+    -DG_DISABLE_DEPRECATED \
+    -I$(top_srcdir)/src \
+    $(GFBGRAPH_CFLAGS) \
+    $(GIO_CFLAGS) \
+    $(GLIB_CFLAGS) \
+    $(GOA_CFLAGS) \
+    $(TRACKER_CFLAGS) \
+    $(NULL)
+
+gom_facebook_miner_LDADD = \
+    libgom-1.0.la  \
+    $(GFBGRAPH_LIBS) \
+    $(GIO_LIBS) \
+    $(GLIB_LIBS) \
+    $(GOA_LIBS) \
+    $(TRACKER_LIBS) \
+    $(NULL)
+
 gom_flickr_miner_SOURCES = \
     gom-flickr-miner-main.c \
     gom-flickr-miner.c \
diff --git a/src/gom-facebook-miner-main.c b/src/gom-facebook-miner-main.c
new file mode 100644
index 0000000..f65336f
--- /dev/null
+++ b/src/gom-facebook-miner-main.c
@@ -0,0 +1,33 @@
+/*
+ * GNOME Online Miners - crawls through your online content
+ * Copyright (c) 2013 Álvaro Peña
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Author: Álvaro Peña <alvaropg gmail com>
+ *
+ */
+
+#include "config.h"
+
+#define INSIDE_MINER
+#define MINER_NAME "FACEBOOK"
+#define MINER_TYPE GOM_TYPE_FACEBOOK_MINER
+#define MINER_BUS_NAME "org.gnome.OnlineMiners.Facebook"
+#define MINER_OBJECT_PATH "/org/gnome/OnlineMiners/Facebook"
+
+#include "gom-facebook-miner.h"
+#include "gom-miner-main.c"
diff --git a/src/gom-facebook-miner.c b/src/gom-facebook-miner.c
new file mode 100644
index 0000000..a85738b
--- /dev/null
+++ b/src/gom-facebook-miner.c
@@ -0,0 +1,382 @@
+/*
+ * GNOME Online Miners - crawls through your online content
+ * Copyright (c) 2013 Álvaro Peña
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Author: Álvaro Peña <alvaropg gmail com>
+ *
+ */
+
+#include "config.h"
+
+#include <goa/goa.h>
+#include <gfbgraph/gfbgraph.h>
+#include <gfbgraph/gfbgraph-goa-authorizer.h>
+
+#include "gom-facebook-miner.h"
+
+#define MINER_IDENTIFIER "gd:facebook:miner:9972c7ff-a30f-4dd4-bc77-1adf9dd14364"
+
+G_DEFINE_TYPE (GomFacebookMiner, gom_facebook_miner, GOM_TYPE_MINER)
+
+static gboolean
+account_miner_job_process_photo (GomAccountMinerJob *job,
+                                 GFBGraphPhoto *photo,
+                                 const gchar *parent_resource_urn,
+                                 const gchar *creator,
+                                 GError **error)
+{
+  const gchar *photo_id;
+  const gchar *photo_name;
+  const gchar *photo_created_time;
+  const gchar *photo_link;
+  gchar *identifier;
+  const gchar *class = "nmm:Photo";
+  gchar *resource = NULL;
+  gboolean resource_exists;
+  gchar *contact_resource;
+
+  photo_id = gfbgraph_node_get_id (GFBGRAPH_NODE (photo));
+  photo_link = gfbgraph_node_get_link (GFBGRAPH_NODE (photo));
+  photo_created_time = gfbgraph_node_get_created_time (GFBGRAPH_NODE (photo));
+  photo_name = gfbgraph_photo_get_name (photo);
+  if (photo_name == NULL)
+    photo_name = photo_created_time;
+
+  identifier = g_strdup_printf ("facebook:%s", photo_id);
+
+  /* remove from the list of the previous resources */
+  g_hash_table_remove (job->previous_resources, identifier);
+
+  resource = gom_tracker_sparql_connection_ensure_resource
+    (job->connection,
+     job->cancellable, error,
+     &resource_exists,
+     job->datasource_urn, identifier,
+     "nfo:RemoteDataObject", class, NULL);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_update_datasource (job->connection, job->datasource_urn,
+                                 resource_exists, identifier, resource,
+                                 job->cancellable, error);
+  if (*error != NULL)
+    goto out;
+
+  /* TODO: Check updated time to avoid updating the photo if has not
+   * been modified since our last run
+   */
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:url", photo_link);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:isPartOf", parent_resource_urn);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:mimeType", "image/jpeg");
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:title", photo_name);
+
+  if (*error != NULL)
+    goto out;
+
+  contact_resource = gom_tracker_utils_ensure_contact_resource
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, creator);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nco:creator", contact_resource);
+
+  g_free (contact_resource);
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:contentCreated", photo_created_time);
+
+  if (*error != NULL)
+    goto out;
+
+ out:
+  g_free (resource);
+  g_free (identifier);
+
+  if (*error != NULL)
+    return FALSE;
+
+  return TRUE;
+}
+
+/* TODO: Until GFBGraph parse the "from" node section, we require the
+ *  album creator (generally the logged user)
+ */
+static gboolean
+account_miner_job_process_album (GomAccountMinerJob *job,
+                                 GFBGraphAlbum *album,
+                                 const gchar *creator,
+                                 GError **error)
+{
+  const gchar *album_id;
+  const gchar *album_name;
+  const gchar *album_description;
+  const gchar *album_link;
+  const gchar *album_created_time;
+  gchar *identifier;
+  const gchar *class = "nfo:DataContainer";
+  gchar *resource = NULL;
+  gboolean resource_exists;
+  gchar *contact_resource;
+  GList *l;
+  GList *photos = NULL;
+
+  album_id = gfbgraph_node_get_id (GFBGRAPH_NODE (album));
+  album_link = gfbgraph_node_get_link (GFBGRAPH_NODE (album));
+  album_created_time = gfbgraph_node_get_created_time (GFBGRAPH_NODE (album));
+  album_name = gfbgraph_album_get_name (album);
+  album_description = gfbgraph_album_get_description (album);
+  g_message ("%s", album_created_time);
+
+  identifier = g_strdup_printf ("photos:collection:facebook:%s", album_id);
+
+  /* remove from the list of the previous resources */
+  g_hash_table_remove (job->previous_resources, identifier);
+
+  resource = gom_tracker_sparql_connection_ensure_resource
+    (job->connection,
+     job->cancellable, error,
+     &resource_exists,
+     job->datasource_urn, identifier,
+     "nfo:RemoteDataObject", class,
+     NULL);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_update_datasource (job->connection, job->datasource_urn,
+                                 resource_exists, identifier, resource,
+                                 job->cancellable, error);
+
+  if (*error != NULL)
+    goto out;
+
+  /* TODO: Check updated time to avoid updating the album if has not
+   * been modified since our last run
+   */
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:url", album_link);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:description", album_description);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:title", album_name);
+
+  if (*error != NULL)
+    goto out;
+
+  contact_resource = gom_tracker_utils_ensure_contact_resource
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, creator);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nco:creator", contact_resource);
+  g_free (contact_resource);
+
+  if (*error != NULL)
+    goto out;
+
+  gom_tracker_sparql_connection_insert_or_replace_triple
+    (job->connection,
+     job->cancellable, error,
+     job->datasource_urn, resource,
+     "nie:contentCreated", album_created_time);
+
+  if (*error != NULL)
+    goto out;
+
+  /* Album photos */
+  photos = gfbgraph_node_get_connection_nodes (GFBGRAPH_NODE (album),
+                                               GFBGRAPH_TYPE_PHOTO,
+                                               GFBGRAPH_AUTHORIZER (job->service),
+                                               error);
+  if (*error != NULL)
+    goto out;
+
+  for (l = photos; l != NULL; l = l->next)
+    {
+      GError *local_error = NULL;
+      GFBGraphPhoto *photo = GFBGRAPH_PHOTO (l->data);
+
+      account_miner_job_process_photo (job, photo, resource, creator, &local_error);
+      if (local_error != NULL)
+        {
+          const gchar *photo_id;
+
+          photo_id = gfbgraph_node_get_id (GFBGRAPH_NODE (photo));
+          g_warning ("Unable to process %s: %s", photo_id, local_error->message);
+          g_clear_error (&local_error);
+        }
+    }
+
+ out:
+  g_free (resource);
+  g_free (identifier);
+
+  g_list_free_full (photos, g_object_unref);
+
+  if (*error != NULL)
+    return FALSE;
+
+  return TRUE;
+}
+
+static void
+query_facebook (GomAccountMinerJob *job,
+                GError **error)
+{
+  GFBGraphUser *me;
+  const gchar *me_name;
+  GList *albums = NULL;
+  GList *l = NULL;
+  GError *local_error = NULL;
+
+  me = gfbgraph_user_get_me (GFBGRAPH_AUTHORIZER (job->service), &local_error);
+  if (local_error != NULL)
+    goto out;
+
+  me_name = gfbgraph_user_get_name (me);
+
+  albums = gfbgraph_user_get_albums (me, GFBGRAPH_AUTHORIZER (job->service), &local_error);
+  if (local_error != NULL)
+    goto out;
+
+  for (l = albums; l != NULL; l = l->next)
+    {
+      GFBGraphAlbum *album = GFBGRAPH_ALBUM (l->data);
+
+      account_miner_job_process_album (job, album, me_name, &local_error);
+      if (local_error != NULL)
+        {
+          const gchar *album_id;
+
+          album_id = gfbgraph_node_get_id (GFBGRAPH_NODE (album));
+          g_warning ("Unable to process %s: %s", album_id, local_error->message);
+          g_clear_error (&local_error);
+        }
+    }
+
+ out:
+  if (local_error != NULL)
+    g_propagate_error (error, local_error);
+
+  g_list_free_full (albums, g_object_unref);
+  g_clear_object (&me);
+}
+
+static GObject *
+create_service (GomMiner *self,
+                GoaObject *object)
+{
+  GFBGraphGoaAuthorizer *authorizer;
+  GError *error = NULL;
+
+  authorizer = gfbgraph_goa_authorizer_new (object);
+
+  gfbgraph_authorizer_refresh_authorization (GFBGRAPH_AUTHORIZER (authorizer), NULL, &error);
+  if (error != NULL)
+    {
+      g_warning ("Error refreshing authorization (%d): %s", error->code, error->message);
+      g_error_free (error);
+    }
+
+  return G_OBJECT (authorizer);
+}
+
+static void
+gom_facebook_miner_init (GomFacebookMiner *miner)
+{
+}
+
+static void
+gom_facebook_miner_class_init (GomFacebookMinerClass *klass)
+{
+  GomMinerClass *miner_class = GOM_MINER_CLASS (klass);
+
+  miner_class->goa_provider_type = "facebook";
+  miner_class->miner_identifier = MINER_IDENTIFIER;
+  miner_class->version = 1;
+
+  miner_class->create_service = create_service;
+  miner_class->query = query_facebook;
+}
diff --git a/src/gom-facebook-miner.h b/src/gom-facebook-miner.h
new file mode 100644
index 0000000..5e6f0d1
--- /dev/null
+++ b/src/gom-facebook-miner.h
@@ -0,0 +1,69 @@
+/*
+ * GNOME Online Miners - crawls through your online content
+ * Copyright (c) 2013 Álvaro Peña
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * Author: Álvaro Peña <alvaropg gmail com>
+ *
+ */
+
+#ifndef __GOM_FACEBOOK_MINER_H__
+#define __GOM_FACEBOOK_MINER_H__
+
+#include <gio/gio.h>
+#include "gom-miner.h"
+
+G_BEGIN_DECLS
+
+#define GOM_TYPE_FACEBOOK_MINER gom_facebook_miner_get_type()
+
+#define GOM_FACEBOOK_MINER(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   GOM_TYPE_FACEBOOK_MINER, GomFacebookMiner))
+
+#define GOM_FACEBOOK_MINER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+   GOM_TYPE_FACEBOOK_MINER, GomFacebookMinerClass))
+
+#define GOM_IS_FACEBOOK_MINER(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   GOM_TYPE_FACEBOOK_MINER))
+
+#define GOM_IS_FACEBOOK_MINER_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+   GOM_TYPE_FACEBOOK_MINER))
+
+#define GOM_FACEBOOK_MINER_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+   GOM_TYPE_FACEBOOK_MINER, GomFacebookMinerClass))
+
+typedef struct _GomFacebookMiner GomFacebookMiner;
+typedef struct _GomFacebookMinerClass GomFacebookMinerClass;
+
+struct _GomFacebookMiner {
+  GomMiner parent;
+};
+
+struct _GomFacebookMinerClass {
+  GomMinerClass parent_class;
+};
+
+GType gom_facebook_miner_get_type(void);
+
+G_END_DECLS
+
+#endif /* __GOM_FACEBOOK_MINER_H__ */


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