/soc/2015/nakulgulati/main: cc7cf62712c9: hangouts: util constru...
Nakul at rock.pidgin.im
Nakul at rock.pidgin.im
Sat Aug 8 19:49:26 EDT 2015
Changeset: cc7cf62712c9921e9cf8910de9b1ce5a436a2888
Author: Nakul Gulati
Date: 2015-08-09 07:48 +0800
Branch: hangouts
URL: https://hg.pidgin.im/soc/2015/nakulgulati/main/rev/cc7cf62712c9
Description:
hangouts: util construct json data dict from chat init data
diffstat:
libpurple/protocols/hangouts/api.c | 24 +++++++++++++++--
libpurple/protocols/hangouts/util.c | 49 ++++++++++++++++++++++++++++++++++++-
libpurple/protocols/hangouts/util.h | 6 ++++
3 files changed, 75 insertions(+), 4 deletions(-)
diffs (121 lines):
diff --git a/libpurple/protocols/hangouts/api.c b/libpurple/protocols/hangouts/api.c
--- a/libpurple/protocols/hangouts/api.c
+++ b/libpurple/protocols/hangouts/api.c
@@ -445,12 +445,30 @@ static void
hangouts_api_get_chat_init_cb (PurpleHttpConnection *http_conn,
PurpleHttpResponse *response, gpointer user_data)
{
- gchar *raw_res;
- GHashTable *tab;
+ const gchar *raw_res;
+ GHashTable *chat_init_tab;
+ JsonNode *root, *result;
+ JsonPath *path;
+ JsonArray *array;
raw_res = purple_http_response_get_data (response, NULL);
- tab = hangouts_util_parse (raw_res);
+ chat_init_tab = hangouts_util_parse (raw_res);
+ root = hangouts_util_construct_json_dictionary (chat_init_tab);
+
+ path = json_path_new ();
+ json_path_compile (path, "$.ds:7[0][2]", NULL);
+
+ result = json_path_match (path, root);
+ array = json_node_dup_array (result);
+ gchar *str = json_array_get_string_element (array, 0);
+
+ purple_debug_misc ("chat_init", "Results: %s\n", str);
+
+ g_hash_table_destroy (chat_init_tab);
+ g_object_unref (path);
+ json_node_free (root);
+ json_node_free (result);
}
void
diff --git a/libpurple/protocols/hangouts/util.c b/libpurple/protocols/hangouts/util.c
--- a/libpurple/protocols/hangouts/util.c
+++ b/libpurple/protocols/hangouts/util.c
@@ -22,6 +22,8 @@
#include <string.h>
#include <glib.h>
+#include <json-glib/json-glib.h>
+
#include "debug.h"
#include "util.h"
@@ -57,7 +59,8 @@ hangouts_util_parse (gchar *raw_data)
gchar *script_data = g_match_info_fetch (match_info, 0);
gchar **key_data = g_regex_split (regex_key_data, script_data, 0);
- g_hash_table_insert (chat_init_data, key_data[1], key_data[2]);
+ g_hash_table_insert (chat_init_data, g_strdup (key_data[1]),
+ g_strdup (key_data[2]));
g_match_info_next (match_info, NULL);
g_free (script_data);
@@ -70,3 +73,47 @@ hangouts_util_parse (gchar *raw_data)
return chat_init_data;
}
+
+void
+hangouts_util_add_node (gpointer key, gpointer value, gpointer data)
+{
+ JsonBuilder *builder;
+ JsonParser *parser;
+ JsonNode *node;
+ gboolean parse_success;
+
+ builder = data;
+ parser = json_parser_new ();
+
+ parse_success = json_parser_load_from_data (parser, g_strcompress (value), -1,
+ NULL);
+
+ if (!parse_success)
+ {
+ //TODO add null values to string where missing for valid json
+ return;
+ }
+
+ node = json_parser_get_root (parser);
+ json_builder_set_member_name (builder, g_strdup (key));
+ json_builder_add_value (builder, node);
+
+}
+
+JsonNode *
+hangouts_util_construct_json_dictionary (GHashTable *tab)
+{
+ JsonBuilder *builder;
+ JsonNode *root;
+
+ builder = json_builder_new ();
+
+ json_builder_begin_object (builder);
+ g_hash_table_foreach (tab, hangouts_util_add_node, builder);
+ json_builder_end_object (builder);
+
+ root = json_builder_get_root (builder);
+
+ g_object_unref (builder);
+ return root;
+}
diff --git a/libpurple/protocols/hangouts/util.h b/libpurple/protocols/hangouts/util.h
--- a/libpurple/protocols/hangouts/util.h
+++ b/libpurple/protocols/hangouts/util.h
@@ -33,4 +33,10 @@ hangouts_util_escape_string (gchar *stri
GHashTable *
hangouts_util_parse (gchar *raw_data);
+void
+hangouts_util_add_node (gpointer key, gpointer value, gpointer data);
+
+JsonNode *
+hangouts_util_construct_json_dictionary (GHashTable *tab);
+
#endif _UTIL_H
More information about the Commits
mailing list