Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created February 15, 2012 21:30
Show Gist options
  • Save ntalbott/1839112 to your computer and use it in GitHub Desktop.
Save ntalbott/1839112 to your computer and use it in GitHub Desktop.
campfire transcript export
require 'rubygems'
require 'time'
require 'fileutils'
require 'httparty'
require 'nokogiri'
require 'progress'
def help!
puts "Usage: <subdomain> <api_token>"
exit(1)
end
$subdomain = (ARGV[0] || help!)
$api_token = (ARGV[1] || help!)
def get(path, params={})
HTTParty.get(
"https://#{$subdomain}.campfirenow.com#{path}",
query: params,
basic_auth: {username: $api_token, password: 'X'}
)
end
def username(id)
@usernames ||= {}
@usernames[id] ||= Nokogiri::XML(get("/users/#{id}.xml").body).css('name').text
end
def message_to_string(message)
type = message.css('type').text
return if(type == "")
return if(%w(AdvertisementMessage IdleMessage TimestampMessage UnidleMessage SystemMessage).include?(type))
user = username(message.css('user-id').text)
body = message.css('body').text
time = Time.parse message.css('created-at').text
prefix = time.strftime('[%H:%M:%S]')
case type
when 'EnterMessage'
"#{prefix} #{user} has entered the room"
when 'KickMessage', 'LeaveMessage'
"#{prefix} #{user} has left the room"
when 'TextMessage'
"#{prefix} #{user}: #{body}"
when 'UploadMessage'
"#{prefix} #{user} uploaded '#{body}'"
when 'PasteMessage'
"#{prefix} #{user} pasted:\n#{body}"
when 'TopicChangeMessage'
"#{prefix} #{user} changed the topic to '#{body}'"
when 'ConferenceCreatedMessage'
"#{prefix} #{user} created conference #{body}"
when 'ConferenceFinishedMessage'
"#{prefix} Conference finished"
when 'TweetMessage'
"#{prefix} #{user} tweeted #{body}"
when 'AllowGuestsMessage'
"#{prefix} #{user} enabled guest access"
when 'DisallowGuestsMessage'
"#{prefix} #{user} disabled guest access"
when 'SoundMessage'
"#{prefix} #{user} played #{body}"
when 'UnlockMessage'
"#{prefix} #{user} unlocked the room"
else
r = "****Unknown Message Type: #{type} - '#{body}'" # don't want to raise and crash it
puts r
r
end
rescue
p message
raise
end
doc = Nokogiri::XML get('/rooms.xml').body
doc.css('room').each do |room_xml|
name = room_xml.css('name').text
id = room_xml.css('id').text
start_date = Date.parse(room_xml.css('created-at').text)
directory = "exports/#{$subdomain}/#{name}"
FileUtils.mkdir_p(File.join(directory, "uploads"))
empties_file = File.join(directory, 'empties')
empties = (File.exist?(empties_file) ? File.readlines(empties_file) : [])
(start_date..Date.today).with_progress(name).each do |date|
filename = File.join(directory, ("%d-%02d-%02d.txt" % [date.year, date.mon, date.mday]))
next if(File.exist?(filename) || empties.include?(filename + "\n"))
transcript = Nokogiri::XML(get("/room/#{id}/transcript/#{date.year}/#{date.mon}/#{date.mday}.xml").body)
output = []
transcript.css('message').each do |message|
string = message_to_string(message)
output << string if(string)
if message.css('type').text == "UploadMessage"
# We get the HTML page because the XML doesn't contain the URL for the uploaded file :(
html_transcript = Nokogiri::XML get("/room/#{id}/transcript/#{date.year}/#{date.mon}/#{date.mday}")
upload_name = "#{message.css('body').text}"
# There must be a better way than cycling through all the hyperlinks
html_transcript.css('a').each do |link|
if(link.text == upload_name)
open(File.join(directory, "uploads", link.text), "wb") do |file|
file.write(get(link.attr("href")))
end
break # We break because there are two links with the same file on the HTML page
end
end
end
end
if output.size > 1
open(filename, 'w') do |f|
f.puts(output.join("\n"))
end
else
open(empties_file, 'a') do |f|
f.puts(filename)
end
end
end
end
source :rubygems
gem 'fileutils'
gem 'httparty'
gem 'nokogiri'
gem 'progress'
GEM
remote: http://rubygems.org/
specs:
fileutils (0.7)
rmagick (>= 2.13.1)
httparty (0.8.1)
multi_json
multi_xml
multi_json (1.0.4)
multi_xml (0.4.1)
nokogiri (1.5.0)
progress (2.4.0)
rmagick (2.13.1)
PLATFORMS
ruby
DEPENDENCIES
fileutils
httparty
nokogiri
progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment