-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbulltwitter.rb
61 lines (53 loc) · 1.75 KB
/
bulltwitter.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
#!/usr/bin/env ruby
=begin
BullTwitter will listen for the map page's creation of a websocket. Once it's
found it, it will then start listening for events from the bulltwitter map and
call the tweet method when a message is received.
=end
require 'em-websocket'
require 'yaml'
require 'json'
require 'oauth'
require 'twitter'
cfgfile = 'auth.cfg'
cnf = YAML::load(File.open(cfgfile))
#set up Twitter client
$client = Twitter::REST::Client.new do |config|
config.consumer_key = cnf['crcbot']['con_key']
config.consumer_secret = cnf['crcbot']['con_sec']
config.access_token = cnf['crcbot']['o_tok']
config.access_token_secret = cnf['crcbot']['o_tok_sec']
end
def bulltweet(lat,lng,status)
$client.update status, {lat: lat, long: lng}
end
def bulltweet_with_image(lat,lng,status,imgfile)
media_id = $client.upload File.new imgfile
$client.update status, {lat: lat, long: lng, media_ids: media_id}
end
EM.run {
EM::WebSocket.run(:host => "127.0.0.1", :port => 8567) do |ws|
ws.onopen do
puts "WebSocket opened"
end
ws.onclose do
ws.close(code = nil, body = nil)
puts "WebSocket closed"
exit
end
ws.onmessage do |msg|
# check for coords in parens + string
if msg =~ /^(?!IMG)\(-?\d{1,3}\.\d+, -?\d{1,3}\.\d+\)/
lat,lng,status = msg.match(/\((-?\d{1,3}\.\d+), (-?\d{1,3}\.\d+)\)(.+)$/).captures
bulltweet(lat,lng,status)
elsif msg =~ /IMG/
puts "got #{msg}\n"
imgfile,lat,lng,status = msg.match(/IMG(.+)\((-?\d{1,3}\.\d+), (-?\d{1,3}\.\d+)\)(.+)$/).captures
puts "sending #{imgfile} and status #{status}\n"
bulltweet_with_image(lat,lng,status,imgfile)
else
puts "Got unintelligible command, please resend."
end
end
end
}