Created
March 4, 2011 20:17
-
-
Save titanous/855635 to your computer and use it in GitHub Desktop.
Live tweets in Campfire
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
#!/usr/bin/env ruby | |
$KCODE = 'u' # for twitter-text | |
# gem install httparty json twitter-stream twitter-text | |
# | |
# set these environment variables: | |
# TWITTER_USER = twitter username | |
# TWITTER_PASS = twitter password | |
# TWITTER_KEYWORDS = the keywords to track, comma separated (see http://dev.twitter.com/pages/streaming_api_methods#track) | |
# CAMPFIRE_SUBDOMAIN = the subdomain of your campfire account (ex. flames if your domain is flames.campfirenow.com) | |
# CAMPFIRE_API_KEY = the API key for the user to post from (from https://flames.campfirenow.com/member/edit) | |
# CAMPFIRE_ROOM = the room id (ex. 1234 if the room URL is https://flames.campfirenow.com/room/1234) | |
# URL_BLACKLIST = a regular expression that matches URLs in tweets you don't want to see | |
require 'httparty' | |
require 'json' | |
require 'twitter/json_stream' | |
require 'twitter-text' | |
require 'open-uri' | |
EventMachine::run { | |
stream = Twitter::JSONStream.connect( | |
:path => '/1/statuses/filter.json?track=' + ENV['TWITTER_KEYWORDS'], | |
:auth => ENV['TWITTER_USER'] + ':' + ENV['TWITTER_PASS'] | |
) | |
stream.each_item do |item| | |
status = JSON.parse(item) | |
next if has_blacklisted_urls?(status['text']) | |
Campfire.room(ENV['CAMPFIRE_ROOM']).message "http://twitter.com/#{status['user']['screen_name']}/status/#{status['id']}" | |
end | |
} | |
def has_blacklisted_urls?(text) | |
if ENV['URL_BLACKLIST'] | |
urls = Twitter::Extractor.extract_urls(text).map { |u| LongURL.find(u) } | |
urls.any? { |url| url.to_s =~ Regexp.new(ENV['URL_BLACKLIST']) } | |
end | |
end | |
# from https://gist.github.com/858062 | |
class LongURL | |
REDIRECT_CODES = %w(300 301 302 303) | |
MAX_REDIRECTS = 5 | |
attr_accessor :short_urls | |
def self.find(short_url) | |
new(short_url).find | |
end | |
def initialize(short_url) | |
@short_urls = [] | |
@url = short_url | |
end | |
def find(uri = nil) | |
uri ||= @url | |
url = URI.parse(uri) | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true if url.scheme == 'https' | |
response = http.request_head url.path.empty? ? '/' : url.path | |
if REDIRECT_CODES.include?(response.code) | |
@short_urls << uri | |
return uri if @short_urls.length > MAX_REDIRECTS | |
find(response['Location']) | |
else | |
return uri | |
end | |
rescue | |
return @url | |
end | |
end | |
# From http://developer.37signals.com/campfire/ | |
class Campfire | |
include HTTParty | |
base_uri 'https://' + ENV['CAMPFIRE_SUBDOMAIN'] + '.campfirenow.com' | |
basic_auth ENV['CAMPFIRE_API_KEY'], 'x' | |
headers 'Content-Type' => 'application/json' | |
def self.rooms | |
Campfire.get('/rooms.json')["rooms"] | |
end | |
def self.room(room_id) | |
Room.new(room_id) | |
end | |
def self.user(id) | |
Campfire.get("/users/#{id}.json")["user"] | |
end | |
end | |
class Room | |
attr_reader :room_id | |
def initialize(room_id) | |
@room_id = room_id | |
end | |
def join | |
post 'join' | |
end | |
def leave | |
post 'leave' | |
end | |
def lock | |
post 'lock' | |
end | |
def unlock | |
post 'unlock' | |
end | |
def message(message) | |
send_message message | |
end | |
def paste(paste) | |
send_message paste, 'PasteMessage' | |
end | |
def play_sound(sound) | |
send_message sound, 'SoundMessage' | |
end | |
def transcript | |
get('transcript')['messages'] | |
end | |
private | |
def send_message(message, type = 'Textmessage') | |
post 'speak', :body => {:message => {:body => message, :type => type}}.to_json | |
end | |
def get(action, options = {}) | |
Campfire.get room_url_for(action), options | |
end | |
def post(action, options = {}) | |
Campfire.post room_url_for(action), options | |
end | |
def room_url_for(action) | |
"/room/#{room_id}/#{action}.json" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment