Created
November 2, 2009 22:56
-
-
Save foca/224578 to your computer and use it in GitHub Desktop.
Get formatted Adium chat transcripts into STDOUT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# Save this somewhere, chmod 755 it, then add | |
# complete -C path/to/this/script -o default transcript | |
# to your ~/.bashrc | |
exit 0 unless /^transcript\b/ =~ ENV["COMP_LINE"] | |
TRANSCRIPTS_DIR = File.expand_path("~/Library/Application\ Support/Adium\ 2.0/Users/Default/Logs") | |
puts Dir[TRANSCRIPTS_DIR + "/*/#{$'.strip}*"].map {|buddy| File.basename(buddy) }.uniq | |
exit 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require "nokogiri" | |
require "pathname" | |
require "time" | |
module Purple | |
TRANSCRIPTS_DIR = Pathname("~/Library/Application\ Support/Adium\ 2.0/Users/Default/Logs").expand_path | |
class Transcript | |
def self.all_files(buddy_name) | |
Dir.glob(TRANSCRIPTS_DIR.join("*/#{buddy_name}/**/*")).sort_by {|file| File.basename(file) } | |
end | |
def self.last_file(buddy_name) | |
all_files(buddy_name).last | |
end | |
def initialize(transcript) | |
@messages = Nokogiri(transcript).search("message") | |
end | |
def pretty_print(out=STDOUT) | |
@messages.each do |message| | |
out.print timestamp(message), " " | |
out.print nickname(message), " " | |
out.puts message.inner_text | |
end | |
end | |
def max_nickname_size | |
@max_nickname_size ||= @messages.map {|m| (m["alias"] || m["sender"]).size }.max | |
end | |
def nickname(message) | |
"<#{message["alias"] || message["sender"]}>".rjust(max_nickname_size + 2) | |
end | |
def timestamp(message) | |
"[" + Time.parse(message["time"]).strftime("%H:%M:%S") + "]" | |
end | |
end | |
end | |
if $0 == __FILE__ | |
banner = <<-END_HELP | |
Usage: #{File.basename($0)} <buddy_name> | |
Display a nicely formatted Adium chat transcript to STDOUT. For example: | |
#{File.basename($0)} [email protected] | |
#{File.basename($0)} [email protected] | |
#{File.basename($0)} clever_aim_handle | |
This will get the latest (or currently active) chat transcript for the selected | |
buddy. | |
END_HELP | |
abort banner if ARGV.empty? | |
file = Purple::Transcript.last_file(ARGV[0]) | |
abort "#{ARGV[0]} has no transcripts\n\n#{banner}" unless file && File.file?(file) | |
Purple::Transcript.new(File.read(file)).pretty_print | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment