brendano /pushdot

uploads your dotfiles to dotfiles.org!
#!/usr/bin/env ruby

# uploads your dotfiles to dotfiles.org
# dotfiles.org/~brendano/pushdot

require 'pp'
require 'net/http'
require 'open-uri'
require 'yaml'
require 'rubygems'
require 'hpricot'

y = YAML.load_file "#{ENV['HOME']}/.dotfiles.org.yml"
userpass = "#{y['username']}:#{y['password']}"
user = y['username']

h = Hpricot(open("http://dotfiles.org/~#{user}/").read)
dotfiles = (h/'li'/'a').map{|a| a.inner_html}.select{|df| df =~ /^\./}

for dotfile in dotfiles
  puts dotfile
  edit_url = "http://dotfiles.org/~#{user}/#{dotfile}/edit"
  puts edit_url
  h = Hpricot(open(edit_url).read)
  post_url = (h/'form').map{|f| f['action']}.find{|a| a =~ %r{^/update}}
  post_url = "http://#{userpass}@dotfiles.org" + post_url
  puts post_url

  # pp h

  res = Net::HTTP.post_form(URI.parse(post_url), {
    'filename' => dotfile,
    'contents' => File.read("#{ENV['HOME']}/#{dotfile}"),
    'description' => (h/'textarea').find{|t| t['name']=='description'}.inner_html
  })

end