Skip to content

Instantly share code, notes, and snippets.

@koseki
Created June 26, 2009 18:04
Show Gist options
  • Save koseki/136631 to your computer and use it in GitHub Desktop.
Save koseki/136631 to your computer and use it in GitHub Desktop.
WordPress Client using WWW::Mechanize
require "rubygems"
require "www/mechanize"
# WordPress Client using WWW::Mechanize.
class WpClient
attr_accessor :m
def initialize(baseurl)
@base = baseurl
@base = @base[0..-2] if @base[-1] == ?/
end
# Login to WordPress.
def login(user,pass)
@m = WWW::Mechanize.new
page = @m.get(@base + "/wp-login.php")
login_form = page.forms_with("loginform").first
login_form.log = user
login_form.pwd = pass
dashboard = login_form.click_button
dashboard_title_ja = "ダッシュボード"
dashboard_title_en = "Dashboard"
unless dashboard.title.index(dashboard_title_ja)
raise("Login failed: " + dashboard.title)
end
return self
end
# Create WpEntry. arg is post ID, entry url or nil(new entry).
def get(arg = nil)
if arg
return WpEntry.new(editform(edit(arg)))
else
return WpEntry.new(createform)
end
end
# Get edit page. arg is porst ID or entry url.
def edit(arg)
if arg =~ %r{^https?://}
pid = post_id(arg)
elsif arg =~ /^\d+$/
pid = arg.to_i
end
url = @base + "/wp-admin/post.php?action=edit&post=#{pid}"
return @m.get(url)
end
# Get post ID from entry url.
def post_id(url)
page = @m.get(url)
postid = page.content[%r{#{@base}/wp-admin/post\.php\?action=edit&post=(\d+)}, 1]
raise "Edit page not found: #{url}" unless postid
return postid.to_i if postid
end
# Get edit form from entry page.
def editform(page)
return page.forms_with(:name => "post").first
end
# Get new entry edit form.
def createform
url = @base + "/wp-admin/post-new.php"
page = @m.get(url)
return editform(page)
end
end
# WordPress Entry model.
class WpEntry
FIELDS = {
:title => "post_title",
:content => "content",
:post_name => "post_name",
:tags => "tax_input[post_tag]",
:ping_status => ["ping_status", :checkbox, "open", nil],
:comment_status => ["comment_status", :checkbox, "open", nil],
}
attr_accessor :form
def initialize(form)
@form = form
end
FIELDS.each do |key,real|
define_method(key) do
return @form[real]
end
if real.is_a? String
define_method("#{key}=") do |value|
@form[real] = value
end
elsif real[1] == :checkbox
define_method("#{key}=") do |value|
@form[real[0]] = value ? value[2] : value[3]
end
end
end
def to_s
result = ""
[:title, :post_name, :tags, :content].each do |k|
result << "#{k}: #{@form[FIELDS[k]]}\n"
end
return result
end
def save
@form.click_button(@form.button("publish"))
end
# Save draft (only when create new entry).
def save_draft
@form.click_button(@form.button("save-post"))
end
end
if __FILE__ == $0
baseurl = "http://example.com/"
user = "user"
pass = "pass"
wpc = WpClient.new(baseurl).login(user,pass)
entry = wpc.get("http://example.com/2009/07/01/postname1")
entry.title = "new title"
entry.content = "hello wordpress."
entry.tags = "sample,tools,github"
entry.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment