diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2012-11-10 23:52:21 +0000 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2012-11-10 23:52:21 +0000 |
commit | ddc2de54664ec25b95bbce997fbbb6a7104f1203 (patch) | |
tree | f1f76bf48e9e5ca4eeb6eaa0b84687253f0f78f3 | |
parent | 8bd866f3d336cb695006bc48592cf7475139aa36 (diff) |
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.
-rw-r--r-- | lib/http_client.c | 1 | ||||
-rw-r--r-- | protocols/twitter/twitter.h | 1 | ||||
-rw-r--r-- | protocols/twitter/twitter_http.c | 31 | ||||
-rw-r--r-- | protocols/twitter/twitter_http.h | 4 | ||||
-rw-r--r-- | protocols/twitter/twitter_lib.c | 68 | ||||
-rw-r--r-- | protocols/twitter/twitter_lib.h | 2 |
6 files changed, 93 insertions, 14 deletions
diff --git a/lib/http_client.c b/lib/http_client.c index 17e21a49..acbf230c 100644 --- a/lib/http_client.c +++ b/lib/http_client.c @@ -281,6 +281,7 @@ static gboolean http_incoming_data( gpointer data, int source, b_input_condition memcpy( req->sbuf + req->sblen, buffer, st ); req->bytes_read += st; req->sblen += st; + req->sbuf[req->sblen] = '\0'; req->reply_body = req->sbuf + pos; req->body_size = req->sblen - pos; } diff --git a/protocols/twitter/twitter.h b/protocols/twitter/twitter.h index 14e43824..f2afb754 100644 --- a/protocols/twitter/twitter.h +++ b/protocols/twitter/twitter.h @@ -56,6 +56,7 @@ struct twitter_data guint64 last_status_id; /* For undo */ gint main_loop_id; + struct http_request *stream; struct groupchat *timeline_gc; gint http_fails; twitter_flags_t flags; diff --git a/protocols/twitter/twitter_http.c b/protocols/twitter/twitter_http.c index dbac5461..216dad9e 100644 --- a/protocols/twitter/twitter_http.c +++ b/protocols/twitter/twitter_http.c @@ -46,14 +46,15 @@ static char *twitter_url_append(char *url, char *key, char *value); * Do a request. * This is actually pretty generic function... Perhaps it should move to the lib/http_client.c */ -void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, - gpointer data, int is_post, char **arguments, int arguments_len) +struct http_request *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, + gpointer data, int is_post, char **arguments, int arguments_len) { struct twitter_data *td = ic->proto_data; char *tmp; GString *request = g_string_new(""); void *ret; char *url_arguments; + url_t *base_url = NULL; url_arguments = g_strdup(""); @@ -66,20 +67,34 @@ void *twitter_http(struct im_connection *ic, char *url_string, http_input_functi url_arguments = tmp; } } + + if (strstr(url_string, "://")) { + base_url = g_new0(url_t, 1); + if (!url_set(base_url, url_string)) { + g_free(base_url); + return NULL; + } + } + // Make the request. g_string_printf(request, "%s %s%s%s%s HTTP/1.0\r\n" "Host: %s\r\n" "User-Agent: BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\r\n", is_post ? "POST" : "GET", - td->url_path, url_string, - is_post ? "" : "?", is_post ? "" : url_arguments, td->url_host); + base_url ? base_url->file : td->url_path, + base_url ? "" : url_string, + is_post ? "" : "?", is_post ? "" : url_arguments, + base_url ? base_url->host : td->url_host); // If a pass and user are given we append them to the request. if (td->oauth_info) { char *full_header; char *full_url; - full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url"), url_string, NULL); + if (base_url) + full_url = g_strdup(url_string); + else + full_url = g_strconcat(set_getstr(&ic->acc->set, "base_url"), url_string, NULL); full_header = oauth_http_header(td->oauth_info, is_post ? "POST" : "GET", full_url, url_arguments); @@ -108,10 +123,14 @@ void *twitter_http(struct im_connection *ic, char *url_string, http_input_functi g_string_append(request, "\r\n"); } - ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data); + if (base_url) + ret = http_dorequest(base_url->host, base_url->port, base_url->proto == PROTO_HTTPS, request->str, func, data); + else + ret = http_dorequest(td->url_host, td->url_port, td->url_ssl, request->str, func, data); g_free(url_arguments); g_string_free(request, TRUE); + g_free(base_url); return ret; } diff --git a/protocols/twitter/twitter_http.h b/protocols/twitter/twitter_http.h index 393a1c26..02a7ea43 100644 --- a/protocols/twitter/twitter_http.h +++ b/protocols/twitter/twitter_http.h @@ -29,8 +29,8 @@ struct oauth_info; -void *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, - gpointer data, int is_post, char** arguments, int arguments_len); +struct http_request *twitter_http(struct im_connection *ic, char *url_string, http_input_function func, + gpointer data, int is_post, char** arguments, int arguments_len); #endif //_TWITTER_HTTP_H diff --git a/protocols/twitter/twitter_lib.c b/protocols/twitter/twitter_lib.c index 654cf467..8d10a406 100644 --- a/protocols/twitter/twitter_lib.c +++ b/protocols/twitter/twitter_lib.c @@ -726,8 +726,48 @@ static void twitter_private_message_chat(struct im_connection *ic, GSList * list } } -static void twitter_http_get_home_timeline(struct http_request *req); -static void twitter_http_get_mentions(struct http_request *req); +static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); +static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); + +static void twitter_http_stream(struct http_request *req) +{ + int len; + int i; + char c; + json_value *j; + + printf( "%d bytes in stream\n", req->body_size ); + + /* m/^[\d\s]*/ /* why is there a second commend here? :-) */ + for (i = 0; i < req->body_size; i ++) { + if (!isspace(req->reply_body[i]) && !isdigit(req->reply_body[i])) + break; + } + + /* Nothing but numbers and whitespace in there. Try again later. */ + if (i == req->body_size) + return; + + /* Get length. */ + if (sscanf(req->reply_body, "%d", &len) != 1) + return; + + if (req->body_size < i + len) { + printf("Not enough bytes in buffer yet\n"); + return; + } + + http_flush_bytes(req, i); + c = req->reply_body[len]; + req->reply_body[len] = '\0'; + + printf("JSON: %s\n", req->reply_body); + printf("parsed: %p\n", (j = json_parse(req->reply_body))); + json_value_free(j); + req->reply_body[len] = c; + + http_flush_bytes(req, len); +} /** * Get the timeline with optionally mentions @@ -751,6 +791,23 @@ void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor) if (include_mentions) { twitter_get_mentions(ic, next_cursor); } + + static int bla = 0; + + if (bla) + return; + bla = 1; + + char *args[4]; + args[0] = "with"; + args[1] = "followings"; + args[2] = "delimited"; + args[3] = "length"; + + if ((td->stream = twitter_http(ic, "https://userstream.twitter.com/1.1/user.json", + twitter_http_stream, ic, 0, args, 4))) { + td->stream->flags |= HTTPC_STREAMING; + } } /** @@ -809,10 +866,13 @@ void twitter_flush_timeline(struct im_connection *ic) td->home_timeline_obj = td->mentions_obj = NULL; } +static void twitter_http_get_home_timeline(struct http_request *req); +static void twitter_http_get_mentions(struct http_request *req); + /** * Get the timeline. */ -void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) +static void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) { struct twitter_data *td = ic->proto_data; @@ -848,7 +908,7 @@ void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor) /** * Get mentions. */ -void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) +static void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor) { struct twitter_data *td = ic->proto_data; diff --git a/protocols/twitter/twitter_lib.h b/protocols/twitter/twitter_lib.h index 7b640b00..624fc80a 100644 --- a/protocols/twitter/twitter_lib.h +++ b/protocols/twitter/twitter_lib.h @@ -80,8 +80,6 @@ void twitter_get_timeline(struct im_connection *ic, gint64 next_cursor); void twitter_get_friends_ids(struct im_connection *ic, gint64 next_cursor); -void twitter_get_home_timeline(struct im_connection *ic, gint64 next_cursor); -void twitter_get_mentions(struct im_connection *ic, gint64 next_cursor); void twitter_get_statuses_friends(struct im_connection *ic, gint64 next_cursor); void twitter_post_status(struct im_connection *ic, char *msg, guint64 in_reply_to); |