Skip to content

Instantly share code, notes, and snippets.

@elim
Created November 20, 2009 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elim/239318 to your computer and use it in GitHub Desktop.
Save elim/239318 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# -*- mode: ruby; coding: utf-8-unix; indent-tabs-mode: nil -*-
#
# Author:: Takeru Naito (mailto:takeru.naito@gmail.com)
# Copyright:: Copyright (c) 2009 Takeru Naito
# License:: Distributes under the same terms as Ruby
#
#
require 'logger'
require 'rubygems'
require 'mechanize'
require 'pit'
class DestoryTweets
def initialize(opts = {})
@agent = WWW::Mechanize.new do |a|
a.max_history = 1
a.user_agent_alias = 'Mac FireFox'
a.log = Logger.new(opts[:log_output] ||= STDOUT)
a.log.level = opts[:log_level] ||= Logger::WARN
end
@config = Pit.get('twitter', :require => {
'username' => 'your username in Twitter.',
'password' => 'your password in Twitter.',
})
@baseurl = 'http://twitter.com/statuses/user_timeline.xml';
end
def run
@agent.auth(@config['username'], @config['password'])
loop do
fetch(@baseurl)
ids = Nokogiri(@agent.page.body).search('status/id')
break if ids.length == 0
delete(ids)
end
end
private
def delete(ids)
ids.each do |id|
url = "http://twitter.com/statuses/destroy/#{id.inner_text}.xml"
@agent.log.warn "trying delete #{url}."
result = fetch(url, :delete)
if result.methods.include?('response') and result.response["status"] == "200 OK"
@agent.log.warn "... deleted."
wait(if result.response["x-ratelimit-remaining"].to_i < 10
Time.at(result.response["x-ratelimit-reset"].to_i) - Time.now
else
60 * 60 / result.response["x-ratelimit-limit"].to_i
end)
else
@agent.log.warn "... dalete fail. retrying..."
wait(60)
retry
end
end
end
def wait(sec)
@agent.log.warn "(wait for #{sec} seconds...)"
sleep sec
end
def fetch(uri, method_name = :get)
begin
@agent.send(method_name, uri)
rescue Timeout::Error
@agent.log.warn ' caught Timeout::Error. retrying...'
retry
rescue WWW::Mechanize::ResponseCodeError => e
case e.response_code
when '503'
@agent.log.warn " caught Net::HTTPServiceUnavailable (#{uri})."
when '400'
@agent.log.warn " caught Net::HTTPBadRequest (#{uri})."
end
end
end
end
if $0 == __FILE__
DestoryTweets.new.run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment