Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 89 lines (78 sloc) 1.73 KB
#!/usr/bin/env ruby
require 'date'
require 'rubygems'
require 'activesupport'
require 'htmlentities'
# are there maybe dependency problems in the tweetparser gem?
# Why do I have to do this?
gem 'polyglot','=0.2.9'
gem 'treetop','=1.4.2'
gem 'tweetparser'
require 'tweetparser'
class String
def to_xml
unpack('U*').map {|n|
case n
when 38
"&"
when 60
"<"
when 62
">"
when 1..127
n.chr
else
"&##{n};"
end
}.join
end
def from_html
coder = HTMLEntities.new
coder.decode(self)
end
end
def translate(text)
parsed = TweetParser.parse(text)
parsed.map { |sexp|
type, token = *sexp
token = token.from_html.to_xml
case type
when :url
"<link>"+token+"</link>"
when :username
"<name>"+token+"</name>"
when :hashtag
"<hashtag>"+token+"</hashtag>"
else
token
end
}.join("")
end
cur_month = ""
cur_day = ""
puts "<tweets>"
while (tweet = gets)
id, date,text = tweet.split("\t")
date = DateTime.strptime(date,"%Y-%m-%dT%H:%M:%S%z")
month = date.strftime("%B %Y")
day = date.strftime("%A #{date.day.ordinalize} %B")
time = date.strftime("%I:%M%p").downcase
new_month = (cur_month != month)
new_day = (cur_day != day)
if new_day
puts "</day>" unless cur_day == ""
end
if new_month
puts "</month>" unless cur_month == ""
puts "<month name=\"#{month}\">"
cur_month = month
end
if new_day
puts "<day name=\"#{day}\">"
cur_day = day
end
puts "<tweet>#{time}&#160;&#160;#{translate(text)}</tweet>"
end
puts "</day>"
puts "</month>"
puts "</tweets>"