Created
March 19, 2010 23:56
-
-
Save nh7a/338326 to your computer and use it in GitHub Desktop.
This file contains 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 | |
# -*- coding: utf-8 -*- | |
# KEYWORDS=@n,@n1 TWITTER_USERNAME=n TWITTER_PASSWORD=password ruby blockem.rb | |
require 'rubygems' | |
require "uri" | |
require "yajl/http_stream" | |
def twitter_uri(path, stream = false) | |
u = ENV['TWITTER_USERNAME'] | |
p = ENV['TWITTER_PASSWORD'] | |
s = 'stream.' if stream | |
uri = URI.parse("http://#{u}:#{p}@#{s}twitter.com#{path}") | |
end | |
def block_user(uid) | |
uri = twitter_uri('/blocks/create.json') | |
Yajl::HttpStream.post(uri, "user_id=#{uid}", :symbolize_keys => true) {|i| | |
return i | |
} | |
end | |
def unblock_user(uid) | |
uri = twitter_uri('/blocks/destroy.json') | |
Yajl::HttpStream.post(uri, "user_id=#{uid}", :symbolize_keys => true) {|i| | |
return i | |
} | |
end | |
def filter(track) | |
uri = twitter_uri('/1/statuses/filter.json', true) | |
Yajl::HttpStream.post(uri, "track=#{track}", :symbolize_keys => true) {|i| | |
yield i | |
} | |
end | |
def get_exempt_ids() | |
arr = [] | |
uri = twitter_uri('/followers/ids.json') | |
Yajl::HttpStream.get(uri, :symbolize_keys => true) {|i| | |
arr += i | |
} | |
uri = twitter_uri('/friends/ids.json') | |
Yajl::HttpStream.get(uri, :symbolize_keys => true) {|i| | |
arr += i | |
} | |
arr | |
end | |
def test_tweet(status) | |
uid = status[:user][:id] | |
return if $exempts.include?(uid) | |
text = status[:text] | |
source = status[:source] | |
block = false | |
if source =~ /(twtkr|Blip.fm|twitterfeed|Tuitwit)/i | |
block = $1 | |
elsif text =~ /\(cont\)/ | |
block = '(cont)' | |
elsif text =~ /(?:RT .*){3}/ | |
block = 'too many RT' | |
elsif text =~ /(?:@){10}/i | |
block = 'too many @' | |
elsif text =~ /[a-z0-9]+@[a-z0-9]+/i | |
block = 'idiot' | |
end | |
if block | |
screen_name = status[:user][:screen_name] | |
puts "#{block}\t@#{screen_name}\t#{text}" | |
block_user(uid) | |
unblock_user(uid) | |
end | |
end | |
$exempts = get_exempt_ids | |
while true | |
filter(ENV['KEYWORDS']) do |status| | |
test_tweet(status) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment