check_late 0.0.1 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/check_late +12 -1
- data/lib/check_late/notifier.rb +19 -3
- data/lib/check_late/scraper.rb +54 -0
- data/lib/check_late/version.rb +3 -0
- data/lib/check_late.rb +25 -5
- metadata +3 -1
data/bin/check_late
CHANGED
@@ -1,4 +1,15 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
|
3
3
|
require 'check_late'
|
4
|
-
|
4
|
+
require 'optparse'
|
5
|
+
|
6
|
+
options = {}
|
7
|
+
OptionParser.new do |opts|
|
8
|
+
opts.banner = "Usage: check_late -t train_number"
|
9
|
+
|
10
|
+
opts.on("-t num", "--train num", "Insert train number") do |num|
|
11
|
+
options[:train] = num
|
12
|
+
end
|
13
|
+
end.parse!
|
14
|
+
|
15
|
+
CheckLate.new(options[:train]).start!
|
data/lib/check_late/notifier.rb
CHANGED
@@ -1,5 +1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'rubygems'
|
2
|
+
require 'synack'
|
3
|
+
|
4
|
+
|
5
|
+
class Notifier
|
6
|
+
|
7
|
+
def notify!(message)
|
8
|
+
if message and not message.empty?
|
9
|
+
puts message
|
10
|
+
end
|
4
11
|
end
|
12
|
+
|
13
|
+
def synack!(message)
|
14
|
+
if message and not message.empty?
|
15
|
+
Synack::Client.new(:host => 'localhost', :port => '1234').say(message)
|
16
|
+
#`synack -p 1234 #{message}`
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
|
5
21
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'mechanize'
|
3
|
+
|
4
|
+
module Scraper
|
5
|
+
URL = "http://mobile.viaggiatreno.it/viaggiatreno/mobile/numero/"
|
6
|
+
SEARCH_TYPE = "numero"
|
7
|
+
LANG = "IT"
|
8
|
+
|
9
|
+
def generate_request(train_number)
|
10
|
+
agent = Mechanize.new
|
11
|
+
response = agent.post( URL,
|
12
|
+
{
|
13
|
+
:numeroTreno => train_number,
|
14
|
+
:tipoRicerca => SEARCH_TYPE,
|
15
|
+
:lang => LANG
|
16
|
+
}
|
17
|
+
)
|
18
|
+
return response.parser.xpath('//div[@class="evidenziato"]/strong')
|
19
|
+
end
|
20
|
+
|
21
|
+
def scrape!(train_number)
|
22
|
+
xpath = generate_request(train_number)
|
23
|
+
begin
|
24
|
+
|
25
|
+
if xpath.to_a.first.nil?
|
26
|
+
raise Exception
|
27
|
+
else
|
28
|
+
sanitize_text xpath.to_a.first.to_html
|
29
|
+
end
|
30
|
+
|
31
|
+
rescue Exception
|
32
|
+
puts "Invalid Train Number"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def sanitize_text(text)
|
38
|
+
text = text.gsub(/\r|\n/, '')
|
39
|
+
text = text.gsub(/\t+/, ' ')
|
40
|
+
text = text.gsub(/ +/, ' ')
|
41
|
+
text = text.gsub(/<!--.*-->/, '')
|
42
|
+
text = text.gsub(/<br>/, '')
|
43
|
+
text = text.gsub(/<strong>|<\/strong>/, '')
|
44
|
+
text = text.strip
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
|
50
|
+
# # Some useless variables to store data
|
51
|
+
# delay = text.match(/(\d+) minuti di ritardo/)
|
52
|
+
# early = text.match(/(\d+) minuti di anticipo/)
|
53
|
+
# arrived = text.match(/arrivato/)
|
54
|
+
# not_departed = text.match(/non e' ancora partito/)
|
data/lib/check_late.rb
CHANGED
@@ -1,9 +1,29 @@
|
|
1
|
+
require 'check_late/notifier'
|
2
|
+
require 'check_late/scraper'
|
3
|
+
|
1
4
|
class CheckLate
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
+
include Scraper
|
6
|
+
attr_accessor :train_number, :status, :old_status
|
7
|
+
|
8
|
+
def initialize(train_number)
|
9
|
+
@train_number = train_number
|
10
|
+
@notifier = Notifier.new
|
5
11
|
end
|
6
|
-
end
|
7
12
|
|
13
|
+
def status
|
14
|
+
new = scrape!(train_number)
|
15
|
+
if @status != new
|
16
|
+
@status = new
|
17
|
+
return new
|
18
|
+
end
|
19
|
+
end
|
8
20
|
|
9
|
-
|
21
|
+
def start!
|
22
|
+
while true
|
23
|
+
#@notifier.notify!(status)
|
24
|
+
@notifier.synack!(status)
|
25
|
+
sleep(60)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: check_late
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,6 +19,8 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- lib/check_late/notifier.rb
|
22
|
+
- lib/check_late/scraper.rb
|
23
|
+
- lib/check_late/version.rb
|
22
24
|
- lib/check_late.rb
|
23
25
|
- bin/check_late
|
24
26
|
- README.md
|