123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421 |
- Qt.include("lib/sha1.js")
- String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
- function XAuth() {
- var OAUTH_CONSUMER_TOKEN = "rUeGDPrcxbYN6r63JA39Kg";
- var OAUTH_CONSUMER_SECRET = "EoNvzuQ5gz0LhbbssK4B9e9REdlN52ngl6YoN6eUqrs";
- var HMACSHA1SignatureType = "HMAC-SHA1";
- var OAuthVersion = "1.0";
- var OAuthParameterPrefix = "oauth_";
- var OAuthConsumerKeyKey = "oauth_consumer_key";
- var OAuthCallbackKey = "oauth_callback";
- var OAuthVersionKey = "oauth_version";
- var OAuthSignatureMethodKey = "oauth_signature_method";
- var OAuthSignatureKey = "oauth_signature";
- var OAuthTimestampKey = "oauth_timestamp";
- var OAuthNonceKey = "oauth_nonce";
- var OAuthTokenKey = "oauth_token";
- var OAuthTokenSecretKey = "oauth_token_secret";
- var OAuthVerifier = "oauth_verifier";
- var token = "";
- var tokenSecret = "";
- var timeCorrection = 0;
- var showError;
- var OAUTH_ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token";
- this.setErrorCallback = function(callback) {
- showError = callback;
- }
- this.setTokenAndSecret = function(newToken, newTokenSecret) {
- token = newToken;
- tokenSecret = newTokenSecret;
- }
- function doWebRequest(method, url, callback, errorCallback) {
- var doc = new XMLHttpRequest();
-
- doc.onreadystatechange = function() {
- var shortURL = url;
- if (doc.readyState == XMLHttpRequest.HEADERS_RECEIVED) {
- var status = doc.status;
-
- if(timeCorrection==0) {
- var twitterDateString = doc.getResponseHeader("Date");
-
- if(twitterDateString.length>0) {
- var twitterDate = parseDate(twitterDateString);
- timeCorrection = twitterDate.getTime() - (new Date()).getTime();
-
- } else {
-
- }
- }
- if(status!=200) {
- if(shortURL.length>32) {
- shortURL = shortURL.substring(0,40) + "..";
- }
- showError("Twitter API returned " + status + " " + doc.statusText + " on URL " + shortURL);
- return;
- }
- } else if (doc.readyState == XMLHttpRequest.DONE) {
- var status = doc.status;
- if(timeCorrection==0) {
- var twitterDateString = doc.getResponseHeader("Date");
-
- if(twitterDateString.length>0) {
- var twitterDate = parseDate(twitterDateString);
- timeCorrection = twitterDate.getTime() - (new Date()).getTime();
-
- } else {
-
- }
- }
- if(status!=200) {
- console.log("HTTP status " + status);
- if(shortURL.length>40) {
- shortURL = shortURL.substring(0,40) + "..";
- }
- showError("Twitter API returned " + status + " " + doc.statusText + " on URL " + shortURL);
- return;
- }
- var data = doc.responseText;
- if(typeof(data)=="undefined") {
- data = "";
- }
-
-
- callback(data);
- }
- }
- doc.open(method, url);
- doc.send();
- }
- this.getQueryParameters = function(url) {
- var questionMarkIndex = url.indexOf("?");
- if(questionMarkIndex<0) {
- return new Array();
- }
- var parameters = url.substring(questionMarkIndex+1);
- var params = new Array();
- var para = parameters.split("&");
- for(var i=0; i<para.length; i++) {
- if(para[i].startsWith("oauth")==false) {
- var nameValue = para[i].split("=");
- var q = [nameValue[0], nameValue[1]];
- params.push(q);
- }
- }
- return params;
- }
- this.generateSignatureBase = function(url,consumerKey,token,tokenSecret,httpMethod,timeStamp,nonce,signatureType) {
- if (typeof(token)=="undefined")
- {
- token = "";
- }
- if (typeof(tokenSecret)=="undefined")
- {
- tokenSecret = "";
- }
- var parameters = this.getQueryParameters(url);
- parameters.push( [OAuthVersionKey, OAuthVersion] );
- parameters.push( [OAuthNonceKey, nonce] );
- parameters.push( [OAuthTimestampKey, timeStamp] );
- parameters.push( [OAuthSignatureMethodKey, signatureType] );
- parameters.push( [OAuthConsumerKeyKey, consumerKey] );
- if (typeof(token)!="undefined" && token!="")
- {
- parameters.push( [OAuthTokenKey, token] );
- }
- this.sortParameters( parameters );
- var normalizedUrl = this.getSchemeAndHost(url);
- normalizedUrl += this.getAbsolutePath(url);
- var normalizedRequestParameters = this.normalizeRequestParameters(parameters);
- var signatureBase = "";
- signatureBase += httpMethod + "&";
- signatureBase += this.encode(normalizedUrl) + "&";
- signatureBase += this.encode(normalizedRequestParameters);
- var returnObject = new Object();
- returnObject.normalizedUrl = normalizedUrl;
- returnObject.normalizedRequestParameters = normalizedRequestParameters;
- returnObject.signatureBase = signatureBase;
- return returnObject;
- }
- this.getSchemeAndHost = function(url) {
- var startIndex = url.indexOf("//")+2;
- var endIndex = url.indexOf("/", startIndex);
- return url.substring(0,endIndex);
- }
- this.getAbsolutePath = function(url) {
- var startIndex = url.indexOf("//")+2;
- var endIndex = url.indexOf("/", startIndex);
- var questionMark = url.indexOf("?");
- if(questionMark>0) {
- return url.substring(endIndex, questionMark);
- } else {
- return url.substring(endIndex);
- }
- }
- this.sortParameters = function(items) {
- items.sort();
- }
- this.generateSignature = function(url, consumerKey, consumerSecret,token,tokenSecret,httpMethod,timeStamp,nonce) {
- var obj = this.generateSignatureBase(
- url,
- consumerKey,
- token,
- tokenSecret,
- httpMethod,
- timeStamp,
- nonce,
- HMACSHA1SignatureType);
- var signatureBase = obj.signatureBase;
- var tokenSec = "";
- if(typeof(tokenSecret)!="undefined") {
- tokenSec = tokenSecret;
- }
- var key = this.encode(consumerSecret) + "&" + this.encode(tokenSec);
- var signature = this.getSignature(signatureBase, key);
- obj.signature = signature;
- return obj;
- }
- this.getSignature = function(message, key) {
- var b64pad = '=';
- var signature = b64_hmac_sha1(key, message);
- return signature;
- }
- this.normalizeRequestParameters = function( parameters ) {
- var sb = "";
- for(var i in parameters) {
- var par = parameters[i];
- sb += par[0] + "=" + par[1] + "&";
- }
- return sb.substring(0, sb.length-1);
- }
- this.generateTimeStamp = function() {
-
-
-
- var p = (new Date()).getTime() + timeCorrection;
-
-
- return Math.floor(p / 1000);
- }
-
-
-
- function parseDate(stamp)
- {
- try {
- var parts = stamp.split(" ");
- var dayName;
- var monthName;
- var day;
- var time;
- var hours;
- var minutes;
- var seconds;
- var offset;
- var year;
-
- dayName = parts[0];
- day = parseInt(parts[1], 10);
- monthName = parts[2];
- year = parseInt(parts[3], 10);
- time = parts[4].split(":");
- hours = parseInt(time[0], 10);
- minutes = parseInt(time[1], 10);
- seconds = parseInt(time[2], 10);
- var month = 0;
- if(monthName=="Jan") {
- month = 0;
- } else if(monthName=="Feb") {
- month = 1;
- } else if(monthName=="Mar") {
- month = 2;
- } else if(monthName=="Apr") {
- month = 3;
- } else if(monthName=="May") {
- month = 4;
- } else if(monthName=="Jun") {
- month = 5;
- } else if(monthName=="Jul") {
- month = 6;
- } else if(monthName=="Aug") {
- month = 7;
- } else if(monthName=="Sep") {
- month = 8;
- } else if(monthName=="Oct") {
- month = 9;
- } else if(monthName=="Nov") {
- month = 10;
- } else if(monthName=="Dec") {
- month = 11;
- }
- var dt = new Date();
- dt.setUTCDate(day);
- dt.setYear(year);
- dt.setUTCMonth(month);
- dt.setUTCHours(hours);
- dt.setUTCMinutes(minutes);
- dt.setUTCSeconds(seconds);
- return dt;
- } catch(err) {
- console.log("Error while parsing date: " + err);
- return new Date();
- }
- }
- var nonceChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
- this.generateNonce = function(length) {
- var chars = nonceChars;
- var result = "";
- for (var i = 0; i < length; ++i) {
- var rnum = Math.floor(Math.random() * chars.length);
- result += chars.substring(rnum, rnum+1);
- }
- return result;
- }
-
-
- this.encode = function(s) {
- if (typeof(s)=="undefined" || s=="") {
- return "";
- }
- s = encodeURIComponent(s);
-
-
-
-
- s = s.replace(/\!/g, "%21");
- s = s.replace(/\*/g, "%2A");
- s = s.replace(/\'/g, "%27");
- s = s.replace(/\(/g, "%28");
- s = s.replace(/\)/g, "%29");
- return s;
- }
- this.webRequest = function(isPost, url, parameters, callback) {
- var method = "GET"
-
-
- var postData = "";
- if (isPost)
- {
- method = "POST";
- }
- if (typeof(parameters)!=undefined && parameters!=null && parameters.length>0)
- {
-
- for(var i in parameters) {
- var q = parameters[i];
- if(typeof(q[0]!="undefined" && q[0]!="" && typeof(q[1])!="undefined" && q[1]!="")) {
- if(postData.length>0) {
- postData += "&";
- }
- postData += q[0] + "=" + this.encode(q[1]);
- }
- }
- if (url.indexOf("?") > 0)
- {
- url += "&";
- }
- else
- {
- url += "?";
- }
- url += postData;
- }
-
- while(url.indexOf("?&")>0) {
- url = url.replace("?&", "?");
- }
-
- var nonce = this.generateNonce(16);
- var timeStamp = this.generateTimeStamp();
-
- var obj = this.generateSignature(
- url,
- OAUTH_CONSUMER_TOKEN,
- OAUTH_CONSUMER_SECRET,
- token,
- tokenSecret,
- method,
- timeStamp,
- nonce);
- var sig = obj.signature;
- var outUrl = obj.normalizedUrl;
- var querystring = obj.normalizedRequestParameters;
-
- if(querystring.length>0) {
- querystring += "&";
- }
- querystring += "oauth_signature=" + this.encode(sig);
-
- if (querystring.length > 0)
- {
- outUrl += "?";
- }
- doWebRequest(method, outUrl + querystring, callback);
- }
- }
|