Created
March 14, 2010 01:07
-
-
Save peterc/331684 to your computer and use it in GitHub Desktop.
WordPressr - WordPress posting client library for Ruby
This file contains 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
require 'rubygems' | |
require 'mime/types' | |
require 'xmlrpc/client.rb' | |
require 'uri' | |
# WordPressr - WordPress posting client library for Ruby | |
# By Peter Cooper | |
# | |
# LICENSE: | |
# MIT, BSD, whatever. Use the code at your own risk, I'm not | |
# responsible if it kills your dog, etc. | |
# | |
# DEPENDENCIES: | |
# - uri and xmlrpc/client are in the stdlib | |
# - mime/types, just do a gem install mime-types | |
# - A WordPress powered blog.. d'oh! | |
# | |
# TODO: | |
# - Exceptions. Currently none. If something fails, that's it, you're done. | |
# (It's better than crappily handled exceptions, at least..) | |
class WordPressr | |
# Set up a Wordpressr object by getting an XMLRPC client going and storing the username and password | |
def initialize(username, password, url) | |
@username, @password = username, password | |
uri = URI.parse(url) | |
@xmlrpc = XMLRPC::Client.new(uri.host, uri.path, uri.port) | |
end | |
# Get the categories associated with the blog | |
# Returns an array of hashes (one per category). Hash keys include categoryName, categoryId, description, htmlUrl, rssUrl, and categoryDescription | |
def get_categories | |
req("wp.getCategories") | |
end | |
def new_post(content, publish = false) | |
req("metaWeblog.newPost", content) | |
end | |
# Upload a file to the blog | |
# Returns a hash like { "url" => "..", "type" => "..", "file" => ".." } | |
def upload_file(filename) | |
req("wp.uploadFile", { :name => filename, :bits => XMLRPC::Base64.new(File.read(filename)), :type => MIME::Types.type_for(filename).first.content_type }) | |
end | |
# metaWeblog.newPost | |
private | |
# Perform a request with the XMLRPC client | |
def req(m, *args) | |
@xmlrpc.call(m, 0, @username, @password, *args) | |
end | |
end | |
# If running this file directly, do some stuff.. this is mostly for you to use as an example for your own use | |
if $0 == __FILE__ | |
wp_username = "admin" | |
wp_password = "" | |
wp_xmlrpc_url = "http://www.example.com/xmlrpc.php" | |
blog = WordPressr.new(wp_username, wp_password, wp_xmlrpc_url) | |
p blog.get_categories | |
uploaded_file = blog.upload_file("image.png") | |
url = uploaded_file["url"] | |
content = { | |
:title => "A test post", | |
:description => "Hello world, this is a test", | |
:wp_slug => "a-test-post", | |
:mt_allow_comments => 1, # 1 - open, 0 - closed (2 might mean yes, but no new ones) | |
:mt_allow_pings => 1, # ditto | |
:excerpt => "blah blah blah", # optional | |
:categories => ["Elsewhere", "Cool"] | |
} | |
# default is to NOT publish and only add to drafts.. add an extra 'true' argument to publish | |
p blog.new_post(content) | |
end | |
# Miscellaneous crud.. | |
# | |
# Some useful stuff for when exceptions are implemented.. | |
# rescue XMLRPC::FaultException => e | |
# puts "ERROR: #{e.faultCode} #{e.faultString}" | |
# end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment