Permalink
Browse files

prepare gems

  • Loading branch information...
1 parent 8c01a2e commit 51a9c79aa313042ebdee4df37a95cea35447a628 @siuying committed Aug 27, 2010
Showing with 199 additions and 66 deletions.
  1. +2 −0 .gitignore
  2. +3 −0 Gemfile
  3. +10 −0 Manifest
  4. +13 −0 README.md
  5. +14 −0 Rakefile
  6. +9 −0 bin/reflect
  7. +2 −1 config/config.default.yml
  8. +8 −0 init.rb
  9. +9 −0 lib/reflect.rb
  10. +46 −10 lib/reflect/helpers.rb
  11. +83 −0 lib/reflect/reflect.rb
  12. +0 −55 reflect.rb
View
@@ -0,0 +1,2 @@
+config/config.yml
+pkg
View
@@ -1,4 +1,7 @@
source 'http://rubygems.org'
+gem 'rake'
+gem 'echoe'
gem 'twitter-stream', :require => 'twitter/json_stream'
+gem 'plurk'
gem 'json'
View
@@ -0,0 +1,10 @@
+Gemfile
+Manifest
+README.md
+Rakefile
+bin/reflect
+config/config.default.yml
+init.rb
+lib/reflect.rb
+lib/reflect/helpers.rb
+lib/reflect/reflect.rb
View
@@ -0,0 +1,13 @@
+# Reflect - A retweet bot for Plurk #
+
+Reflect listen to your twitter stream, and retweet your message to plurk.
+
+## Usage ##
+
+1. Clone the repository
+2. Create file config/config.yml from config/config.default.yml, enter your twitter username/password and plurk api key/username/password. Get Plurk API key at [http://www.plurk.com/API](http://www.plurk.com/API).
+3. Run the app using following command line:
+
+ ruby reflect.rb
+
+Released under MIT License.
View
@@ -0,0 +1,14 @@
+# Rakefile
+require 'rubygems'
+require 'bundler'
+Bundler.require(:default)
+
+Echoe.new('reflect', '0.1.0') do |p|
+ p.description = "Send your Tweets to Plurk"
+ p.url = "http://github.com/siuying/reflect"
+ p.author = "Francis Chong"
+ p.email = "francis@ignition.hk"
+ p.executable_pattern = ["bin/*"]
+ p.ignore_pattern = ["config/config.yml", "tmp/*", "pkg/*"]
+ p.runtime_dependencies = ["rake", "echoe", "bundler", "twitter-stream", "plurk", "json"]
+end
View
@@ -0,0 +1,9 @@
+#!/usr/bin/env ruby
+require 'reflect'
+include Reflect::Helpers
+
+#### Read Configuration ####
+CONFIG = configure(config_file)
+
+reflect = Reflect::Main.new(CONFIG)
+reflect.listen
@@ -4,4 +4,5 @@ twitter:
plurk:
api: your_plurk_api_key
username: your_username
- password: your_password
+ password: your_password
+plurk_retweet: true
View
@@ -0,0 +1,8 @@
+require 'reflect'
+
+include Reflect::Helpers
+
+CONFIG = configure(config_file) rescue nil
+
+puts "*** Welcome to Reflect! ***"
+puts "To get started, please edit #{config_file} with your twitter and plurk account!"
View
@@ -0,0 +1,9 @@
+path = File.expand_path(File.dirname(__FILE__))
+$:.unshift(path) unless $:.include?(path)
+
+module Reflect
+end
+
+require 'reflect/helpers'
+require 'reflect/reflect'
+
@@ -1,21 +1,57 @@
+require 'ftools'
+
module Reflect
module Helpers
- def read_config(path)
+
+ def config_file
+ dir = "#{ENV["HOME"]}/.reflect"
+ config = "#{dir}/config.yml"
+
+ unless File.exists?(dir)
+ puts "Create config file folder: #{dir}"
+ File.makedirs(dir)
+
+ unless File.exists?(config)
+ source = File.expand_path('../../../config/config.default.yml', __FILE__)
+ puts "Copy config file from #{source} to #{config}"
+ File.copy(source, config)
+ end
+ else
+ puts "Use config file #{config}"
+ end
+
+ return config
+ end
+
+ def configure(config_file)
begin
- raw_config = File.read(path)
+ puts "Read config file: #{config_file}"
+ raw_config = File.read(config_file)
config = YAML.load(raw_config)
-
rescue
- raise "Failed loading config from path: #{path}"
+ raise "Cannot read the config file: #{config_file}. Please check if the config file exists!"
end
- end
-
- def validate_config(config)
- if config.nil? || config["twitter"].nil?
- return false
+
+ if config.nil?
+ raise "Config file (#{config_file}) not exists!"
+
+ elsif config["twitter"].nil? || config["plurk"].nil?
+ raise "Config file (#{config_file}) invalid!"
+
+ elsif config["twitter"]["username"].nil? || config["twitter"]["username"] == "your_username"
+ raise "You must configure twitter account in config file first! (#{config_file})"
+
+ elsif config["plurk"]["username"].nil? || config["plurk"]["username"] == "your_username"
+ raise "You must configure plurk account in config file first! (#{config_file})"
+
+ elsif config["plurk"]["api"] == "your_plurk_api_key"
+ raise "You must set the Plurk API Key in config file first! (#{config_file})"
+
else
- return true
+ return config
+
end
end
+
end
end
@@ -0,0 +1,83 @@
+require 'open-uri'
+require 'logger'
+
+require 'rubygems'
+require 'bundler'
+Bundler.require(:default)
+
+module Reflect
+ class Main
+ attr_accessor :logger, :config, :send_retweet
+
+ def initialize(config, logger = Logger.new($stdout))
+ @logger = logger
+ @config = config
+
+ @send_retweet = config["plurk_retweet"]
+ @twitter_auth = "#{config["twitter"]["username"]}:#{config["twitter"]["password"]}"
+
+ @logger.info "Fetch twitter userid of user: #{config["twitter"]["username"]}"
+ @user_id = userid_from_username(config["twitter"]["username"])
+
+ @logger.info "Login to plurk"
+ @plurk = init_plurk(config["plurk"]["api"], config["plurk"]["username"], config["plurk"]["password"])
+ end
+
+ def listen
+ #### Start Listening Twitter Streams ####
+ @logger.info "Start listening user stream of (#{@user_id})..."
+ EventMachine::run {
+ stream = Twitter::JSONStream.connect(
+ :path => "/1/statuses/filter.json?follow=#{@user_id}",
+ :auth => @twitter_auth
+ )
+
+ stream.each_item do |item|
+ data = JSON(item)
+ from_user = data["user"]["screen_name"] rescue nil
+ text = data["text"] rescue nil
+ in_reply_to_user_id = data["in_reply_to_user_id"] rescue nil
+ retweeted_status = data["retweeted_status"] rescue nil
+ delete = data["delete"] rescue nil
+
+ if from_user == @config["twitter"]["username"] && text
+ if in_reply_to_user_id || delete || (!send_retweet && retweeted_status)
+ @logger.debug " skipped, this is a reply, delete or retweet"
+ else
+ @logger.info "Plurk: #{text}"
+ @plurk.plurk_add :content => text, :qualifier => "says"
+ end
+ end
+ end
+
+ stream.on_error do |message|
+ @logger.error message
+ end
+
+ stream.on_max_reconnects do |timeout, retries|
+ @logger.error "max reconnects reached"
+ end
+
+ trap('TERM') {
+ @logger.info "stopping client"
+ stream.stop
+ EventMachine.stop if EventMachine.reactor_running?
+ }
+ }
+ end
+
+ private
+ # Find twitter userid from twitter username
+ def userid_from_username(username)
+ data = JSON(open("http://api.twitter.com/1/users/show/#{username}.json").read)
+ data["id"]
+ end
+
+ # create plurk api and login
+ def init_plurk(api, username, password)
+ plurk = Plurk::Client.new api
+ plurk.login :username => username, :password => password
+ plurk
+ end
+ end
+end
View
@@ -1,55 +0,0 @@
-require 'rubygems'
-require 'bundler'
-require 'open-uri'
-
-Bundler.require(:default)
-
-require 'lib/reflect/helpers'
-include Reflect::Helpers
-
-CONFIG = read_config("./config/config.yml")
-unless validate_config(CONFIG)
- exit
-end
-
-username = CONFIG["twitter"]["username"]
-password = CONFIG["twitter"]["password"]
-
-puts "Find user id of #{username}"
-data = JSON(open("http://api.twitter.com/1/users/show/#{username}.json").read)
-user_id = data["id"] rescue nil
-puts " user id = #{user_id}"
-puts "start listening user stream ..."
-
-EventMachine::run {
- stream = Twitter::JSONStream.connect(
- :path => "/1/statuses/filter.json?follow=#{user_id}",
- :auth => "#{username}:#{password}"
- )
-
- stream.each_item do |item|
- data = JSON(item)
- from_user = data["user"]["name"] rescue nil
- text = data["text"] rescue nil
-
- if from_user == username && text
- # this is tweet from username
- end
- puts "#{from_user}: #{text}"
- end
-
- stream.on_error do |message|
- puts " ERROR: #{message}"
- end
-
- stream.on_max_reconnects do |timeout, retries|
- puts " ERROR: max reconnects"
- end
-
- trap('TERM') {
- stream.stop
- EventMachine.stop if EventMachine.reactor_running?
- }
-}
-
-

0 comments on commit 51a9c79

Please sign in to comment.