Skip to content

Instantly share code, notes, and snippets.

@hnaohiro
Created January 23, 2013 21:01
Show Gist options
  • Save hnaohiro/4613204 to your computer and use it in GitHub Desktop.
Save hnaohiro/4613204 to your computer and use it in GitHub Desktop.
はてなブログへの投稿プログラム
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'mechanize'
username = '****'
password = '****'
agent = Mechanize.new
agent.get('https://www.hatena.ne.jp/login') do |page|
response = page.form_with(:action => '/login') do |form|
form.field_with(:name => 'name').value = username
form.field_with(:name => 'password').value = password
end.click_button
if response.body.include?('The Hatena ID or password you entered does not match our records.')
puts 'error: failed to login'
exit
end
end
agent.get("http://blog.hatena.ne.jp/#{username}/#{username}.hatenablog.com/edit") do |page|
response = page.form_with(:id => 'edit-form') do |form|
form.field_with(:name => 'title').value = 'Mechanizeから投稿'
form.field_with(:name => 'body').value = 'Mechanizeから投稿しました。'
end.click_button
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment