Changeset ddc2de5 for protocols


Ignore:
Timestamp:
2012-11-10T23:52:21Z (8 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Branches:
master
Children:
dff0e0b
Parents:
8bd866f
Message:

Very immature code for reading from the streaming API. It reads from a
fixed URL and tried to parse individual JSON objects. Not doing anything
useful with it.

Location:
protocols/twitter
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • protocols/twitter/twitter.h

    r8bd866f rddc2de5  
    5757        guint64 last_status_id; /* For undo */
    5858        gint main_loop_id;
     59        struct http_request *stream;
    5960        struct groupchat *timeline_gc;
    6061        gint http_fails;
  • protocols/twitter/twitter_http.c

    r8bd866f rddc2de5  
    4747 * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c
    4848 */
    49 void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
    50                    gpointer data, int is_post, char **arguments, int arguments_len)
     49struct http_request *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
     50                                  gpointer data, int is_post, char **arguments, int arguments_len)
    5151{
    5252        struct twitter_data *td = ic->proto_data;
     
    5555        void *ret;
    5656        char *url_arguments;
     57        url_t *base_url = NULL;
    5758
    5859        url_arguments = g_strdup("");
     
    6768                }
    6869        }
     70       
     71        if (strstr(url_string, "://")) {
     72                base_url = g_new0(url_t, 1);
     73                if (!url_set(base_url, url_string)) {
     74                        g_free(base_url);
     75                        return NULL;
     76                }
     77        }
     78       
    6979        // Make the request.
    7080        g_string_printf(request, "%s %s%s%s%s HTTP/1.0\r\n"
     
    7282                        "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n",
    7383                        is_post ? "POST" : "GET",
    74                         td->url_path, url_string,
    75                         is_post ? "" : "?", is_post ? "" : url_arguments, td->url_host);
     84                        base_url ? base_url->file : td->url_path,
     85                        base_url ? "" : url_string,
     86                        is_post ? "" : "?", is_post ? "" : url_arguments,
     87                        base_url ? base_url->host : td->url_host);
    7688
    7789        // If a pass and user are given we append them to the request.
     
    8092                char *full_url;
    8193
    82                 full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url"), url_string, NULL);
     94                if (base_url)
     95                        full_url = g_strdup(url_string);
     96                else
     97                        full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url"), url_string, NULL);
    8398                full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET",
    8499                                                full_url, url_arguments);
     
    109124        }
    110125
    111         ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data);
     126        if (base_url)
     127                ret = http_dorequest(base_url->host, base_url->port, base_url->proto == PROTO_HTTPS, request->str, func, data);
     128        else
     129                ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data);
    112130
    113131        g_free(url_arguments);
    114132        g_string_free(request, TRUE);
     133        g_free(base_url);
    115134        return ret;
    116135}
  • protocols/twitter/twitter_http.h

    r8bd866f rddc2de5  
    3030struct oauth_info;
    3131
    32 void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
    33                    gpointer data, int is_post, char** arguments, int arguments_len);
     32struct http_request *twitter_http(struct im_connection *ic, char *url_string, http_input_function func,
     33                                  gpointer data, int is_post, char** arguments, int arguments_len);
    3434
    3535#endif //_TWITTER_HTTP_H
  • protocols/twitter/twitter_lib.c

    r8bd866f rddc2de5  
    727727}
    728728
    729 static void twitter_http_get_home_timeline(struct http_request *req);
    730 static void twitter_http_get_mentions(struct http_request *req);
     729static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
     730static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
     731
     732static void twitter_http_stream(struct http_request *req)
     733{
     734        int len;
     735        int i;
     736        char c;
     737        json_value *j;
     738       
     739        printf( "%d bytes in stream\n", req->body_size );
     740       
     741        /* m/^[\d\s]*/      /* why is there a second commend here? :-) */
     742        for (i = 0; i < req->body_size; i ++) {
     743                if (!isspace(req->reply_body[i]) && !isdigit(req->reply_body[i]))
     744                        break;
     745        }
     746       
     747        /* Nothing but numbers and whitespace in there. Try again later. */
     748        if (i == req->body_size)
     749                return;
     750       
     751        /* Get length. */
     752        if (sscanf(req->reply_body, "%d", &len) != 1)
     753                return;
     754       
     755        if (req->body_size < i + len) {
     756                printf("Not enough bytes in buffer yet\n");
     757                return;
     758        }
     759       
     760        http_flush_bytes(req, i);
     761        c = req->reply_body[len];
     762        req->reply_body[len] = '\0';
     763       
     764        printf("JSON: %s\n", req->reply_body);
     765        printf("parsed: %p\n", (j = json_parse(req->reply_body)));
     766        json_value_free(j);
     767        req->reply_body[len] = c;
     768       
     769        http_flush_bytes(req, len);
     770}
    731771
    732772/**
     
    751791        if (include_mentions) {
    752792                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;
    753810        }
    754811}
     
    810867}
    811868
     869static void twitter_http_get_home_timeline(struct http_request *req);
     870static void twitter_http_get_mentions(struct http_request *req);
     871
    812872/**
    813873 * Get the timeline.
    814874 */
    815 void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
     875static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor)
    816876{
    817877        struct twitter_data *td = ic->proto_data;
     
    849909 * Get mentions.
    850910 */
    851 void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor)
     911static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor)
    852912{
    853913        struct twitter_data *td = ic->proto_data;
  • protocols/twitter/twitter_lib.h

    r8bd866f rddc2de5  
    8181void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor);
    8282void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor);
    83 void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor);
    84 void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor);
    8583void twitter_get_statuses_friends(struct im_connection *ic, gint64 next_cursor);
    8684
Note: See TracChangeset for help on using the changeset viewer.