Changeset 8e3b7ac for protocols/twitter


Ignore:
Timestamp:
2012-11-08T22:38:20Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
fb351ce
Parents:
0688e99
Message:

It logs in and fetches statuses! \o/ But, some corruption..

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    r0688e99 r8e3b7ac  
    272272               
    273273                txl->list = g_slist_prepend(txl->list,
    274                         g_strdup_printf("%ld", c->u.array.values[i]->u.integer));
     274                        g_strdup_printf("%lld", c->u.array.values[i]->u.integer));
    275275               
    276276        }
     
    403403}
    404404
     405struct twitter_xml_user *twitter_xt_get_user(const json_value *node)
     406{
     407        struct twitter_xml_user *txu;
     408       
     409        txu = g_new0(struct twitter_xml_user, 1);
     410        txu->name = g_strdup(json_o_str(node, "name"));
     411        txu->screen_name = g_strdup(json_o_str(node, "screen_name"));
     412       
     413        return txu;
     414}
     415
    405416/**
    406417 * Function to fill a twitter_xml_list struct.
     
    422433        // Walk over the nodes children.
    423434        for (i = 0; i < node->u.array.length; i ++) {
    424                 txu = g_new0(struct twitter_xml_user, 1);
    425                 txu->name = g_strdup(json_o_str(node->u.array.values[i], "name"));
    426                 txu->screen_name = g_strdup(json_o_str(node->u.array.values[i], "screen_name"));
    427                 txl->list = g_slist_prepend(txl->list, txu);
     435                txu = twitter_xt_get_user(node->u.array.values[i]);
     436                if (txu)
     437                        txl->list = g_slist_prepend(txl->list, txu);
    428438        }
    429439
     
    445455 *  - the user in a twitter_xml_user struct.
    446456 */
    447 static xt_status twitter_xt_get_status(struct xt_node *node, struct twitter_xml_status *txs)
    448 {
    449         struct xt_node *child, *rt = NULL;
    450 
    451         // Walk over the nodes children.
    452         for (child = node->children; child; child = child->next) {
    453                 if (g_strcasecmp("text", child->name) == 0) {
    454                         txs->text = g_memdup(child->text, child->text_len + 1);
    455                 } else if (g_strcasecmp("retweeted_status", child->name) == 0) {
    456                         rt = child;
    457                 } else if (g_strcasecmp("created_at", child->name) == 0) {
     457static xt_status twitter_xt_get_status(const json_value *node, struct twitter_xml_status *txs)
     458{
     459        const json_value *c, *rt = NULL, *entities = NULL;
     460        int i;
     461       
     462        if (node->type != json_object)
     463                return XT_ABORT;
     464
     465        for (i = 0; i < node->u.object.length; i ++) {
     466                const char *k = node->u.object.values[i].name;
     467                const json_value *v = node->u.object.values[i].value;
     468               
     469                if (strcmp("text", k) == 0 && v->type == json_string) {
     470                        txs->text = g_memdup(v->u.string.ptr, v->u.string.length);
     471                } else if (strcmp("retweeted_status", k) == 0 && v->type == json_object) {
     472                        rt = v;
     473                } else if (strcmp("created_at", k) == 0 && v->type == json_string) {
    458474                        struct tm parsed;
    459475
     
    461477                           this field. :-( Also assumes the timezone used
    462478                           is UTC since C time handling functions suck. */
    463                         if (strptime(child->text, TWITTER_TIME_FORMAT, &parsed) != NULL)
     479                        if (strptime(v->u.string.ptr, TWITTER_TIME_FORMAT, &parsed) != NULL)
    464480                                txs->created_at = mktime_utc(&parsed);
    465                 } else if (g_strcasecmp("user", child->name) == 0) {
    466                         txs->user = g_new0(struct twitter_xml_user, 1);
    467 //                      twitter_xt_get_user(child, txs->user);
    468                 } else if (g_strcasecmp("id", child->name) == 0) {
    469                         txs->id = g_ascii_strtoull(child->text, NULL, 10);
    470                 } else if (g_strcasecmp("in_reply_to_status_id", child->name) == 0) {
    471                         txs->reply_to = g_ascii_strtoull(child->text, NULL, 10);
     481                } else if (strcmp("user", k) == 0 && v->type == json_object) {
     482                        txs->user = twitter_xt_get_user(v);
     483                } else if (strcmp("id", k) == 0 && v->type == json_integer) {
     484                        txs->id = v->u.integer;
     485                } else if (strcmp("in_reply_to_status_id", k) == 0 && v->type == json_integer) {
     486                        txs->reply_to = v->u.integer;
     487                } else if (strcmp("entities", k) == 0 && v->type == json_object) {
     488                        txs->reply_to = v->u.integer;
    472489                }
    473490        }
     
    485502                txs->text = g_strdup_printf("RT @%s: %s", rtxs->user->screen_name, rtxs->text);
    486503                txs_free(rtxs);
    487         } else {
    488                 struct xt_node *urls, *url;
    489                
    490                 urls = xt_find_path(node, "entities");
    491                 if (urls != NULL)
    492                         urls = urls->children;
    493                 for (; urls; urls = urls->next) {
    494                         if (strcmp(urls->name, "urls") != 0 && strcmp(urls->name, "media") != 0)
     504        } else if (entities && NULL) {
     505                JSON_O_FOREACH (entities, k, v) {
     506                        int i;
     507                       
     508                        if (v->type != json_array)
     509                                continue;
     510                        if (strcmp(k, "urls") != 0 && strcmp(k, "media") != 0)
    495511                                continue;
    496512                       
    497                         for (url = urls ? urls->children : NULL; url; url = url->next) {
    498                                 /* "short" is a reserved word. :-P */
    499                                 struct xt_node *kort = xt_find_node(url->children, "url");
    500                                 struct xt_node *disp = xt_find_node(url->children, "display_url");
     513                        for (i = 0; i < v->u.array.length; i ++) {
     514                                if (v->u.array.values[i]->type != json_object)
     515                                        continue;
     516                               
     517                                const char *kort = json_o_str(v->u.array.values[i], "url");
     518                                const char *disp = json_o_str(v->u.array.values[i], "display_url");
    501519                                char *pos, *new;
    502520                               
    503                                 if (!kort || !kort->text || !disp || !disp->text ||
    504                                     !(pos = strstr(txs->text, kort->text)))
     521                                if (!kort || !disp || !(pos = strstr(txs->text, kort)))
    505522                                        continue;
    506523                               
    507524                                *pos = '\0';
    508                                 new = g_strdup_printf("%s%s &lt;%s&gt;%s", txs->text, kort->text,
    509                                                       disp->text, pos + strlen(kort->text));
     525                                new = g_strdup_printf("%s%s &lt;%s&gt;%s", txs->text, kort,
     526                                                      disp, pos + strlen(kort));
    510527                               
    511528                                g_free(txs->text);
     
    524541 *  - the next_cursor.
    525542 */
    526 static xt_status twitter_xt_get_status_list(struct im_connection *ic, struct xt_node *node,
     543static xt_status twitter_xt_get_status_list(struct im_connection *ic, const json_value *node,
    527544                                            struct twitter_xml_list *txl)
    528545{
    529546        struct twitter_xml_status *txs;
    530         struct xt_node *child;
     547        json_value *c;
    531548        bee_user_t *bu;
     549        int i;
    532550
    533551        // Set the type of the list.
    534552        txl->type = TXL_STATUS;
     553       
     554        if (node->type != json_array)
     555                return XT_ABORT;
    535556
    536557        // The root <statuses> node should hold the list of statuses <status>
    537558        // Walk over the nodes children.
    538         for (child = node->children; child; child = child->next) {
    539                 if (g_strcasecmp("status", child->name) == 0) {
    540                         txs = g_new0(struct twitter_xml_status, 1);
    541                         twitter_xt_get_status(child, txs);
    542                         // Put the item in the front of the list.
    543                         txl->list = g_slist_prepend(txl->list, txs);
    544 
    545                         if (txs->user && txs->user->screen_name &&
    546                             (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
    547                                 struct twitter_user_data *tud = bu->data;
    548 
    549                                 if (txs->id > tud->last_id) {
    550                                         tud->last_id = txs->id;
    551                                         tud->last_time = txs->created_at;
    552                                 }
     559        for (i = 0; i < node->u.array.length; i ++) {
     560                txs = g_new0(struct twitter_xml_status, 1);
     561                twitter_xt_get_status(node->u.array.values[i], txs);
     562                // Put the item in the front of the list.
     563                txl->list = g_slist_prepend(txl->list, txs);
     564
     565                if (txs->user && txs->user->screen_name &&
     566                    (bu = bee_user_by_handle(ic->bee, ic, txs->user->screen_name))) {
     567                        struct twitter_user_data *tud = bu->data;
     568
     569                        if (txs->id > tud->last_id) {
     570                                tud->last_id = txs->id;
     571                                tud->last_time = txs->created_at;
    553572                        }
    554                 } else if (g_strcasecmp("next_cursor", child->name) == 0) {
    555 //                      twitter_xt_next_cursor(child, txl);
    556573                }
    557574        }
     
    879896        struct im_connection *ic = req->data;
    880897        struct twitter_data *td;
    881         struct xt_node *parsed;
     898        json_value *parsed;
    882899        struct twitter_xml_list *txl;
    883900
     
    912929        struct im_connection *ic = req->data;
    913930        struct twitter_data *td;
    914         struct xt_node *parsed;
     931        json_value *parsed;
    915932        struct twitter_xml_list *txl;
    916933
Note: See TracChangeset for help on using the changeset viewer.