My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
#!/usr/bin/env ruby

#puts 'starting'

require 'rubygems'

require 'banned-words'
require 'secret'

require 'hpricot'
require 'json'
require 'net/http'
require 'time'
require 'xmpp4r-simple'

class Updater

def initialize
@MAX_UPDATES = 50
@lastwrite = Time.now
@users = {}
@updates = {}
@updatelist = []
@im = Jabber::Simple.new( Secret::USERNAME, Secret::PASSWORD )
@im.deliver( 'twitter@twitter.com', 'on' )
end

def run
while true
begin
receive
sleep 10
rescue
p "Exception raised!"
end
end
end

#def readupdates
# File.open( 'tweets/tweets.js', 'r' ) do |f|
# data = f.read
# oldUpdates = JSON.parse( data )
# oldUpdates.each { |update|
# @updates[ update['message'] ] = update
# @updatelist.push( update )
# }
# end
#end

def writeupdates
File.open( 'tweets/tweets.js', 'w' ) do |f|
f.puts @updatelist.to_json
end
p 'Checking in updates'
`svn ci -m "Twitter update" tweets/tweets.js`
p 'Done checking in'
end

#readupdates

def onemsg( msg )
return if msg.type != :chat or msg.from != 'twitter@twitter.com' or @updates[msg.body]
body = msg.body
match = /^\s*\((.*)\):(.*)$/.match(body)
return if not match
username = match[1]
message = match[2]
if Banned.banned(body)
p "Blocked: #{message}"
return
end
doc = Hpricot::XML(msg.to_s)
author = (doc/:author/:name).text
user = getuser( username, author )
return if not user
update = {
'body' => msg.body,
'message' => message,
'time' => Time.xmlschema( (doc/:published).text ).to_i
}.merge( user )
if Banned.banned( update['where'] )
p "Blocked location: #{update['where']}"
return
end
p "Posting: #{message}"
@updates[msg.body] = update
@updatelist.push( update )
@updatelist.delete_at(0) if @updatelist.length > @MAX_UPDATES
if Time.now - @lastwrite > 300 and @updatelist.length >= 20
@lastwrite = Time.now
writeupdates
end
end

def getuser( username, author )
if not @users[username]
http = Net::HTTP.new( 'api.twittervision.com' )
headers, body = http.get( "/user/current_status/#{username}.xml" )
if headers.code == '200'
tv = Hpricot::XML(body)
loc = (tv/:location)
lat = (loc/:latitude).text
lon = (loc/:longitude).text
if lat != '' and lon != ''
@users[username] = {
'user' => username,
'author' => author,
'name' => (tv/:name).text,
'image' => (tv/'profile-image-url').text,
'lat' => (loc/:latitude).text,
'lon' => (loc/:longitude).text,
'where' => (tv/'current-location').text,
'status' => 0
}
end
end
end
@users[username]
end

def receive
@im.received_messages do |msg|
onemsg msg
end
end

end

updater = Updater.new
updater.run

Change log

r1766 by gearylabs on Feb 27, 2008   Diff
Refactor
Go to: 
Project members, sign in to write a code review

Older revisions

r1669 by gearylabs on Feb 14, 2008   Diff
Refactor
r1647 by gearylabs on Feb 9, 2008   Diff
Add posting log message
r1646 by gearylabs on Feb 9, 2008   Diff
Add "require 'rubygems'"
All revisions of this file

File info

Size: 2762 bytes, 130 lines
Powered by Google Project Hosting