forked from icco/Agent355
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.rb
164 lines (133 loc) · 4.21 KB
/
utils.rb
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
require 'sqlite3'
require 'time'
# this class stores all kinds of helper stuff.
class Utils
def Utils.db
return @@db
end
def Utils.getSeen user
return (self.getKeyPair user, 'seens').to_i
end
def Utils.markSeen user
return self.storeKeyPair user, Time.now.to_i, 'seens'
end
def Utils.getDefine key
return self.getKeyPair key, 'defines'
end
def Utils.storeDefine key, value
return self.storeKeyPair key, value, 'defines'
end
def Utils.storeKeyPair key, value, table
sql = <<-SQL
INSERT OR REPLACE INTO #{table} (key, value) VALUES (:k, :v);
SQL
out = Utils.db.execute(sql, :k => key, :v => value)
return out
end
def Utils.getKeyPair key, table
sql = <<-SQL
SELECT value
FROM #{table}
WHERE key = ?;
SQL
return Utils.db.get_first_value(sql, key)
end
# nice little howto for sqlite in ruby
# http://viewsourcecode.org/why/hacking/aQuickGuideToSQLite.html
# http://www.sqlite.org/lang.html
def Utils.buildDB filename
@@db = SQLite3::Database.new(filename)
['defines', 'seens', 'bans'].each {|tbl|
Utils.db.execute <<-SQL
CREATE TABLE IF NOT EXISTS #{tbl} (
key VARCHAR(255) PRIMARY KEY,
value TEXT
);
SQL
}
end
def Utils.mature_words
return [
'beaner',
'chink',
'chnk',
'fag',
'gook',
'naggar',
'nagger',
'niga',
'nigar',
'nigga',
'niggar',
'nigger',
'niggr',
]
end
# this is the function that builds our mature language regex.
def Utils.mature_regex words
leet = {
"a" => "4@",
"b" => "68",
"c" => "(",
"e" => "3",
"g" => "6",
"i" => "1!",
"i" => "17",
"o" => "0",
"p" => "9",
"s" => "5$",
"t" => "7+",
}
words.map! {|w|
'(^|\W)' + w.split('').map {|c|
c.gsub!(Regexp.compile("[#{leet.keys.to_s}]")) {|m|
"[#{Regexp.escape(leet[m]) + m}]" }
c + '+[^A-Za-z]*'
}.join
}
return Regexp.new("(#{words.join('|')})", Regexp::EXTENDED|Regexp::IGNORECASE)
end
def Utils.rss feed
require 'rss'
rss = RSS::Parser.parse(open(feed).read(), false).items[0]
ts = Utils.time_since rss.pubDate
return "\"#{rss.title}\" #{ts} -- #{rss.link}"
end
def Utils.twitter username
params = "?screen_name=#{username}&count=1&trim_user=true"
json = "http://api.twitter.com/1/statuses/user_timeline.json#{params}"
url = URI.parse(json)
resp = Net::HTTP.get_response(url)
data = resp.body
result = JSON.parse(data)
ts = Utils.time_since Time.parse(result[0]['created_at'])
return "\"#{result[0]['text']}\" #{ts} -- http://twitter.com/#{username}"
end
# pass in a time object
def Utils.time_since time
distance = Time.now - time
out = case distance
when 0 .. 59 then "#{distance} seconds ago"
when 60 .. (60*60) then "#{distance/60} minutes ago"
when (60*60) .. (60*60*24) then "#{distance/(60*60)} hours ago"
when (60*60*24) .. (60*60*24*30) then "#{distance/((60*60)*24)} days ago"
else time.strftime("%m/%d/%Y")
end
return out.sub(/^1 (\w+)s ago$/, '1 \1 ago')
end
def Utils.lastplayed username
api = "c8a55898b287950c836a1af12d91ce7d"
base_url = "http://ws.audioscrobbler.com/2.0/?method=user.getrecenttracks"
url = "#{base_url}&user=#{username}&api_key=#{api}&limit=1&format=json"
resp = Net::HTTP.get_response(URI.parse(url))
result = JSON.parse(resp.body)
if !result.nil? && !result['recenttracks'].nil? && !result['recenttracks']['track'].nil?
track = result['recenttracks']['track']
title = track[0].nil? ? track['name'] : track[0]['name']
artist = track[0].nil? ? track['artist']['#text'] : track[0]['artist']['#text']
return "#{username} is listening to \"#{title}\" by #{artist}."
else
return "#{username} is not a valid last.fm user."
end
end
end