Created
June 2, 2011 10:23
-
-
Save uasi/1004220 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 | |
## | |
## Delete hatena messages sent from Hatena Group | |
## | |
require 'mechanize' | |
require 'pit' | |
# Endpoints | |
HATENA_LOGIN_URL = 'https://www.hatena.ne.jp/login' | |
HATENA_MESSAGE_URL = 'http://m.hatena.ne.jp/' | |
# Username and password | |
HATENA_USER_CONFIG = Pit.get('hatena', :require => { | |
'username' => 'Hatena username', | |
'password' => 'Hatena password' | |
}) | |
USERNAME = HATENA_USER_CONFIG['username'] | |
PASSWORD = HATENA_USER_CONFIG['password'] | |
# | |
# Log in to Hatena | |
# | |
agent = Mechanize.new | |
agent.get(HATENA_LOGIN_URL) | |
logged_in = !!agent.page.at('meta[@http-equiv="Refresh"]') | |
unless logged_in | |
agent.page.form_with(:action => '/login') do |form| | |
form.field_with(:name => 'name').value = USERNAME | |
form.field_with(:name => 'password').value = PASSWORD | |
form.click_button | |
end | |
end | |
# | |
# Gather messages | |
# | |
agent.get(HATENA_MESSAGE_URL) | |
messages = [] | |
page = 1 | |
while true | |
puts "Gathering messages from page #{page}" | |
agent.page.search('table[@class="list"]/tr').map do |tr| | |
messages << { | |
:id => tr['id'].gsub(/^message-(\d+)$/, '\1'), | |
:from => tr.at('td[@class = "from"]').content.strip, | |
:title => tr.at('td[@class = "title"]').content.strip, | |
:timestamp => tr.at('td[@class = "timestamp"]').content.strip | |
} | |
end | |
page += 1 | |
next_page = agent.page.link_with(:href => "./?page=#{page}") | |
break unless next_page | |
next_page.click | |
end | |
num_gathered_messages = messages.size | |
puts "Gathered #{num_gathered_messages} messages" | |
# | |
# Delete messages sent from Hatena Group | |
# | |
messages = messages.select {|m| m[:from] == 'hatenagroup' } | |
puts "Deleting #{messages.size} messages out of #{num_gathered_messages}" | |
messages.each do |m| | |
puts "Deleting message ##{m[:id]} \"#{m[:title]}\" from #{m[:from]}" | |
begin | |
agent.get(HATENA_MESSAGE_URL + "tree?#{m[:id]}").forms.first.click_button | |
rescue | |
warn "Failed to delete message ##{m[:id]}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment