1
0
Fork 0
mirror of git://git.psyced.org/git/psyced synced 2024-08-15 03:25:10 +00:00

twitter user stream

This commit is contained in:
Gabor Adam Toth 2011-08-24 10:57:50 +02:00
parent 7840f73c91
commit 319d7329bf
3 changed files with 326 additions and 47 deletions

View file

@ -0,0 +1,44 @@
#include <net.h>
#include <ht/http.h>
inherit NET_PATH "http/fetch";
int buffer_content(string data) {
P2(("%O got data:\n%O\n", ME, data))
mixed *waiter;
foreach (waiter : qToArray(ME)) {
funcall(waiter[0], data, waiter[1] ? fheaders : copy(fheaders), http_status, 1);
}
next_input_to(#'buffer_content); //'
return 1;
}
disconnected(string data) {
P2(("%O got disconnected:\n%O\n", ME, data))
headers["_fetchtime"] = isotime(ctime(time()), 1);
if (headers["last-modified"])
rheaders["if-modified-since"] = headers["last-modified"];
//if (headers["etag"])
// rheaders["if-none-match"] = headers["etag"]; // heise does not work with etag
fheaders = headers;
buffer = headers = 0;
switch (http_status) {
default:
mixed *waiter;
while (qSize(ME)) {
waiter = shift(ME);
P2(("%O calls back.. body is %O\n", ME, data))
funcall(waiter[0], data, waiter[1] ? fheaders : copy(fheaders), http_status, 0);
}
if (http_status == R_OK) break;
// doesn't seem to get here when HTTP returns 301 or 302. strange.
// fall thru
case R_NOTMODIFIED:
qDel(ME);
qInit(ME, 150, 5);
}
fetching = 0;
return 1; // presume this disc was expected
}

View file

@ -11,15 +11,19 @@
#include <tls.h>
#include <ht/http.h>
string consumer_key;
string consumer_secret;
string request_token_url;
mapping request_params = ([ ]);
mapping access_params = ([ ]);
string access_token_url;
string authorize_url;
string callback_url = HTTPS_OR_HTTP_URL + "/oauth";
object user;
volatile string consumer_key;
volatile string consumer_secret;
volatile string request_token_url;
persistent mapping request_params = ([ ]);
persistent mapping access_params = ([ ]);
volatile string access_token_url;
volatile string authorize_url;
volatile string callback_url = HTTPS_OR_HTTP_URL + "/oauth";
volatile object user;
volatile int authorized = 0;
oauth_success() {}
oauth_error() {}
varargs void fetch(object ua, string url, string method, mapping post, mapping oauth) {
P3((">> oauth:fetch(%O, %O, %O)\n", url, method, oauth))
@ -60,12 +64,16 @@ void parse_request_token(string body, mapping headers, int http_status) {
if (strlen(request_params["oauth_token"]) && strlen(request_params["oauth_token_secret"])) {
shared_memory("oauth_request_tokens")[request_params["oauth_token"]] = ME;
//P3((">>> adding token: %O to shm: %O\n", request_params["oauth_token"], shared_memory("oauth_request_tokens")))
sendmsg(user, "_notice_oauth_authorize_url", "Open [_url] to perform authorization.",
(["_url": authorize_url + "?oauth_token=" + request_params["oauth_token"]]));
if (user) sendmsg(user, "_notice_oauth_authorize_url", "Open [_url] to perform authorization.",
(["_url": authorize_url + "?oauth_token=" + request_params["oauth_token"]]));
P1(("OAuth: open %s to perform authorization.\n", authorize_url + "?oauth_token=" + request_params["oauth_token"]));
return;
}
}
sendmsg(user, "_error_oauth_token_request", "OAuth failed: could not get a request token.");
if (user) sendmsg(user, "_error_oauth_token_request", "OAuth failed: could not get a request token.");
P1(("OAuth failed: could not get a request token.\n"));
authorized = -1;
oauth_error();
}
void parse_access_token(string body, mapping headers, int http_status) {
@ -74,11 +82,17 @@ void parse_access_token(string body, mapping headers, int http_status) {
access_params = ([]);
url_parse_query(access_params, body);
if (strlen(access_params["oauth_token"]) && strlen(access_params["oauth_token_secret"])) {
sendmsg(user, "_notice_oauth_success", "OAuth successful.");
if (user) sendmsg(user, "_notice_oauth_success", "OAuth successful.");
P1(("OAuth successful.\n"));
authorized = 1;
oauth_success();
return;
}
}
sendmsg(user, "_error_oauth_token_access", "OAuth failed: could not get an access token.");
if (user) sendmsg(user, "_error_oauth_token_access", "OAuth failed: could not get an access token.");
P1(("OAuth failed: could not get an access token.\n"));
authorized = -1;
oauth_error();
}
void verified(string verifier) {
@ -88,18 +102,30 @@ void verified(string verifier) {
fetch(ua, access_token_url, "POST", 0, (["oauth_verifier": verifier]));
}
object load(object usr, string key, string secret, string request, string access, string authorize) {
if (usr) user = usr;
if (key) consumer_key = key;
if (secret) consumer_secret = secret;
if (request) request_token_url = request;
if (access) access_token_url = access;
if (authorize) authorize_url = authorize;
void oauth() {
if (!request_token_url) return;
if (request_token_url && user) {
object ua = clone_object(NET_PATH "http/fetch");
ua->content(#'parse_request_token, 1, 1); //');
fetch(ua, request_token_url, "POST", 0, (["oauth_callback": callback_url]));
}
request_params = ([ ]);
access_params = ([ ]);
authorized = 0;
object ua = clone_object(NET_PATH "http/fetch");
ua->content(#'parse_request_token, 1, 1); //');
fetch(ua, request_token_url, "POST", 0, (["oauth_callback": callback_url]));
}
object load(object usr, mapping opts) {
if (usr) user = usr;
unless (mappingp(opts)) opts = ([]);
if (opts["consumer_key"]) consumer_key = opts["consumer_key"];
if (opts["consumer_secret"]) consumer_secret = opts["consumer_secret"];
if (opts["request_token_url"]) request_token_url = opts["request_token_url"];
if (opts["access_token_url"]) access_token_url = opts["access_token_url"];
if (opts["authorize_url"]) authorize_url = opts["authorize_url"];
if (access_params["oauth_token"])
authorized = 1;
else
oauth();
return ME;
}