Skip to content

Instantly share code, notes, and snippets.

@koseki
Created July 20, 2009 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save koseki/150470 to your computer and use it in GitHub Desktop.
Save koseki/150470 to your computer and use it in GitHub Desktop.
super easy wordpress client.
require "rubygems"
require "www/mechanize"
class WpClient
attr_accessor :m
def initialize(baseurl, opts = {})
@base = baseurl
@base = @base[0..-2] if @base[-1] == ?/
@opts = opts
end
def login(user,pass)
@m = WWW::Mechanize.new
auth = @opts[:http_basic_authentication]
@m.auth(auth[0],auth[1]) if auth
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_page_title = "Dashboard"
dashboard_page_title = "ダッシュボード"
unless dashboard.title.index(dashboard_page_title)
raise("Login failed: " + dashboard.title)
end
return self
end
# 記事のurlから編集ページを取得します。HTMLから正規表現で抜き出すだけです。
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
# 指定されたエントリーの編集画面を返します。
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
def editform(page)
return page.forms_with(:name => "post").first
end
def createform
url = @base + "/wp-admin/post-new.php"
page = @m.get(url)
return editform(page)
end
# 指定されたエントリーURLのWpEntryを返します。
def get(arg = nil)
if arg
return WpEntry.new(editform(edit(arg)))
else
return WpEntry.new(createform)
end
end
end
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],
:year => "aa",
:month => "mm",
:day => "jj",
:hour => "hh",
:min => "mn",
:sec => "ss",
}
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 date=(date)
(y,m,d,hh,mm,ss) = date.strftime("%Y,%m,%d,%H,%M,%S").split(",")
self.year = y
self.month = m
self.day = d
self.hour = hh
self.min = mm
self.sec = ss
return date
end
def date
return Time.local(year.to_i,month.to_i,day.to_i,hour.to_i,min.to_i,sec.to_i)
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
# 新規作成時に下書きで保存します。
def save_draft
@form.click_button(@form.button("save-post"))
end
end
if __FILE__ == $0
url = "http://wordpress.local/"
user = "admin"
pass = "admin"
wpc = WpClient.new(url).login(user,pass)
entry = wpc.get
entry.title = "title"
entry.content = "body"
entry.post_name = "abcde"
entry.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment