Permalink
Browse files

add publisher using coffeescript

  • Loading branch information...
samsonw committed Jun 3, 2011
1 parent 4d18608 commit 3440b21e46bc291c5bed4bf4ee2398ec0a5ae3b4
Showing with 84 additions and 1 deletion.
  1. +2 −1 .gitignore
  2. +58 −0 lib/publisher.coffee
  3. +1 −0 package.json
  4. +23 −0 publish.coffee
View
@@ -2,4 +2,5 @@
*~
*.swp
node_modules
npm-debug.log
npm-debug.log
publish.prod.coffee
View
@@ -0,0 +1,58 @@
OAuth = require('oauth').OAuth
exec = require('child_process').exec
class Publisher
constructor: (options) ->
@settings =
requestURL: options.requestURL
accessURL: options.accessURL
consumerKey: options.consumerKey
consumerSecret: options.consumerSecret
accessToken: options.accessToken
accessSecret: options.accessSecret
version: options.version ? "1.0A"
authorizeCallback: options.authorizeCallback ? null
signatureMethod: options.signatureMethod ? "HMAC-SHA1"
publishURL: options.publishURL
@init_oauth_client()
init_oauth_client: ->
# console.log @settings
@client = new OAuth @settings.requestURL
, @settings.accessURL
, @settings.consumerKey
, @settings.consumerSecret
, @settings.version
, @settings.authorizeCallback
, @settings.signatureMethod
publish: (message) ->
@client.post @settings.publishURL
, @settings.accessToken
, @settings.accessSecret
, { source: @settings.consumerKey, status: message }
, (error, data) ->
if error
console.log require('sys').inspect error
else
console.log data
console.log "PUBLISHED: #{message}"
publish_fortune: ->
self = @
exec '/usr/bin/env fortune',
(error, stdout, stderr) ->
console.log "stderr: #{stderr}" if stderr? && stderr isnt ''
console.log "exec error: #{error}" if error?
console.log stdout
fortune = "[Live Fortunes] #{stdout.trim().replace(/\s/g, ' ')}"
if fortune.length <= 140
self.publish fortune
else
# try again
console.log "> 140 chars, try again..."
self.publish_fortune()
module.exports = Publisher
View
@@ -11,5 +11,6 @@
, "dependencies": {
"express": ">= 2.3.10"
, "faye": ">= 0.6.0"
, "oauth": ">= 0.9.0"
}
}
View
@@ -0,0 +1,23 @@
#!/usr/bin/env coffee
process.addListener 'uncaughtException', (err, stack) ->
console.log '------------------------------'
console.log "Exception: #{err}"
console.log err.stack
console.log '------------------------------'
Publisher = require './lib/publisher'
new Publisher({
requestURL: 'http://api.t.sina.com.cn/oauth/request_token'
, accessURL: 'http://api.t.sina.com.cn/oauth/access_token'
, consumerKey: '[TODO: INPUT CONSUMERKEY]'
, consumerSecret: '[TODO: INPUT CONSUMERSECRET]'
, accessToken: '[TODO: INPUT ACCESSTOKEN]'
, accessSecret: '[TODO: INPUT ACCESSSECRET]'
, publishURL: 'http://api.t.sina.com.cn/statuses/update.json'
}).publish_fortune()
# TODO add twitter publisher

0 comments on commit 3440b21

Please sign in to comment.