Changeset e08ae0c for protocols/twitter
- Timestamp:
- 2012-11-04T23:39:48Z (12 years ago)
- Branches:
- master
- Children:
- 0688e99
- Parents:
- c08d201
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
protocols/twitter/twitter_lib.c
rc08d201 re08ae0c 37 37 #include "xmltree.h" 38 38 #include "twitter_lib.h" 39 #include "json_util.h" 39 40 #include <ctype.h> 40 41 #include <errno.h> … … 188 189 } 189 190 190 static struct xt_node *twitter_parse_response(struct im_connection *ic, struct http_request *req)191 static json_value *twitter_parse_response(struct im_connection *ic, struct http_request *req) 191 192 { 192 193 gboolean logging_in = !(ic->flags & OPT_LOGGED_IN); 193 194 gboolean periodic; 194 195 struct twitter_data *td = ic->proto_data; 195 struct xt_node *ret;196 json_value *ret; 196 197 char path[64] = "", *s; 197 198 … … 227 228 } 228 229 229 if ((ret = xt_from_string(req->reply_body, req->body_size)) == NULL) {230 if ((ret = json_parse(req->reply_body)) == NULL) { 230 231 imcb_error(ic, "Could not retrieve %s: %s", 231 232 path, "XML parse error"); … … 251 252 252 253 /** 253 * Function to help fill a list.254 */255 static xt_status twitter_xt_next_cursor(struct xt_node *node, struct twitter_xml_list *txl)256 {257 char *end = NULL;258 259 if (node->text)260 txl->next_cursor = g_ascii_strtoll(node->text, &end, 10);261 if (end == NULL)262 txl->next_cursor = -1;263 264 return XT_HANDLED;265 }266 267 /**268 254 * Fill a list of ids. 269 255 */ 270 static xt_status twitter_xt_get_friends_id_list(struct xt_node *node, struct twitter_xml_list *txl) 271 { 272 struct xt_node *child; 256 static xt_status twitter_xt_get_friends_id_list(json_value *node, struct twitter_xml_list *txl) 257 { 258 json_value *c; 259 int i; 273 260 274 261 // Set the list type. 275 262 txl->type = TXL_ID; 276 263 277 // The root <statuses> node should hold the list of statuses <status> 278 // Walk over the nodes children. 279 for (child = node->children; child; child = child->next) { 280 if (g_strcasecmp("ids", child->name) == 0) { 281 struct xt_node *idc; 282 for (idc = child->children; idc; idc = idc->next) 283 if (g_strcasecmp(idc->name, "id") == 0) 284 txl->list = g_slist_prepend(txl->list, 285 g_memdup(idc->text, idc->text_len + 1)); 286 } else if (g_strcasecmp("next_cursor", child->name) == 0) { 287 twitter_xt_next_cursor(child, txl); 288 } 289 } 290 264 c = json_o_get(node, "ids"); 265 if (!c || c->type != json_array) 266 return XT_ABORT; 267 268 for (i = 0; i < c->u.array.length; i ++) { 269 txl->list = g_slist_prepend(txl->list, 270 g_strdup_printf("%ld", c->u.array.values[i]->u.integer)); 271 272 } 273 274 c = json_o_get(node, "next_cursor"); 275 if (c && c->type == json_integer) 276 txl->next_cursor = c->u.integer; 277 else 278 txl->next_cursor = -1; 279 291 280 return XT_HANDLED; 292 281 } … … 300 289 { 301 290 struct im_connection *ic; 302 struct xt_node *parsed;291 json_value *parsed; 303 292 struct twitter_xml_list *txl; 304 293 struct twitter_data *td; … … 322 311 if (!(parsed = twitter_parse_response(ic, req))) 323 312 return; 313 324 314 twitter_xt_get_friends_id_list(parsed, txl); 325 xt_free_node(parsed);315 json_value_free(parsed); 326 316 327 317 td->follow_ids = txl->list; … … 338 328 } 339 329 340 static xt_status twitter_xt_get_users( struct xt_node *node, struct twitter_xml_list *txl);330 static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl); 341 331 static void twitter_http_get_users_lookup(struct http_request *req); 342 332 … … 379 369 { 380 370 struct im_connection *ic = req->data; 381 struct xt_node *parsed;371 json_value *parsed; 382 372 struct twitter_xml_list *txl; 383 373 GSList *l = NULL; … … 395 385 return; 396 386 twitter_xt_get_users(parsed, txl); 397 xt_free_node(parsed);387 json_value_free(parsed); 398 388 399 389 // Add the users as buddies. … … 410 400 411 401 /** 412 * Function to fill a twitter_xml_user struct.413 * It sets:414 * - the name and415 * - the screen_name.416 */417 static xt_status twitter_xt_get_user(struct xt_node *node, struct twitter_xml_user *txu)418 {419 struct xt_node *child;420 421 // Walk over the nodes children.422 for (child = node->children; child; child = child->next) {423 if (g_strcasecmp("name", child->name) == 0) {424 txu->name = g_memdup(child->text, child->text_len + 1);425 } else if (g_strcasecmp("screen_name", child->name) == 0) {426 txu->screen_name = g_memdup(child->text, child->text_len + 1);427 }428 }429 return XT_HANDLED;430 }431 432 /**433 402 * Function to fill a twitter_xml_list struct. 434 403 * It sets: 435 404 * - all <user>s from the <users> element. 436 405 */ 437 static xt_status twitter_xt_get_users( struct xt_node *node, struct twitter_xml_list *txl)406 static xt_status twitter_xt_get_users(json_value *node, struct twitter_xml_list *txl) 438 407 { 439 408 struct twitter_xml_user *txu; 440 struct xt_node *child;409 int i; 441 410 442 411 // Set the type of the list. 443 412 txl->type = TXL_USER; 444 413 414 if (!node || node->type != json_array) 415 return XT_ABORT; 416 445 417 // The root <users> node should hold the list of users <user> 446 418 // Walk over the nodes children. 447 for (child = node->children; child; child = child->next) { 448 if (g_strcasecmp("user", child->name) == 0) { 449 txu = g_new0(struct twitter_xml_user, 1); 450 twitter_xt_get_user(child, txu); 451 // Put the item in the front of the list. 452 txl->list = g_slist_prepend(txl->list, txu); 453 } 419 for (i = 0; i < node->u.array.length; i ++) { 420 txu = g_new0(struct twitter_xml_user, 1); 421 txu->name = g_strdup(json_o_str(node->u.array.values[i], "name")); 422 txu->screen_name = g_strdup(json_o_str(node->u.array.values[i], "screen_name")); 423 txl->list = g_slist_prepend(txl->list, txu); 454 424 } 455 425 … … 491 461 } else if (g_strcasecmp("user", child->name) == 0) { 492 462 txs->user = g_new0(struct twitter_xml_user, 1); 493 twitter_xt_get_user(child, txs->user);463 // twitter_xt_get_user(child, txs->user); 494 464 } else if (g_strcasecmp("id", child->name) == 0) { 495 465 txs->id = g_ascii_strtoull(child->text, NULL, 10); … … 579 549 } 580 550 } else if (g_strcasecmp("next_cursor", child->name) == 0) { 581 twitter_xt_next_cursor(child, txl);551 // twitter_xt_next_cursor(child, txl); 582 552 } 583 553 } … … 921 891 goto end; 922 892 twitter_xt_get_status_list(ic, parsed, txl); 923 xt_free_node(parsed);893 // json_value_free(parsed); 924 894 925 895 td->home_timeline_obj = txl; … … 954 924 goto end; 955 925 twitter_xt_get_status_list(ic, parsed, txl); 956 xt_free_node(parsed);926 // json_value_free(parsed); 957 927 958 928 td->mentions_obj = txl;
Note: See TracChangeset
for help on using the changeset viewer.