Skip to content

Instantly share code, notes, and snippets.

@tily
Created December 30, 2009 09:57
Show Gist options
  • Save tily/265955 to your computer and use it in GitHub Desktop.
Save tily/265955 to your computer and use it in GitHub Desktop.
delete hatenabookmark tag
require 'uri'
require 'rubygems'
require 'highline'
require 'mechanize'
class HatenaBookmark
ENDPOINT = 'http://b.hatena.ne.jp/'
def initialize(username, password)
@username = username
@mech = WWW::Mechanize.new
@rks = login(username, password)
end
def login(username, password)
@mech.get('https://www.hatena.ne.jp/login')
@mech.page.form_with(:action => '/login') do |f|
f.field_with(:name => 'name').value = username
f.field_with(:name => 'password').value = password
f.click_button
end
@mech.get(ENDPOINT + username)
@mech.page.content.match(/'#{username}',\s'(.+)'/)[1]
end
def rename_tag(old_name, new_name)
end
def delete_tag(name)
@mech.post(
"/#{@username}/tag.delete",
'tag' => name,
'rks' => @rks
)
end
def get_all_tags
tags = []
@mech.get(ENDPOINT + @username)
@mech.page.search('//ul[@id="tags"]/li').each do |li|
name = li.search('.//a').text
num = li.search('.//span').text.match(/\((\d+)\)/)[1]
tags.push({:name => name, :num => num.to_i})
end
tags
end
end
hb = HatenaBookmark.new(
HighLine.new.ask('username: '),
HighLine.new.ask('password: ') {|q| q.echo = '*' }
)
hb.get_all_tags.each do |tag|
if tag[:num] <= 10
hb.delete_tag(tag[:name])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment