The Accidental Rubyist

invalid byte sequence in UTF-8

Ruby Blogging Client for Live Journal – Part 2

leave a comment »

This is a continuation of Part 1. Please see details in that post.

The previous post described a Ruby cum shell program for posting to a LiveJournal account.
Having since discovered the File.expand_path method, I am now able to make it into a pure ruby program than can be invoked from Vim or some other editor.

What stopped me earlier was that I was unable to read up a file prepended with “~”, and thus was picking up from the current folder.

So now the file is ljpost.rb, and can be placed anywhere in PATH. The config file with user and password is “~/.lwrc” in YAML format (Yaml rocks!). I am as yet calling curl using the system command.

The command from Vim is simply “:! ljpost.rb %“.

The format is simply:


  • First line is subject

  • The rest is content or body.

You may call it from anywhere on the command line too.

The file listing is as follows (source):

#!/usr/bin/env ruby
require 'yaml'
# takes a file for LiveJournal. First line is subject. Rest is the data to be encoded.
#
if ARGV.size == 0 then
  puts "Please give a file to send to LiveJournal"
  exit 1
end
file=ARGV[0]
config=File.expand_path("~/.lwrc")
mymap = YAML::load( File.open(config));
if mymap == nil then
  puts "Could not find config file: %s" % config
  return 1
end
t=Time.now
buff=t.strftime("year=%Y&mon=%m&day=%d&hour=%H&min=%M")
buff << "&mode=postevent&user="
buff << mymap["user"]
buff << ("&password=" + mymap["user1"] + "&")
f = File.new(file)
fline = f.readline.chomp!;
fline.gsub!(' ', '+')
buff <<  "subject=%s&event=" % fline;
f.each_byte { |x|
  if (x >= 65 && x <= 90) ||  (x >= 97 && x <= 122) || (x >= 48 && x <= 57) then
    buff << x
  elsif x == 32 then
    buff << "+"
  else
    buff << "%" + ("%02X" % x)
  end
}
tmpfile=".t.t1111"
File.open( tmpfile, 'w' ) do |f|
         f << buff
end
result=system("curl -d @%s  http://www.livejournal.com/interface/flat" % tmpfile)
print "System returned: %s" % result
File.unlink(tmpfile)

(I trust WP will gobble many < so please refer the source linked above.)
If it does not come through, i will push the source through the encoder and send it.

[Edited in Vim using HTML.vim]

Advertisements

Written by totalrecall

March 7, 2007 at 1:32 am

Posted in Uncategorized

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: