[Previous] XXII | Home | [Next] Stop Conceding Points To Christianity

New Blogging Script

I wrote a new program for writing to my blog from the command line. I'm pleased. Now i can create, view, edit, and list posts, instead of just writing new ones. Here it is (of course I had to add a new controller to handle these requests and I changed my Post model, but here's the local part):

#!/usr/bin/env ruby

require "optparse"
require "curi"
require 'tempfile'
require 'net/http'

BASE_URL = 'http://curi.us/remote/'
LOCA_BASE_URL = 'http://localhost:3000/remote/'

what = nil
id = nil
$local = false

opts = OptionParser.new
opts.on("-l", "--list") {|val| what=:list}
opts.on("-n", "--new") {|val| what=:new}
opts.on("-L", "--local") {|val| $local = true}
opts.on("-e VAL", "--edit VAL") {|val| what=:edit; id = val}
opts.on("-s VAL", "--show") {|val| what=:show; id = val}

rest = opts.parse(*ARGV)

def http_post(url, post="")
  if $local
    base_url = LOCA_BASE_URL
  else
    base_url = BASE_URL
  end
  res = Net::HTTP.post_form(URI.parse(base_url+url),
  {"user" => blog_username(), "password" => blog_password(), 'post' => post})
  res
end


def textmate(input)
  if input
    tf = Tempfile.new("textmateinput")
    File.open(tf.path, "w") {|f| f.write input.strip}
    result = `mate -w < #{tf.path}`
    File.delete(tf.path)
  else
    result = `mate -w < /dev/null`
  end
  result
end

case what
when :list
  puts http_post("list").body
when :show
  puts http_post("show_pretty/#{id}").body
when :new
  blank = http_post("blank").body
  post = textmate(blank)
  puts http_post("new", post).body
when :edit
  old = http_post("show_plain/#{id}").body
  post = textmate(old)
  puts http_post("edit/#{id}", post).body
end

Elliot Temple on October 3, 2007

Comments

What do you think?

(This is a free speech zone!)