Created
June 5, 2011 11:47
-
-
Save KOBA789/1008894 to your computer and use it in GitHub Desktop.
@xrekkusu 用、ゆれくる投稿すくりぷと
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // この辺は自分でアプリ登録して書き換える | |
| var consumerKey = '', | |
| consumerSecret = '', | |
| token = '', | |
| tokenSecret = ''; | |
| // 投稿するメッセージの内容 | |
| var message = 'ゆれくる!!'; | |
| /* === 以下ごにょごにょ === */ | |
| var crypto = require('crypto'), | |
| http = require('http'), | |
| URL = require('url'), | |
| util = require('util'), | |
| EventEmitter = require('events').EventEmitter; | |
| String.prototype.replaceAll = function (origin, destination) { | |
| var result = new String(); | |
| result = this.split(origin).join(destination); | |
| return result; | |
| } | |
| function OAuth(consumerKey, consumerSecret, token, tokenSecret, option) { | |
| this.consumerKey = consumerKey || ''; | |
| this.consumerSecret = consumerSecret || ''; | |
| this.token = token || ''; | |
| this.tokenSecret = tokenSecret || ''; | |
| if (option instanceof Object) { | |
| for (var property in option) { | |
| this[property] = option[property]; | |
| } | |
| } | |
| const NONCE_CHARS = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'; | |
| this.getNonce = function (charsLength) { | |
| var result = String(); | |
| for (var i = 0; i < charsLength; i++) { | |
| var randomNumber = this.getRandom(NONCE_CHARS.length); | |
| result += NONCE_CHARS.charAt(randomNumber); | |
| } | |
| return result; | |
| } | |
| this.getRandom = function (maxVal) { | |
| return Math.floor(Math.random() * maxVal); | |
| } | |
| const REPLACE_MAP = { | |
| '!': '%21', | |
| '\'': '%27', | |
| '(': '%28', | |
| ')': '%29', | |
| '*': '%2A' | |
| } | |
| this.escapeChars = function (str) { | |
| var result = String(); | |
| if (!str) { | |
| return result; | |
| } | |
| result = encodeURIComponent(str); | |
| for (var origin in REPLACE_MAP) { | |
| var destination = REPLACE_MAP[origin]; | |
| result = result.replaceAll(origin, destination); | |
| } | |
| return result; | |
| } | |
| this.generateSignature = function (method, url, query, consumerSecret, tokenSecret) { | |
| var result = String(); | |
| var escapedMethod = this.escapeChars(method); | |
| var escapedUrl = this.escapeChars(url); | |
| var escapedQuery = this.escapeChars(query); | |
| var key = consumerSecret + '&' + tokenSecret; | |
| var jointed = escapedMethod + '&' + escapedUrl + '&' + escapedQuery; | |
| var hmac = crypto.createHmac('sha1', key); | |
| hmac.update(jointed); | |
| var digest = hmac.digest('base64'); | |
| result = String(digest); | |
| return result; | |
| } | |
| this.generateQueryString = function (queryies) { | |
| var result = String(); | |
| var keys = Array(); | |
| for (var key in queryies) { | |
| keys.push(key); | |
| } | |
| keys.sort(); | |
| for (var index in keys) { | |
| var key = keys[index]; | |
| result += this.escapeChars(key) + '=' + this.escapeChars(queryies[key]) + '&' | |
| } | |
| result = result.substring(0, result.length - 1); | |
| return result; | |
| } | |
| this.generateAuthorizationHeader = function (queryies) { | |
| var result = String(); | |
| for (var key in queryies) { | |
| if (key.lastIndexOf('oauth_', 0) == 0) { | |
| result += this.escapeChars(key) + '="' + this.escapeChars(queryies[key]) + '",' | |
| } | |
| } | |
| result = 'OAuth ' + result.substring(0, result.length - 1); | |
| return result; | |
| } | |
| this.removeAuthorizationHeader = function (queryies) { | |
| var result = Object(); | |
| for (var key in queryies) { | |
| if (!(key.lastIndexOf('oauth_', 0) == 0)) { | |
| result[key] = queryies[key]; | |
| } | |
| } | |
| return result; | |
| } | |
| this.getTimestamp = function () { | |
| var result = Number(); | |
| result = parseInt(Number(new Date) / 1000); | |
| return result; | |
| } | |
| this.performNomalRequest = function (method, url, query, header, streaming) { | |
| method = String(method); | |
| var parsedUrl = URL.parse(url, true); | |
| var isSSL = (String(parsedUrl.protocol) === 'https:'); | |
| var host = String(parsedUrl.hostname); | |
| var port = Number(parsedUrl.port) || (isSSL ? 443 : 80); | |
| var path = String(parsedUrl.pathname); | |
| var body = String(); | |
| var isGET = (method === 'GET'); | |
| var isPOST = (method === 'POST'); | |
| var isDELETE = (method === 'DELETE'); | |
| var isPUT = (method === 'PUT'); | |
| if (!(header instanceof Object)) { | |
| header = Object(); | |
| } | |
| var queryString = String(); | |
| if (query instanceof Object) { | |
| for (var key in query) { | |
| queryString += this.escapeChars(key) + '=' + this.escapeChars(query[key]) + '&'; | |
| } | |
| queryString = queryString.substring(0, queryString.length - 1); | |
| if (isGET && queryString.length != 0) { | |
| path += "?" + queryString; | |
| } | |
| } | |
| header['Host'] = host; | |
| if (isPOST) { | |
| header['Content-Length'] = queryString.length; | |
| header['Content-Type'] = 'application/x-www-form-urlencoded'; | |
| body = queryString; | |
| } | |
| var httpClient = http.createClient(port, host, isSSL, crypto.createCredentials({})); | |
| var request = httpClient.request(method, path, header); | |
| request.end(body); | |
| var ev = new EventEmitter(); | |
| request.on('response', function (response) { | |
| response.setEncoding('utf8'); | |
| var statusCode = Number(response.statusCode); | |
| var buffer = String(); | |
| response.on('data', function (chunk) { | |
| if (!streaming) { | |
| buffer += chunk; | |
| } | |
| ev.emit('data', chunk); | |
| }); | |
| response.on('end', function () { | |
| ev.emit('end', buffer); | |
| }); | |
| }); | |
| return ev; | |
| } | |
| this.performSecureRequest = function (method, url, query, header, streaming) { | |
| var nonce = this.getNonce(20); | |
| var timestamp = this.getTimestamp(); | |
| if (!(query instanceof Object)) { | |
| query = Object(); | |
| } | |
| query.oauth_consumer_key = this.consumerKey; | |
| query.oauth_nonce = nonce; | |
| query.oauth_signature_method = 'HMAC-SHA1'; | |
| query.oauth_timestamp = timestamp; | |
| query.oauth_version = '1.0'; | |
| if (this.tokenSecret) query.oauth_token = this.token; | |
| var queryString = this.generateQueryString(query); | |
| var signature = this.generateSignature(method, url, queryString, this.consumerSecret, this.tokenSecret); | |
| query.oauth_signature = signature; | |
| var authorizationHeader = this.generateAuthorizationHeader(query); | |
| if (!(header instanceof Object)) { | |
| header = Object(); | |
| } | |
| header.Authorization = authorizationHeader; | |
| query = this.removeAuthorizationHeader(query); | |
| return this.performNomalRequest(method, url, query, header, streaming); | |
| } | |
| this.getRequestToken = function (callback) { | |
| this.performSecureRequest('GET', 'http://api.twitter.com/oauth/request_token', null, null, false, callback); | |
| } | |
| this.getAccessToken = function (PIN, callback) { | |
| this.performSecureRequest('GET', 'http://api.twitter.com/oauth/access_token', {oauth_verifier: PIN}, null, false, callback); | |
| } | |
| } | |
| function Twitter (OAuth) { | |
| var baseUrl = { | |
| api: 'http://api.twitter.com/1/', | |
| stream: 'https://userstream.twitter.com/2/' | |
| }; | |
| this.update = function (str, callback) { | |
| OAuth.performSecureRequest('POST', baseUrl.api + 'statuses/update.json', {status: str}, null, false, callback); | |
| } | |
| } | |
| var oauth = new OAuth(consumerKey, consumerSecret, token, tokenSecret); | |
| var twitter = new Twitter(oauth); | |
| twitter.update(message); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment