#!/usr/bin/env ruby require "net/http" require "net/https" require "pp" require "rexml/document" module Net class HTTPS < HTTP def initialize(address, port = nil) super(address, port) self.use_ssl = true end end end class Blogger class < self GOOGLE_LOGIN_URL = URI.parse('https://www.google.com/accounts/ClientLogin') BLOGGER_FEED_REGEX = /\/feeds\/(.*?)\/posts\/default/ def get_gdata_headers(email,pass,service = 'blogger',source = 'gdata-ruby' ,url = 'www.blogger.com') headers = {} response = Net::HTTPS.post_form(GOOGLE_LOGIN_URL, {'Email' => email, 'Passwd' => pass, 'source' => source, 'service' => service }) raise "Not Authorized" unless response.kind_of? Net::HTTPSuccess headers['Content-Type'] = 'application/atom+xml' headers['Authorization'] = "GoogleLogin auth=#{response.body.split(/=/).last}" headers end def build_atom_entry_xml(entry) <<-EOENTRY #{entry[:title]}

#{entry[:body]}

EOENTRY end def post_xml_to_blog(base_url, headers, blog_url,post_array) conn = Net::HTTP.new(base_url, 80) list_of_blogs = conn.get("/feeds/default/blogs",headers) blog_xml = find_blog_by_url_from_xml(blog_url,list_of_blogs.body) blog_links = blog_xml.each_element("link"){|mr| mr} post_url = blog_links.select{|e| e.attributes["href"] =~ BLOGGER_FEED_REGEX }.first.attributes["href"] post_path = post_url.match(BLOGGER_FEED_REGEX).to_s response = Array.new post_array.each{|entry| response << conn.post(post_path, build_atom_entry_xml(entry).to_s, headers) } response end def find_blog_by_url_from_xml(blog_url,xml) xml_of_blogs = REXML::Document.new(xml) blogs = xml_of_blogs.each_element("/*/entry"){|z| z} urls = blogs.map{|e| e.each_element("link"){|mr| mr}[0].attributes["href"] } index = urls.index(urls.select{|u| u.include?(blog_url)}.first) blogs[index] end def send_ical_to_blogger headers = get_gdata_headers(USERNAME,PASSWORD) response = post_xml_to_blog("www.blogger.com",headers,"http://sbeckeriv.blogspot.com/", [{:title=>"test2",:body=>"nothing to see here"}]) end end end