Skip to content

Instantly share code, notes, and snippets.

@ox
Last active December 17, 2015 13:29
Show Gist options
  • Save ox/5617961 to your computer and use it in GitHub Desktop.
Save ox/5617961 to your computer and use it in GitHub Desktop.
Pass in names of contacts and this script will create a table of messages received from those people according to date. $ruby texting.rb "Sam Smith" "Bobby Lynch" "Julia Roberts"
#!/usr/bin/env ruby
# usage:
# $ ruby texting.rb ["<Name>", ...]
#
# There should be a folder called SMSBackups with XML files created by SMS Backup Tool for Android.
#
# example:
# $ ruby texting.rb "Sam Smith" "Bobby Lynch" "Julia Roberts"
# now it maps top 5 thoughts
require 'nokogiri'
require 'ridic'
return if ARGV.size < 1
users = ARGV
months = {}
Dir['SMSBackups/*'].each do |dir|
f = File.open(dir)
doc = Nokogiri::XML(f)
f.close
k = doc.css("sms")
k.each do |message|
str_date = message.attributes["date"].value
str_user = message.attributes["contact_name"].value
type = message.attributes["type"].value.to_i
text = message.attributes["body"].value
# type 1 is received
# type 2 is sent
next unless users.include? str_user and type == 1
date = Time.at(str_date[0..-4].to_i).strftime('%m/%d')
months[date] ||= {}
months[date][str_user] ||= {}
months[date][str_user]["messages"] = {}
next if months[date][str_user]["messages"][str_date] == 1
tertiary = RiDic.category_distribution text
tertiary.each_pair do |cat, count|
months[date][str_user][cat] ||= 0
months[date][str_user][cat] += count
end
months[date][str_user]["messages"][str_date] = 1
end
end
f = File.new('report.txt', 'w+')
f.print "Date"
users.each do |user|
f.print "\t", user
end
f.print "\n"
months.each_pair do |date, texted_users|
f.print date
users.each do |user|
if texted_users.include? user
texted_users[user].delete "messages"
f.print "\t", texted_users[user].sort_by {|cat, num| num}.reverse.take(5).inspect
else
f.print "\t", 0
end
end
f.print "\n"
end
f.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment