timetabler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,69 @@
1
+ class Timetabler
2
+ def subinitialize
3
+ # Used for temporary route storage since we would have to load the same page twice to get the times and routes
4
+ @route_nodes = []
5
+ end
6
+
7
+ def fetch_all
8
+ {:lines => fetch_lines, :routes => fetch_routes, :stops => fetch_stops}
9
+ end
10
+
11
+ def fetch_lines
12
+ line_list = Nokogiri::HTML(open('http://www.mzk.pl/rozklady/?co=lista_linii'))
13
+
14
+ lines = line_list.css('ul.linie_typu a').select{|l| l.content.length <= 5}
15
+
16
+ @lines = lines.map{ |link|
17
+ l = Timetabler::Line.new
18
+ l[:name] = link.content.gsub(/\*/, "")
19
+ l[:href] = link[:href]
20
+
21
+ l
22
+ }
23
+ end
24
+
25
+ def fetch_routes
26
+ if @lines.nil?
27
+ fetch_lines
28
+ end
29
+
30
+ @lines.each_index do |id|
31
+ link = @lines[id]
32
+
33
+ line = Nokogiri::HTML(open('http://www.mzk.pl/rozklady/'+link[:href]))
34
+ line.css("div#trasy div.trasa").each do |route|
35
+ r = Timetabler::Route.new
36
+ r[:line_id] = id;
37
+ r[:name] = route.css("h4 span").first.content
38
+
39
+ @routes << r
40
+ @route_nodes << route
41
+ end
42
+ end
43
+
44
+ return @routes
45
+ end
46
+
47
+ def fetch_stops
48
+
49
+ if @routes.nil? or @route_nodes.nil?
50
+ fetch_routes
51
+ end
52
+
53
+ @route_nodes.each_index do |id|
54
+ route = @route_nodes[id]
55
+
56
+ route.css("ul li a").map{|stop|
57
+ s = Timetabler::Stop.new
58
+ s[:line_id] = @routes[id][:line_id]
59
+ s[:route_id] = id
60
+ s[:name] = stop.content.strip
61
+ s[:href] = stop[:href]
62
+
63
+ @stops << s
64
+ }
65
+ end
66
+
67
+ return @stops
68
+ end
69
+ end
data/lib/timetabler.rb ADDED
@@ -0,0 +1,58 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ class Timetabler
5
+ def initialize(parser=nil)
6
+ @lines = []
7
+ @routes = []
8
+ @stops = []
9
+ @times = []
10
+
11
+ @parser = parser
12
+
13
+ if !@parser.nil?
14
+ use_parser @parser
15
+ end
16
+ end
17
+
18
+ def parsers
19
+ Dir.entries(File.join(File.dirname(__FILE__), "parsers")).select{|f| !["..", "."].include?(f)}
20
+ end
21
+
22
+ def fetch(parser=nil)
23
+ if @parser.nil? and parser.nil?
24
+ raise "Parser not specified"
25
+ elsif @parser.nil? and !parser.nil?
26
+ use_parser parser
27
+ end
28
+
29
+ fetch_all
30
+ end
31
+
32
+ def use_parser(parser)
33
+ if parsers.include? parser
34
+ require File.join(File.dirname(__FILE__), "parsers", parser, "parser")
35
+ subinitialize
36
+ else
37
+ raise "Unknown parser: " + parser.to_s
38
+ end
39
+
40
+ return self
41
+ end
42
+ end
43
+
44
+ class Timetabler::Line < Hash
45
+
46
+ end
47
+
48
+ class Timetabler::Route < Hash
49
+
50
+ end
51
+
52
+ class Timetabler::Stop < Hash
53
+
54
+ end
55
+
56
+ class Timetabler::Time < Hash
57
+
58
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: timetabler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Kamil Kowalski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-06-01 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.5.2
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 1.5.2
30
+ description: Timetabler is a Polish public transportation timetable parser that allows
31
+ you to extract bus schedules directly from websites in a code-friendly format.
32
+ email: kowalski.tychy@gmail.com
33
+ executables: []
34
+ extensions: []
35
+ extra_rdoc_files: []
36
+ files:
37
+ - lib/timetabler.rb
38
+ - lib/parsers/mzk-tychy/parser.rb
39
+ homepage: https://github.com/kamilkowalski/timetable-parsers
40
+ licenses:
41
+ - MIT
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: 1.9.2
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ requirements: []
59
+ rubyforge_project:
60
+ rubygems_version: 1.8.18
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: A public transportation timetable parser
64
+ test_files: []