-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_xml
executable file
·89 lines (78 loc) · 1.73 KB
/
generate_xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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}  #{translate(text)}</tweet>"
end
puts "</day>"
puts "</month>"
puts "</tweets>"