-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmvg
executable file
·170 lines (132 loc) · 3.81 KB
/
mvg
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
164
165
166
167
168
169
170
#!/usr/bin/env ruby
# encoding: UTF-8
require 'optparse'
require 'ostruct'
require 'date'
require "rubygems"
require "open-uri"
class MvgLive
VERSION = '0.0.1'
# The URL where the servce is available
URL = "http://www.mvg-live.de/ims/dfiStaticAnzeige.svc"
# These are the difernt means of transportatios provided by mvg
TRANSPORTATIONS = %w[ubahn bus sbahn tram]
attr_reader :options
attr_reader :url_parameters
def initialize(arguments, stdin)
@arguments = arguments
@stdin = stdin
# Set defaults
@options = OpenStruct.new
@url_parameters = {}
TRANSPORTATIONS.each do |transport|
@url_parameters[transport] = "checked"
end
end
def run
if parsed_options? && arguments_valid?
output_options if @options.verbose
process_arguments
process_command
else
output_usage
end
end
protected
def parsed_options?
@opts = OptionParser.new do |opts|
opts.banner = "Usage: mvg station [options]"
opts.separator ""
opts.separator "Transportation options:"
TRANSPORTATIONS.each do |t|
opts.on("-#{t[0,1].downcase}", "--no-#{t}", "Dont display #{t}") do
@url_parameters[t] = ""
end
end
opts.separator ""
opts.separator "Options:"
opts.on('-p', '--http-proxy [proxy]') { |proxy| @options.proxy = proxy }
opts.on('-V', '--verbose') { @options.verbose = true }
opts.on('-v', '--version') { output_version ; exit 0 }
opts.on('-h', '--help') { output_help }
end
@opts.parse!(@arguments) rescue return false
process_options
true
end
def process_options
end
def output_options
puts "Options:\n"
@options.marshal_dump.each do |name, val|
puts " #{name} = #{val}"
end
end
def arguments_valid?
true if @arguments.length == 1
end
def process_arguments
@url_parameters[:haltestelle] = @arguments.first
end
def output_help
puts "Description"
puts " The mvg tool is cli tool consuming the http://www.mvg-live.de service"
puts " to make it accessable from command line."
puts ""
puts "Examples"
puts " mvg Marienplatz"
puts " mvg Marienplatz --no-bus"
puts " mvg Hauptbahnhof -btu"
puts ""
output_usage
puts ""
output_author
puts ""
output_version
exit 0
end
def output_usage
puts @opts
end
def output_copyright
puts "Copyright"
puts " Copyright (c) 2007 Konstantin Ikaros Kanellopoulos. Licensed under the MIT License:"
puts " http://www.opensource.org/licenses/mit-license.php"
end
def output_author
puts "Author"
puts " Konstantin Ikaros Kanellopoulos"
end
def output_version
puts "Version"
puts " #{File.basename(__FILE__)} version #{VERSION}"
end
def encode_url_value(value)
value.gsub(/ß/, "%DF")
end
def process_command
params = []
@url_parameters.each do |param,value|
value = encode_url_value(value)
params << "#{param}=#{value}"
end
params = params.join("&")
url = "#{URL}?#{params}"
puts url if @options.verbose
# Options for the connection to the service
http_options = {}
http_options[:proxy] = @options.proxy if @options.proxy
# get html data
doc = open(url, http_options).read
doc.tr!("\n\r\t",'')
data = doc.scan(/<tr class="row.*?">.*?class="lineColumn">(.*?)<.*?class="stationColumn">(.*?)<.*?class="inMinColumn">(.*?)<.*?<\/tr>/)
data.each do|r|
puts "%-4s | %-30s | %-3s\n" % r
end
end
def process_standard_input
input = @stdin.read
end
end
app = MvgLive.new(ARGV, STDIN)
app.run