processi

about processes and engines

posting to wordpress via ruby and atompub

with 13 comments

The ruby script here uploads posts to WordPress blogs via AtomPub.

Once you’ve set your blog coordinates in the script (around line 20), you can post like that :

echo "I love Ruby" | post.rb -t "title" -c "thoughts,ruby"

The script will publish your post and then ouptput the WordPress server reply.


#!/usr/bin/env ruby

# Copyright (c) 2007 John Mettraux
# Released under the MIT license
# http://www.opensource.org/licenses/mit-license.php

require 'optparse'
require 'net/http'

require 'rubygems'
require 'atom/entry' # sudo gem install atom-tools
require 'atom/collection'

    # a great thanks to the devs of all the libs used here

#
# some info about you and your blog

blog = "yourblog"
authorname = "Your Name"
username = "x"
password = "y"

bloguri = "http://#{blog}.wordpress.com"
base = "https://#{blog}.wordpress.com/wp-app.php"

#
# parse options

tags = []
title = nil
type = 'html'

opts = OptionParser.new
opts.banner = "Usage: post.rb [options]"
opts.separator ""
opts.separator "options :"

opts.on(
    "-c",
    "--categories {list}",
    "comma separated list of tags/categories") do |v|

    tags = v.split ","
end

opts.on(
    "-t",
    "--title {title}",
    "title for the post") do |v|

    title = v
end

opts.on(
    "-T",
    "--type {html|xhtml|text}",
    "type of the content. ('html' is the default).") do |v|

    type = v
end

opts.on(
    "-h",
    "--help",
    "displays this help") do

    puts
    puts opts.to_s
    puts
    exit 0
end

opts.parse ARGV

raise "please specify a title for the post with the -t option" \
    unless title

#
# gather content

content = ""
loop do
    line = STDIN.gets
    break unless line
    content += line
end

#
# create entry

entry = Atom::Entry.new
entry.title = title
#entry.updated = Time.now.httpdate
entry.updated!

author = Atom::Author.new
author.name = authorname
author.uri = bloguri
entry.authors << author

tags.each do |t|
    c = Atom::Category.new
    c["scheme"] = bloguri
    c["term"] = t.strip
    entry.categories << c
end

entry.content = content
entry.content["type"] = type if type

#puts entry.to_s

h = Atom::HTTP.new
h.user = username
h.pass = password
h.always_auth = :basic

c = Atom::Collection.new(base + "/posts", h)
res = c.post! entry

puts res.read_body

Next step for me, integration of the technique into OpenWFEru.

Updated 2007/11/11 : following the suggestions of Brendan Taylor, the author of atom-tools, I simplified the scripts, now relying on atom-tools for posting the entry.

Enjoy.

Written by John Mettraux

November 5, 2007 at 1:25 am

Posted in atom, atompub, rest, ruby, wordpress

13 Responses

Subscribe to comments with RSS.

  1. [...] Use ruby and atompub to post to WordPress [...]

  2. I can’t get this to work…
    I have WordPress hosted on my site (I don’t know if this makes a difference).
    At first I got an ECONNREFUSED error.
    I changes the https in the base string to http, and I got “No input file specified”.
    I also have a (related?) problem with my pipe:
    I get “The process tried to write to a nonexistent pipe”…

    Please help?

    Tal.

    Tal Weiss

    March 31, 2008 at 11:06 am

  3. Hi Tal,

    please send me a more detailed error report at jmettraux@openwfe.org

    Include details about which version of Ruby you utilise and perhaps make sure your WordPress has its AtomPub API turned on (I don’t know anything about WP administration as I gladly uses WordPress.com only).

    Cheers,

    John

    John Mettraux

    March 31, 2008 at 12:18 pm

  4. [...] posting to wordpress via ruby and atompub (tags: blog wordpress atom ruby web programming) [...]

  5. Strange thing is, I had build an auto email-to-wordpress system that pulled from a POP3 box, did some processing of attachments and the like and uploaded entries to WordPress… I last used in the first part of January 2008. I wanted to reactivate it, but now get weird errors when it tries to post, something about not having the right credentials even though it is logging in, getting the collections, and using that information to post the entries. It may be something the WP admin changed, or some WP change itself, but I was wondering if anyone else had seen this.

    SteveP

    June 11, 2008 at 4:06 am

  6. Hello Steve,

    you should check the wordpress.com forums. If there is no answer there, fill a support request, they reply usually within one or two days.

    http://en.forums.wordpress.com/
    http://wordpress.com/contact-support/

    Cheers,

    John Mettraux

    June 11, 2008 at 4:11 am

  7. Error is:

    Error in process_queue:
    Atom::Unauthorized You must provide a username and password /usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:266:in `username_and_password_for_realm’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:234:in `basic_authenticate’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:381:in `send’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:381:in `dispatch_authorization’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:312:in `http_request’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:333:in `http_request’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/http.rb:169:in `post’/usr/lib/ruby/gems/1.8/gems/atom-tools-2.0.1/lib/atom/collection.rb:97:in `post!’./sga_repo.rb:122:in `post_entry’./pop_handler.rb:101:in `handle_email’./pop_handler.rb:44:in `process_queue’./pop_handler.rb:42:in `each’./pop_handler.rb:42:in `process_queue’/usr/lib/ruby/1.8/net/pop.rb:427:in `start’./pop_handler.rb:39:in `process_queue’/usr/lib/ruby/1.8/timeout.rb:56:in `timeout’/usr/lib/ruby/1.8/timeout.rb:76:in `timeout’./pop_handler.rb:36:in `process_queue’./upd_sga_repo.rb:16

    SteveP

    June 11, 2008 at 4:12 am

  8. Steve,

    > Atom::Unauthorized You must provide a username and password

    seems like you’re not providing a username and a password (or maybe they are outdated).

    The example I have blogged here used atom-tools 0.9.3.

    Best regards.

    update : I just tested this script with atom-tools 2.0.1 and it works fine.

    John Mettraux

    June 11, 2008 at 4:15 am

  9. Strange. Well, I know I hadn’t changed anything, so it must be something the WP admin did or something in the WP updates. I just wanted to verify it wasn’t a general problem, at least now I know it’s all mine. ;)

    SteveP

    June 11, 2008 at 5:02 am

  10. [...] posting to wordpress via ruby and atompub « processi (tags: atom blog blogging ruby wordpress atompub) [...]

  11. [...] pm on July 10, 2008 | # | Testing out posting to WordPress via Ruby and ATOM. From here; http://jmettraux.wordpress.com/2007/11/05/posting-to-wordpress-via-ruby-and-atompub/ [...]

  12. [...] [WORDPRESS] posting to wordpress via ruby and atompub, jmettraux.wordpress.com [...]

  13. [...] posting to wordpress via ruby and atompub « processi [...]


Leave a Reply