#!/usr/bin/env ruby require "net/http" require "net/https" require "rexml/document" DEBUG_ME = true #http://code.google.com/apis/documents/overview.html class GoogleFile attr_reader :document_type,:id_url,:name,:updated,:category,:email def initialize(data,key) @document_type = nil @id_url = nil @name = nil @updated = nil @category = nil #??? @title = nil @link_self = nil @link_alt = nil @email = nil if data.kind_of?(String) data = REXML::Document.new(data,{:ignore_whitespace_nodes =>true}) end #give me some freaking REXML thing here... if data.kind_of?(REXML::Document) ||data.kind_of?(REXML::Element) parse_xml(data) elsif data.kind_of?(hash) parse_hash(data) else raise "I do not know this data type. Give me a string rexml or hash`" end @key = key end def download headers={ "Authorization"=> "GoogleLogin auth=#{@key}" } uri = URI.parse(@link_self) puts uri if DEBUG_ME http2 = Net::HTTP.new(uri.host) http2.use_ssl = true if uri.kind_of?(URI::HTTPS) got = http2.get(uri.path,headers) data = REXML::Document.new(got.response.body.strip) uri = URI.parse(data.get_elements("entry/content").first.attributes["src"]) puts uri http2 = Net::HTTP.new(uri.host) http2.use_ssl = true if uri.kind_of?(URI::HTTPS) got = http2.get(uri.path,headers) got end def upload xml=< example spreadsheet PARAMS end def filename end def file_type end private def parse_xml(data) @document_type = data.get_elements("category").first.attributes["label"] @id_url = data.get_elements("id").first.text @name = data.get_elements("author/name").first.text @updated = data.get_elements("updated").first.text @category = data.get_elements("category").first.text @title = data.get_elements("title").first.text data.get_elements("link").each{|ele| case ele.attributes["rel"].strip when "self" @link_self = ele.attributes["href"] when "alternate" @link_alt = ele.attributes["href"] else puts "we have extra link objects #{ele.to_s}" if DEBUG_ME end } @email = data.get_elements("author/email").first.text end end class GoogleDocument #Email=sbeckeriv@gmail.com&Passwd=moocows&service=writely&analytics=DeathByEscalator-QuidKiller-6.6.6')) SERVICE_NAME = "writely" ACCOUNT_TYPE = "HOSTED_OR_GOOGLE" def initialize(email,password,source=nil) @source = source || "Ruby_lib" @email = email.split("@").first @password = password @auth_response = {} auth_key end def list_documents #set header headers={ "Authorization"=> "GoogleLogin auth=#{@auth_response["Auth"]}" } #default path for document list http2 = Net::HTTP.new("doc.google.com") #get all docus got = http2.get("/feeds/documents/private/full",headers) x = got.response.body.strip #parse feeds in to objects xml = REXML::Document.new(x) #figure out a way to share the auth key and get method xml.get_elements('/feed/entry/').collect{|node| GoogleFile.new(node,@auth_response["Auth"])} end alias_method :list,:list_documents def upload end def upload_word_document end def upload_spreadsheet_document end def download_all_word_documents end def download_word_document end def search end def auth auth_key end private #does not handle captcha #gets the auth key if it is not there #http://code.google.com/apis/accounts/AuthForInstalledApps.html def auth_key return @auth_response["Auth"] unless @auth_response["Auth"].nil? http = Net::HTTP.new("www.google.com",443) http.use_ssl = true response = http.post('/accounts/ClientLogin', URI.encode("accountType=#{ACCOUNT_TYPE}&Email=#{@email}@gmail.com&Passwd=#{@password}&service=#{SERVICE_NAME}&source=#{@source}")) auth_response={} raise "Error: BadAuthentication\n#{response.body}" if response.body.downcase.include?("captcha") raise "Error: BadAuthentication\n#{response.body}" if response.body.include?("Error=BadAuthentication") response.body.split("\n").each{|x| y =x.split("=") @auth_response.merge!(y[0]=>y[1]) } @auth_response["Auth"] end end if $0==__FILE__ gdoc = GoogleDocument.new(ARGV[0],ARGV[1]) files = gdoc.list pp files files.select{|x| x.document_type=="spreadsheet"}.first.download end