forked from hoodie/catch-my-bus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcatch_my_bus.rb
executable file
·75 lines (61 loc) · 1.45 KB
/
catch_my_bus.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
#!/usr/bin/env ruby
# encoding: utf-8
# This file is distributed under the terms of the
# GNU General Public License 2.
# Please see the COPYING-GPL-2 file for details.
require 'net/http'
require 'json'
require 'notify'
require 'pp'
$SCRIPT_PATH = File.split(File.expand_path(__FILE__))[0]
@big_sleep = 180
@little_sleep = 2
stations = [
"Helmholzstrasse",
"Münchnerplatz",
#"Malterstraße"
]
class TramStation
def initialize name
@name = name
@destinations= {}
end
def show
puts @name
puts print
puts
end
def print
string = ""
@destinations.each{ |n,t| string << "#{n} in #{t}min\n" }
return string
end
def notify
Notify.notify @name, print(), {app:"Catch My Bus", :icon => (File.join$SCRIPT_PATH, "Bushaltestelle.png" )}
end
def parse_arrival(arrival)
arrival[2] = 0 if arrival[2] == ""
arrival[2] = arrival[2].to_i
dest = "#{arrival[0]} #{arrival[1]}"
if @destinations[dest].nil? or @destinations[dest] > arrival[2]
@destinations[dest] = arrival[2]
end
end
def update
@destinations= {}
uri = URI URI::encode "http://widgets.vvo-online.de/abfahrtsmonitor/Abfahrten.do?ort=Dresden&hst=#{@name}&vz=0"
JSON.parse(Net::HTTP.get(uri)).each { |a|
parse_arrival(a)
}
end
end
stations.map!{ |station| TramStation.new station}
while true do
stations.each{ |station|
station.update
station.notify
station.show
sleep @little_sleep
}
sleep @big_sleep
end