Ignore:
Timestamp:
2012-11-11T14:42:20Z (11 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
62f6b45
Parents:
ddc2de5
Message:

Showing tweets now, and leaking less memory. Still lots of cleanup left
to do.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter_lib.c

    rddc2de5 rdff0e0b  
    176176                root = json_parse(req->reply_body);
    177177                err = json_o_get(root, "errors");
    178                 if (err->type == json_array && (err = err->u.array.values[0]) &&
     178                if (err && err->type == json_array && (err = err->u.array.values[0]) &&
    179179                    err->type == json_object) {
    180180                        const char *msg = json_o_str(err, "message");
     
    529529        }
    530530
    531         return TRUE;
     531        return txs->text && txs->user && txs->id;
    532532}
    533533
     
    727727}
    728728
    729 static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
    730 static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
     729static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o);
    731730
    732731static void twitter_http_stream(struct http_request *req)
    733732{
    734         int len;
    735         int i;
     733        struct im_connection *ic = req->data;
     734        //struct twitter_data *td = ic->proto_data;
     735        json_value *parsed;
     736        int len, i;
    736737        char c;
    737         json_value *j;
     738       
     739        if (!g_slist_find(twitter_connections, ic))
     740                return;
     741        //td = ic->proto_data;;
    738742       
    739743        printf( "%d bytes in stream\n", req->body_size );
     
    763767       
    764768        printf("JSON: %s\n", req->reply_body);
    765         printf("parsed: %p\n", (j = json_parse(req->reply_body)));
    766         json_value_free(j);
     769        printf("parsed: %p\n", (parsed = json_parse(req->reply_body)));
     770        if (parsed) {
     771                twitter_stream_handle_object(ic, parsed);
     772        }
     773        json_value_free(parsed);
    767774        req->reply_body[len] = c;
    768775       
    769776        http_flush_bytes(req, len);
    770 }
     777       
     778        /* One notification might bring multiple events! */
     779        if (req->body_size > 0)
     780                twitter_http_stream(req);
     781}
     782
     783static gboolean twitter_stream_handle_object(struct im_connection *ic, json_value *o)
     784{
     785        struct twitter_xml_status *txs = g_new0(struct twitter_xml_status, 1);
     786       
     787        if (twitter_xt_get_status(o, txs)) {
     788                GSList *output = g_slist_append(NULL, txs);
     789                twitter_groupchat(ic, output);
     790                txs_free(txs);
     791                g_slist_free(output);
     792                return TRUE;
     793        }
     794        txs_free(txs);
     795        return FALSE;
     796}
     797
     798gboolean twitter_open_stream(struct im_connection *ic)
     799{
     800        struct twitter_data *td = ic->proto_data;
     801        char *args[4] = {"with", "followings", "delimited", "length"};
     802       
     803        if ((td->stream = twitter_http(ic, TWITTER_USER_STREAM_URL,
     804                                       twitter_http_stream, ic, 0, args, 4))) {
     805                /* This flag must be enabled or we'll get no data until EOF
     806                   (which err, kind of, defeats the purpose of a streaming API). */
     807                td->stream->flags |= HTTPC_STREAMING;
     808                return TRUE;
     809        }
     810       
     811        return FALSE;
     812}
     813
     814static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
     815static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
    771816
    772817/**
     
    791836        if (include_mentions) {
    792837                twitter_get_mentions(ic, next_cursor);
    793         }
    794        
    795         static int bla = 0;
    796        
    797         if (bla)
    798                 return;
    799         bla = 1;
    800        
    801         char *args[4];
    802         args[0] = "with";
    803         args[1] = "followings";
    804         args[2] = "delimited";
    805         args[3] = "length";
    806        
    807         if ((td->stream = twitter_http(ic, "https://userstream.twitter.com/1.1/user.json",
    808                                        twitter_http_stream, ic, 0, args, 4))) {
    809                 td->stream->flags |= HTTPC_STREAMING;
    810838        }
    811839}
Note: See TracChangeset for help on using the changeset viewer.