It get the like count of Facebook.
Dependencies: EthernetInterface HTTPClient mbed-rtos mbed
Fork of HTTPClient_HelloWorld by
main.cpp@3:7ad266ae429f, 2013-12-22 (annotated)
- Committer:
- okini3939
- Date:
- Sun Dec 22 05:43:22 2013 +0000
- Revision:
- 3:7ad266ae429f
- Parent:
- 2:270e2d0bb85a
1st build
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:0e0debc29569 | 1 | #include "mbed.h" |
donatien | 0:0e0debc29569 | 2 | #include "EthernetInterface.h" |
donatien | 0:0e0debc29569 | 3 | #include "HTTPClient.h" |
okini3939 | 3:7ad266ae429f | 4 | #include <string.h> |
okini3939 | 3:7ad266ae429f | 5 | #include <ctype.h> |
okini3939 | 3:7ad266ae429f | 6 | |
okini3939 | 3:7ad266ae429f | 7 | #define URL "https://www.facebook.com/mbedmicro" |
donatien | 0:0e0debc29569 | 8 | |
donatien | 1:d263603373ac | 9 | EthernetInterface eth; |
okini3939 | 3:7ad266ae429f | 10 | |
okini3939 | 3:7ad266ae429f | 11 | int urlencode (char *str, char *buf, int len); |
okini3939 | 3:7ad266ae429f | 12 | |
okini3939 | 3:7ad266ae429f | 13 | int facebookLikes (char *target) { |
okini3939 | 3:7ad266ae429f | 14 | HTTPClient http; |
okini3939 | 3:7ad266ae429f | 15 | char url[256], buf[1024]; |
okini3939 | 3:7ad266ae429f | 16 | char *s; |
okini3939 | 3:7ad266ae429f | 17 | |
okini3939 | 3:7ad266ae429f | 18 | strcpy(url, "http://graph.facebook.com/"); |
okini3939 | 3:7ad266ae429f | 19 | urlencode(target, &url[strlen(url)], sizeof(url) - strlen(url)); |
okini3939 | 3:7ad266ae429f | 20 | // printf("url: %s\r\n", url); |
okini3939 | 3:7ad266ae429f | 21 | |
okini3939 | 3:7ad266ae429f | 22 | if (http.get(url, buf, sizeof(buf))) return -1; |
okini3939 | 3:7ad266ae429f | 23 | |
okini3939 | 3:7ad266ae429f | 24 | s = strstr(buf, "\"likes\":"); |
okini3939 | 3:7ad266ae429f | 25 | if (s == NULL) return -1; |
okini3939 | 3:7ad266ae429f | 26 | s += 8; |
okini3939 | 3:7ad266ae429f | 27 | return atoi(s); |
okini3939 | 3:7ad266ae429f | 28 | } |
donatien | 1:d263603373ac | 29 | |
donatien | 0:0e0debc29569 | 30 | int main() |
donatien | 0:0e0debc29569 | 31 | { |
donatien | 0:0e0debc29569 | 32 | eth.init(); //Use DHCP |
okini3939 | 3:7ad266ae429f | 33 | eth.connect(); |
okini3939 | 3:7ad266ae429f | 34 | printf("IP: %s\r\n", eth.getIPAddress()); |
donatien | 0:0e0debc29569 | 35 | |
okini3939 | 3:7ad266ae429f | 36 | printf("Target: %s\r\n", URL); |
okini3939 | 3:7ad266ae429f | 37 | printf("Likes: %d\r\n", facebookLikes(URL)); |
donatien | 2:270e2d0bb85a | 38 | |
donatien | 0:0e0debc29569 | 39 | eth.disconnect(); |
okini3939 | 3:7ad266ae429f | 40 | } |
donatien | 0:0e0debc29569 | 41 | |
okini3939 | 3:7ad266ae429f | 42 | |
okini3939 | 3:7ad266ae429f | 43 | int to_hex (int code) { |
okini3939 | 3:7ad266ae429f | 44 | static char hex[] = "0123456789abcdef"; |
okini3939 | 3:7ad266ae429f | 45 | return hex[code & 15]; |
okini3939 | 3:7ad266ae429f | 46 | } |
okini3939 | 3:7ad266ae429f | 47 | |
okini3939 | 3:7ad266ae429f | 48 | /* urlencode code from |
okini3939 | 3:7ad266ae429f | 49 | * Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
okini3939 | 3:7ad266ae429f | 50 | */ |
okini3939 | 3:7ad266ae429f | 51 | int urlencode (char *str, char *buf, int len) { |
okini3939 | 3:7ad266ae429f | 52 | // char *pstr = str, *buf = (char*)malloc(strlen(str) * 3 + 1), *pbuf = buf; |
okini3939 | 3:7ad266ae429f | 53 | char *pstr = str, *pbuf = buf; |
okini3939 | 3:7ad266ae429f | 54 | |
okini3939 | 3:7ad266ae429f | 55 | if (len < (strlen(str) * 3 + 1)) return -1; |
okini3939 | 3:7ad266ae429f | 56 | while (*pstr) { |
okini3939 | 3:7ad266ae429f | 57 | if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') |
okini3939 | 3:7ad266ae429f | 58 | *pbuf++ = *pstr; |
okini3939 | 3:7ad266ae429f | 59 | else if (*pstr == ' ') |
okini3939 | 3:7ad266ae429f | 60 | *pbuf++ = '+'; |
okini3939 | 3:7ad266ae429f | 61 | else |
okini3939 | 3:7ad266ae429f | 62 | *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15); |
okini3939 | 3:7ad266ae429f | 63 | pstr++; |
donatien | 0:0e0debc29569 | 64 | } |
okini3939 | 3:7ad266ae429f | 65 | *pbuf = '\0'; |
okini3939 | 3:7ad266ae429f | 66 | return 0; |
donatien | 0:0e0debc29569 | 67 | } |