Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created July 27, 2009 22:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msonnabaum/156764 to your computer and use it in GitHub Desktop.
Save msonnabaum/156764 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'rubygems'
require 'gdata'
require 'zlib'
require 'fastercsv'
require 'twitter'
=begin
This script was used at DrupalCamp Dallas 09 to post schedule changes made on a google spreadsheet to twitter.
=end
def truncate_text(text, length)
truncated = text[0, length] + '…'
return truncated
end
class GoogSS
attr_reader :results, :diff
def initialize(key = nil)
client = GData::Client::DocList.new
begin
@feed = client.get("http://spreadsheets.google.com/pub?key=#{key}&single=true&gid=0&output=csv")
@results = FasterCSV.parse(@feed.body, {:converters => :all} )
rescue
end
end
def store(filename, options={})
marshal_dump = Marshal.dump(@results)
file = File.new(filename,'w')
file = Zlib::GzipWriter.new(file) unless options[:gzip] == false
file.write marshal_dump
file.close
end
def load(filename)
begin
file = Zlib::GzipReader.open(filename)
rescue Zlib::GzipFile::Error
file = File.open(filename, 'r')
ensure
obj = Marshal.load file.read
file.close
@results = obj
end
end
def compare(results2)
@diff = []
@results.each_with_index do |r, i|
r.each_with_index do |c, ci|
if c != results2[i][ci]
tweettext = "#{@results[0][ci]}: #{r[0]} #{c}"
if results[0][ci] == 'Birds of a Feather (RM 3)'
tweettext = truncate_text(tweettext, 93)
tweettext = "new/changed #{tweettext} http://bit.ly/2cbhpv #drupald09"
else
tweettext = truncate_text(tweettext, 83)
tweettext = "new/changed session - #{tweettext} http://bit.ly/2cbhpv #drupald09"
end
puts tweettext
@diff << tweettext
end
end
end
end
def print
@results.each {|r| puts "#{r[0]} #{r[3]}"}
end
end
## load the latest schedule
g = GoogSS.new('toRhKaXT0cNy0WGOAJyvFHg')
## load the last stored schedule
begin
g2 = GoogSS.new()
g2.load('googdoc.marshal')
## compare schedules, populate the diff array
g.compare(g2.results)
## post diff to twitter
httpauth = Twitter::HTTPAuth.new('username', 'password')
base = Twitter::Base.new(httpauth)
g.diff.each {|d| base.update(d)}
rescue
end
## store the latest schedule
g.store('googdoc.marshal')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment