summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorRoss Burton <ross@linux.intel.com>2009-05-24 11:36:04 +0100
committerRoss Burton <ross@linux.intel.com>2009-05-24 21:25:02 +0100
commit31562649ec63448ad5b2ce05e99acf05e779449f (patch)
tree2dc965ef0acfac3df9f3e71428d445d073765607 /examples
parent4058853009971e158d3cc1fab7d93f9e857fe1a7 (diff)
Display the friend's status updates
Diffstat (limited to 'examples')
-rw-r--r--examples/test-facebook.c97
1 files changed, 86 insertions, 11 deletions
diff --git a/examples/test-facebook.c b/examples/test-facebook.c
index dac21e1..1a867ba 100644
--- a/examples/test-facebook.c
+++ b/examples/test-facebook.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
#include <rest/facebook-proxy.h>
#include <rest/rest-xml-parser.h>
@@ -27,18 +28,63 @@ get_xml (RestProxyCall *call)
return root;
}
+static GHashTable *user_hash;
+
+static void
+add_user (const char *uid, const char *name)
+{
+ int real_uid = atoi (uid);
+ g_hash_table_insert (user_hash,
+ GINT_TO_POINTER (real_uid),
+ g_strdup (name));
+}
+
+static void
+add_users (RestXmlNode *root)
+{
+ RestXmlNode *node;
+
+ for (node = rest_xml_node_find (root, "user"); node; node = node->next) {
+ add_user (rest_xml_node_find (node, "uid")->content,
+ rest_xml_node_find (node, "name")->content);
+ }
+}
+
+static const char *
+get_username (const char *uid)
+{
+ int real_uid = atoi (uid);
+ return g_hash_table_lookup (user_hash, GINT_TO_POINTER (real_uid));
+}
+
+static void
+print_statuses (RestXmlNode *root)
+{
+ RestXmlNode *node, *msg;
+ const char *name;
+
+ for (node = rest_xml_node_find (root, "status"); node; node = node->next) {
+ name = get_username (rest_xml_node_find (node, "uid")->content);
+ msg = rest_xml_node_find (node, "message");
+ g_print ("%s %s\n", name, msg->content);
+ }
+}
+
int
main (int argc, char **argv)
{
RestProxy *proxy;
RestProxyCall *call;
RestXmlNode *root, *node;
- const char *secret, *session_key;
- char *token, *url, *name;
+ const char *secret, *session_key, *name;
+ char *token, *url, *fql, *uid;
+ time_t cutoff;
g_thread_init (NULL);
g_type_init ();
+ user_hash = g_hash_table_new (NULL, NULL);
+
proxy = facebook_proxy_new ("9632214752c7dfb3a84890fbb6846dad",
"9dfdb14b9f110e0b14bb0607a14caefa");
@@ -96,9 +142,12 @@ main (int argc, char **argv)
g_error ("Cannot get user info");
root = get_xml (call);
+ node = rest_xml_node_find (root, "uid");
+ uid = g_strdup (node->content);
node = rest_xml_node_find (root, "name");
- name = g_strdup (node->content);
- g_print ("Logged in as %s\n", name);
+ name = node->content;
+ g_print ("Logged in as %s [%s]\n", name, uid);
+ add_user (uid, name);
rest_xml_node_unref (root);
/* Get the user status messages */
@@ -110,14 +159,40 @@ main (int argc, char **argv)
g_error ("Cannot get statuses");
root = get_xml (call);
- {
- RestXmlNode *node, *msg;
- for (node = rest_xml_node_find (root, "status"); node; node = node->next) {
- msg = rest_xml_node_find (node, "message");
- g_print ("%s %s\n", name, msg->content);
- }
- }
+ print_statuses (root);
+ rest_xml_node_unref (root);
+
+ /* Get the friend's names */
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "fql.query");
+ fql = g_strdup_printf ("SELECT uid,name FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1= %s)", uid);
+ rest_proxy_call_add_param (call, "query", fql);
+ g_free (fql);
+
+ if (!rest_proxy_call_run (call, NULL, NULL))
+ g_error ("Cannot get statuses");
+
+ root = get_xml (call);
+ add_users (root);
rest_xml_node_unref (root);
+ /* Get the friends status messages */
+ call = rest_proxy_new_call (proxy);
+ rest_proxy_call_set_function (call, "fql.query");
+ cutoff = time (NULL);
+ /* Get posts in the last 10 days */
+ cutoff -= 10 * 24 * 60 * 60;
+ fql = g_strdup_printf ("SELECT uid,status_id,message FROM status WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = %s) AND time > %d ORDER BY time", uid, cutoff);
+ rest_proxy_call_add_param (call, "query", fql);
+ g_free (fql);
+
+ if (!rest_proxy_call_run (call, NULL, NULL))
+ g_error ("Cannot get statuses");
+
+ root = get_xml (call);
+ print_statuses (root);
+ rest_xml_node_unref (root);
+
+
return 0;
}