Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 57 additions and 7 deletions.
  1. +3 −1 bin/campfire_export
  2. +54 −6 lib/campfire_export.rb
View
4 bin/campfire_export
@@ -58,9 +58,11 @@ begin
config['api_token'])
account.find_timezone
account.rooms.each do |room|
- room.export(config_date(config, 'start_date'),
+ room.export(config_date(config, 'start_date'),
config_date(config, 'end_date'))
end
+ account.export_rooms_list
+ account.export_users_list
rescue => e
puts "Unable to export account: #{e}"
end
View
60 lib/campfire_export.rb
@@ -52,17 +52,23 @@ def zero_pad(number)
"%02d" % number
end
- # Requires that room and date be defined in the calling object.
+ # If room or date are defined in the calling object they will be used to
+ # build a more accurate directory.
def export_dir
- "campfire/#{Account.subdomain}/#{room.name}/" +
- "#{date.year}/#{zero_pad(date.mon)}/#{zero_pad(date.day)}"
+ dir = "campfire/#{Account.subdomain}"
+ if defined? room:
+ dir += "/#{room.name}"
+ end
+ if defined? date:
+ dir += "/#{date.year}/#{zero_pad(date.mon)}/#{zero_pad(date.day)}"
+ end
+ dir
end
- # Requires that room_name and date be defined in the calling object.
def export_file(content, filename, mode='w')
# Check to make sure we're writing into the target directory tree.
true_path = File.expand_path(File.join(export_dir, filename))
-
+
unless true_path.start_with?(File.expand_path(export_dir))
raise CampfireExport::Exception.new("#{export_dir}/#{filename}",
"can't export file to a directory higher than target directory; " +
@@ -131,9 +137,10 @@ class Account
@api_token = ""
@base_url = ""
@timezone = nil
+ @user_ids = []
class << self
- attr_accessor :subdomain, :api_token, :base_url, :timezone
+ attr_accessor :subdomain, :api_token, :base_url, :timezone, :user_ids
end
def initialize(subdomain, api_token)
@@ -152,6 +159,41 @@ def rooms
doc = Nokogiri::XML get('/rooms.xml').body
doc.css('room').map {|room_xml| Room.new(room_xml) }
end
+
+ def export_rooms_list
+ begin
+ log(:info, "Exporting rooms list ... ")
+ FileUtils.mkdir_p export_dir
+ xml = Nokogiri::XML get('/rooms.xml').body
+ export_file(xml, 'rooms.xml')
+ log(:info, "ok\n")
+ rescue Exception => e
+ log(:error, "Exporting rooms list failed", e)
+ end
+ end
+
+ def export_users_list
+ # Campfire doesn't provide a /users.xml API to get all the users, so
+ # we'll accumulate the list of user_ids seen in room messages and then
+ # query each one individually to simulate it.
+ # This won't be of any use unless run after exporting room transcripts.
+ doc = Nokogiri::XML.parse('<users type="array"></users>', nil, 'UTF-8')
+ users_node = doc.css('users').first
+
+ begin
+ log(:info, "Exporting users list ... ")
+ FileUtils.mkdir_p export_dir
+ Account.user_ids.each do |user_id|
+ xml = Nokogiri::XML get("/users/#{user_id}.xml").body
+ users_node.add_child(xml.css('user'))
+ end
+ export_file(doc, 'users.xml')
+ log(:info, "ok\n")
+ rescue Exception => e
+ log(:error, "Exporting users list failed", e)
+ end
+ end
+
end
class Room
@@ -326,6 +368,12 @@ def username(user_id)
rescue Exception => e
"[unknown user]"
else
+ # Record this as a user_id we've seen in the account's rooms. Since
+ # Campfire doesn't provide a /users.xml API (wtf), we record all the
+ # IDs we've seen and then use them to export a list of users.
+ # See Account.export_users_list()
+ Account.user_ids.push(user_id)
+
# Take the first name and last initial, if there is more than one name.
name_parts = doc.css('name').text.split
if name_parts.length > 1

No commit comments for this range

Something went wrong with that request. Please try again.