Changeset dff0e0b for protocols


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.

Location:
protocols/twitter
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.c

    rddc2de5 rdff0e0b  
    55*                                                                           *
    66*  Copyright 2009 Geert Mulders <g.c.w.m.mulders@gmail.com>                 *
     7*  Copyright 2010-2012 Wilmer van der Gaast <wilmer@gaast.net>              *
    78*                                                                           *
    89*  This library is free software; you can redistribute it and/or            *
     
    6667        // Run this once. After this queue the main loop function.
    6768        twitter_main_loop(ic, -1, 0);
    68 
    69         // Queue the main_loop
    70         // Save the return value, so we can remove the timeout on logout.
    71         td->main_loop_id =
    72             b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000, twitter_main_loop, ic);
     69       
     70        if (set_getbool(&ic->acc->set, "stream")) {
     71                /* That fetch was just to get backlog, the stream will give
     72                   us the rest. \o/ */
     73                twitter_open_stream(ic);
     74        } else {
     75                /* Not using the streaming API, so keep polling the old-
     76                   fashioned way. :-( */
     77                td->main_loop_id =
     78                    b_timeout_add(set_getint(&ic->acc->set, "fetch_interval") * 1000,
     79                                  twitter_main_loop, ic);
     80        }
    7381}
    7482
     
    279287
    280288        s = set_add(&acc->set, "strip_newlines", "false", set_eval_bool, acc);
     289       
     290        s = set_add(&acc->set, "stream", "true", set_eval_bool, acc);
     291        s->flags |= ACC_SET_OFFLINE_ONLY;
    281292}
    282293
  • 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}
  • protocols/twitter/twitter_lib.h

    rddc2de5 rdff0e0b  
    7979#define TWITTER_REPORT_SPAM_URL "/report_spam.json"
    8080
     81#define TWITTER_USER_STREAM_URL "https://userstream.twitter.com/1.1/user.json"
     82
     83gboolean twitter_open_stream(struct im_connection *ic);
    8184void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor);
    8285void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor);
Note: See TracChangeset for help on using the changeset viewer.