Skip to content

Instantly share code, notes, and snippets.

@rummelonp
Created March 29, 2011 03:58
Show Gist options
  • Save rummelonp/891788 to your computer and use it in GitHub Desktop.
Save rummelonp/891788 to your computer and use it in GitHub Desktop.
Userstreamからふぁぼられとかを監視とかしてGrowlとかするかもしれないRubyスクリプト
---
:consumer_key: consumer_key
:consumer_secret: consumer_secret
:oauth_token: oauth_token
:oauth_token_secret: oauth_token_secret
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
## Userstream is a simple command line interface for growl twitter userstream events and keywords.
##
## Usage: ruby userstream.rb [options]
##
##
require 'rubygems'
require 'userstream'
require 'yaml'
require 'optparse'
module Userstream::Growl
extend self
attr_accessor :growl_path
attr_accessor :app_icon
attr_accessor :keywords
self.growl_path = '/usr/local/bin/growlnotify'
self.keywords = []
attr_reader :oauth
attr_reader :option
attr_reader :credentials
attr_reader :userstream
# Growl title, text and icon
def growl(title, text)
title = title.gsub("'", '')
text = text.gsub("'", '')
command = "#{@growl_path} '#{title}' -m '#{text}'"
command += " -a #{@app_icon}" if @app_icon
`#{command}`
end
# Wrapper of OAuth and Userstream
def oauth_userstream(options = {})
consumer = OAuth::Consumer.new(@oauth.consumer_key, @oauth.consumer_secret, options)
access_token = OAuth::AccessToken.new(consumer, @oauth.oauth_token, @oauth.oauth_token_secret)
Userstream.new(consumer, access_token)
end
# Current user credentials
def credentials
@credentials ||= oauth_userstream(:site => 'https://api.twitter.com/').verify_credentials
end
# Userstream instance
def userstream
@userstream ||= oauth_userstream(:site => 'https://userstream.twitter.com/')
end
# Callback of userstream.
def process(status)
title = text = nil
if status.event
# Do nothing when source is mine
raise if status.source.screen_name == credentials.screen_name
case status.event.to_sym
when :favorite
title = "@#{status.source.screen_name}: Favorited"
text = status.target_object.text
when :unfavorite
title = "@#{status.source.screen_name}: Unfavorited"
text = status.target_object.text
when :follow
title = "@#{status.source.screen_name}: New follower"
text = "@#{status.source.screen_name} started following @#{credentials.screen_name}"
when :list_member_added
title = "@#{status.source.screen_name}: Add to list"
text = "@#{status.source.screen_name} added @#{credentials.screen_name} to the list #{status.target_object.full_name}"
when :list_member_removed
title = "@#{status.source.screen_name}: Remove from list"
text = "@#{status.source.screen_name} removed @#{credentials.screen_name} from the list #{status.target_object.full_name}"
else
title = 'Unimplemented event'
text = status.event
end
else
text = status.text
if @keywords.any? {|k| text.match(k)}
title = "@#{status.user.screen_name}: Keyword \"#{$&.to_s}\""
end
end
if title && text
# Growl title and text
growl title, text
# Outout title and text
$stdout.puts title, text, Time.now, "\n"
$stdout.flush
end
end
# Parse command line options
def parse(args)
@option = OptionParser.new do |option|
option.banner = File.readlines(__FILE__)
.grep(/^##.*/)
.map {|l| l.chomp[3..-1]}
.join("\n")
option.separator 'Options'
credentials_text = 'Path fo YAML file with consumer_key, consumer_secret, oauth_token and oauth_token_secret keys.'
option.on('-c', '--credentials file', credentials_text) do |file|
@oauth = Hashie::Mash.new(YAML.load_file(file))
end
keywords_text = 'Filter keywords separated by a comma.'
option.on('-k', '--keywords [keyword,...]', keywords_text) do |keywords|
@keywords = keywords.split(',')
end
growl_path_text = 'Growlnotify executable path. (default: "/usr/local/bin/growlnotify")'
option.on('-g', '--growl-path [path]', growl_path_text) do |path|
@growl_path = path
end
option.on('-a', '--app-icon [name]', 'Growl app icon.') do |name|
@app_icon = name
end
option.on('-o', '--output [file]', 'Output file path.') do |file|
$stdout = File.open(file, 'a')
end
option.on('-h', '--help', 'Show this help message.') do
$stderr.puts option
exit
end
option.parse! args
end
if @oauth.nil?
$stderr.puts @option
exit
end
end
# Run command
def run(args)
parse(args)
Signal.trap(:INT) do
$stderr.puts "\n", 'Exit'
exit 0
end
begin
userstream.user &method(:process)
rescue
error = $!.to_s
growl 'Exit', error
$stderr.puts error, 'Exit', "\n"
exit
end
end
end
Userstream::Growl.run(ARGV)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment