Created
January 23, 2013 21:01
-
-
Save hnaohiro/4613204 to your computer and use it in GitHub Desktop.
はてなブログへの投稿プログラム
This file contains hidden or 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 -*- | |
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