The Accidental Rubyist

invalid byte sequence in UTF-8

A ruby XML-RPC client for Live Journal

leave a comment »

Today morning I wrote a small XML-RPC program using ruby to post a blog to Live Journal. It is pure ruby (no curl). The positive aspect of this is that it does not require that awful encoding that i had to do with the curl client published earlier.

It is very clean code. However, I don’t think xmlrpc comes with the stock ruby install (i did not check before). So one has to install expat, xmlparser and xmlrpc4r before being able to use xmlrpc. But with this one can access many services that the server exposes.

Here is the code, without further ado.

#!/usr/bin/env ruby
require "xmlrpc/client"
require 'yaml'
# takes a file for LiveJournal. First line is subject. Rest is the data to be encoded.
# totalrecall: 20070307 01:35:47 - completely free in all respects
if ARGV.size == 0 then
  puts "Please give a file to send to LiveJournal"
  exit 1
end
file=ARGV[0]
config=File.expand_path("~/.ljrc")
mymap = YAML::load( File.open(config));
if mymap == nil then
  puts "Could not find config file: %s" % config
  return 1
end
t=Time.now
mymap["year"]=t.year
mymap["mon"]=t.mon
mymap["day"]=t.day
mymap["hour"]=t.hour
mymap["min"]=t.min
mymap["mode"]="postevent"
mymap["lineendings"]="unix"
f = File.new(file)
fline = f.readline.chomp!;
mymap["subject"]=fline
text = f.readlines.join
mymap["event"]=text
mymap["username"] = mymap["user"]
mymap["password"] = mymap["user1"]
#mymap.delete["user"]
#mymap.delete["user1"]

# Make an object to represent the XML-RPC server.
begin
server = XMLRPC::Client.new( "www.livejournal.com", "/interface/xmlrpc")
# # Call the remote server and get our result
result = server.call("LJ.XMLRPC.postevent", mymap );

puts result
rescue XMLRPC::FaultException => e
    puts "Error:"
    puts e.faultCode
    puts e.faultString
end

The first line of the file, goes as “subject”, the content goes as “event”. All these are put into a Hash and sent. The hash corresponds to the XML spec published by LiveJournal.

You may call this from your favorite editor (i.e. Vim) using “! ljrpc.rb %” or call from the command-line passing a file. User name and password are in “~/.ljrc(lwrc in the previous post was an error).
Or you may set a key mapping in Vim for either of these 2 posting methods as:
map ;ljp :!ljrpc.rb %<CR>

The source of ljrpc.rb is here.

[Edited in Vim using HTML.vim] Posted by mtsend.py

Advertisements

Written by totalrecall

March 7, 2007 at 2:48 am

Posted in ruby, unix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: