29
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RubyのMechanizeではてなブログへ投稿

Last updated at Posted at 2013-01-23

はてなブログのエンドポイントがわからなかったので、Mechanizeで作ってみました。


Mechanizeをインストールしておいて下さい。

gem install mechanize

はてなのIDとパスワードを各自設定してください。

hatena.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
              
require 'mechanize'
require 'pp'  
              
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

下から3行目と4行目を変更すれば、記事のタイトルと内容を変更できます。

カテゴリ指定もできるといいなぁ

29
30
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
30

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?