Changeset e08ae0c for protocols


Ignore:
Timestamp:
2012-11-04T23:39:48Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
0688e99
Parents:
c08d201
Message:

Can log in now. Fails to fetch timelines though, which is going to be the
more annoying part anyway. Plus, I want streaming API stuff instead.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    rc08d201 re08ae0c  
    3737#include "xmltree.h"
    3838#include "twitter_lib.h"
     39#include "json_util.h"
    3940#include <ctype.h>
    4041#include <errno.h>
     
    188189}
    189190
    190 static struct xt_node *twitter_parse_response(struct im_connection *ic, struct http_request *req)
     191static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req)
    191192{
    192193        gboolean logging_in = !(ic->flags & OPT_LOGGED_IN);
    193194        gboolean periodic;
    194195        struct twitter_data *td = ic->proto_data;
    195         struct xt_node *ret;
     196        json_value *ret;
    196197        char path[64] = "", *s;
    197198       
     
    227228        }
    228229
    229         if ((ret = xt_from_string(req->reply_body, req->body_size)) == NULL) {
     230        if ((ret = json_parse(req->reply_body)) == NULL) {
    230231                imcb_error(ic, "Could not retrieve %s: %s",
    231232                           path, "XML parse error");
     
    251252
    252253/**
    253  * Function to help fill a list.
    254  */
    255 static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl)
    256 {
    257         char *end = NULL;
    258 
    259         if (node->text)
    260                 txl->next_cursor = g_ascii_strtoll(node->text, &end, 10);
    261         if (end == NULL)
    262                 txl->next_cursor = -1;
    263 
    264         return XT_HANDLED;
    265 }
    266 
    267 /**
    268254 * Fill a list of ids.
    269255 */
    270 static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl)
    271 {
    272         struct xt_node *child;
     256static xt_status twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl)
     257{
     258        json_value *c;
     259        int i;
    273260
    274261        // Set the list type.
    275262        txl->type = TXL_ID;
    276263
    277         // The root <statuses> node should hold the list of statuses <status>
    278         // Walk over the nodes children.
    279         for (child = node->children; child; child = child->next) {
    280                 if (g_strcasecmp("ids", child->name) == 0) {
    281                         struct xt_node *idc;
    282                         for (idc = child->children; idc; idc = idc->next)
    283                                 if (g_strcasecmp(idc->name, "id") == 0)
    284                                         txl->list = g_slist_prepend(txl->list,
    285                                                 g_memdup(idc->text, idc->text_len + 1));
    286                 } else if (g_strcasecmp("next_cursor", child->name) == 0) {
    287                         twitter_xt_next_cursor(child, txl);
    288                 }
    289         }
    290 
     264        c = json_o_get(node, "ids");
     265        if (!c || c->type != json_array)
     266                return XT_ABORT;
     267
     268        for (i = 0; i < c->u.array.length; i ++) {
     269                txl->list = g_slist_prepend(txl->list,
     270                        g_strdup_printf("%ld", c->u.array.values[i]->u.integer));
     271               
     272        }
     273       
     274        c = json_o_get(node, "next_cursor");
     275        if (c && c->type == json_integer)
     276                txl->next_cursor = c->u.integer;
     277        else
     278                txl->next_cursor = -1;
     279       
    291280        return XT_HANDLED;
    292281}
     
    300289{
    301290        struct im_connection *ic;
    302         struct xt_node *parsed;
     291        json_value *parsed;
    303292        struct twitter_xml_list *txl;
    304293        struct twitter_data *td;
     
    322311        if (!(parsed = twitter_parse_response(ic, req)))
    323312                return;
     313       
    324314        twitter_xt_get_friends_id_list(parsed, txl);
    325         xt_free_node(parsed);
     315        json_value_free(parsed);
    326316
    327317        td->follow_ids = txl->list;
     
    338328}
    339329
    340 static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl);
     330static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl);
    341331static void twitter_http_get_users_lookup(struct http_request *req);
    342332
     
    379369{
    380370        struct im_connection *ic = req->data;
    381         struct xt_node *parsed;
     371        json_value *parsed;
    382372        struct twitter_xml_list *txl;
    383373        GSList *l = NULL;
     
    395385                return;
    396386        twitter_xt_get_users(parsed, txl);
    397         xt_free_node(parsed);
     387        json_value_free(parsed);
    398388
    399389        // Add the users as buddies.
     
    410400
    411401/**
    412  * Function to fill a twitter_xml_user struct.
    413  * It sets:
    414  *  - the name and
    415  *  - the screen_name.
    416  */
    417 static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu)
    418 {
    419         struct xt_node *child;
    420 
    421         // Walk over the nodes children.
    422         for (child = node->children; child; child = child->next) {
    423                 if (g_strcasecmp("name", child->name) == 0) {
    424                         txu->name = g_memdup(child->text, child->text_len + 1);
    425                 } else if (g_strcasecmp("screen_name", child->name) == 0) {
    426                         txu->screen_name = g_memdup(child->text, child->text_len + 1);
    427                 }
    428         }
    429         return XT_HANDLED;
    430 }
    431 
    432 /**
    433402 * Function to fill a twitter_xml_list struct.
    434403 * It sets:
    435404 *  - all <user>s from the <users> element.
    436405 */
    437 static xt_status twitter_xt_get_users(struct xt_node *node, struct twitter_xml_list *txl)
     406static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl)
    438407{
    439408        struct twitter_xml_user *txu;
    440         struct xt_node *child;
     409        int i;
    441410
    442411        // Set the type of the list.
    443412        txl->type = TXL_USER;
    444413
     414        if (!node || node->type != json_array)
     415                return XT_ABORT;
     416
    445417        // The root <users> node should hold the list of users <user>
    446418        // Walk over the nodes children.
    447         for (child = node->children; child; child = child->next) {
    448                 if (g_strcasecmp("user", child->name) == 0) {
    449                         txu = g_new0(struct twitter_xml_user, 1);
    450                         twitter_xt_get_user(child, txu);
    451                         // Put the item in the front of the list.
    452                         txl->list = g_slist_prepend(txl->list, txu);
    453                 }
     419        for (i = 0; i < node->u.array.length; i ++) {
     420                txu = g_new0(struct twitter_xml_user, 1);
     421                txu->name = g_strdup(json_o_str(node->u.array.values[i], "name"));
     422                txu->screen_name = g_strdup(json_o_str(node->u.array.values[i], "screen_name"));
     423                txl->list = g_slist_prepend(txl->list, txu);
    454424        }
    455425
     
    491461                } else if (g_strcasecmp("user", child->name) == 0) {
    492462                        txs->user = g_new0(struct twitter_xml_user, 1);
    493                         twitter_xt_get_user(child, txs->user);
     463//                      twitter_xt_get_user(child, txs->user);
    494464                } else if (g_strcasecmp("id", child->name) == 0) {
    495465                        txs->id = g_ascii_strtoull(child->text, NULL, 10);
     
    579549                        }
    580550                } else if (g_strcasecmp("next_cursor", child->name) == 0) {
    581                         twitter_xt_next_cursor(child, txl);
     551//                      twitter_xt_next_cursor(child, txl);
    582552                }
    583553        }
     
    921891                goto end;
    922892        twitter_xt_get_status_list(ic, parsed, txl);
    923         xt_free_node(parsed);
     893//      json_value_free(parsed);
    924894
    925895        td->home_timeline_obj = txl;
     
    954924                goto end;
    955925        twitter_xt_get_status_list(ic, parsed, txl);
    956         xt_free_node(parsed);
     926//      json_value_free(parsed);
    957927
    958928        td->mentions_obj = txl;
Note: See TracChangeset for help on using the changeset viewer.