-
Notifications
You must be signed in to change notification settings - Fork 0
/
millsey_cycle_spambot.rb
73 lines (56 loc) · 1.8 KB
/
millsey_cycle_spambot.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
require 'csv'
require 'dotenv'
require 'pravigo/duration'
require 'faye/websocket'
require 'eventmachine'
require 'json'
Dotenv.load(File.expand_path("../.env", __FILE__))
SPEED_UP_FACTOR = (ENV['SPEED_UP_FACTOR'].to_i || 20)
puts SPEED_UP_FACTOR
WEBSOCKETS_SERVER_ADDRESS = (ENV['WEBSOCKETS_SERVER_ADDRESS'] || 'wss://pravigo-chat.herokuapp.com/')
FILENAME = (ENV['FILENAME'] || "2015Jul12.csv")
CYCLIST_HANDLE = (ENV['CYCLIST_HANDLE'] || "Millsey")
EM.run {
ws = Faye::WebSocket::Client.new(WEBSOCKETS_SERVER_ADDRESS)
io = File.open(FILENAME)
previous_line = nil
time_to_wait = 5
io.gets
read_chunk = proc do
puts "."
if line = io.gets
if !previous_line.blank?
parsed_line = CSV.parse(line)
current_time = Time.parse(parsed_line[0][0])
previous_time = Time.parse(CSV.parse(previous_line)[0][0])
time_to_wait = Duration.new(current_time-previous_time).to_i
payload = { :handle=>CYCLIST_HANDLE, :text=>"GPS", :GMTTIMESTAMP=>parsed_line[0][0], :LATITUDE=>parsed_line[0][2], :LONGITUDE=>parsed_line[0][1], :ALTITUDE=>parsed_line[0][3], :ACCURACY=>parsed_line[0][4] }
message = payload
EM.add_timer(time_to_wait/SPEED_UP_FACTOR) do
ws.send(message.to_json)
puts "I waited #{time_to_wait} seconds"
previous_line = line
EM.next_tick(read_chunk)
end
else
previous_line = line
EM.next_tick(read_chunk)
end
else
ws.close
#EM.stop
end
end
ws.on :open do |event|
p [:open]
EM.next_tick(read_chunk)
end
ws.on :message do |event|
p [:message, event.data]
end
ws.on :close do |event|
p [:close, event.code, event.reason]
ws = nil
EM.stop
end
}