pla 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3629b76b8d19bb0c152b3f9b9d3989fd00704137
4
+ data.tar.gz: 06f52a128ea81c4081ede4de27cfaea19ea78d0a
5
+ SHA512:
6
+ metadata.gz: 5ca799e12bd9bc71863da627edd45b81add14739f925aff6b6fc40da8dd2bf0f6eb37a0cea8e9fbe0e7933aa38b2014fd3323c7f95fab77ac00836ef5050e788
7
+ data.tar.gz: 32004155e0342b737968776277565b8949920887045955d24d06325c80e26f7765d46078441853ad2f4fb0b076fa34036d7b4aaae8c4ce2f420f59a234cf6e6c
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'nokogiri'
4
+ gem 'iso_country_codes'
5
+ gem 'rake'
6
+ gem 'rspec'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 jspc
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ PLA
2
+ ==
3
+
4
+ The PLA is the Port of London Authority. It handles ships and shipping into and around the Thames and stuff for London.
5
+
6
+ It provides ship movement details at it's [website](https://www.pla.co.uk/Port-Trade/Ship-movements/Ship-movements). Unfortunately the information it produces is just awful. There is no good way to handle the order in which comes (it appears to do some kind of ORDER BY on the vessel name field), for instance. Nor anyway to filter the data.
7
+
8
+ Thus I have written a scraper which will provide hashes of each entry.
9
+
10
+ This may be then used for filtering or searching or ordering as is fit.
11
+
12
+ Install
13
+ --
14
+
15
+ `gem install pla`
16
+
17
+ Usage
18
+ --
19
+
20
+ ```ruby
21
+ require 'pla'
22
+
23
+ arrivals = PLA.arrivals
24
+ departures = PLA.departures
25
+
26
+ puts arrivals
27
+ puts departures
28
+ ```
29
+
30
+ Licence
31
+ --
32
+
33
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pla"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ require 'pla/movements'
2
+
3
+ class PLA
4
+ class Arrivals < Movements
5
+ set_url 'https://www.pla.co.uk/Port-Trade/Ship-movements/Ship-movements?flag=5'
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require 'pla/movements'
2
+
3
+ class PLA
4
+ class Departures < Movements
5
+ set_url 'https://www.pla.co.uk/Port-Trade/Ship-movements/Ship-movements?flag=6'
6
+ end
7
+ end
@@ -0,0 +1,73 @@
1
+ require 'iso_country_codes'
2
+
3
+ require 'pla/ship'
4
+
5
+ class PLA
6
+ class Movement
7
+ def initialize record
8
+ # Take a movement record from records and do stuff with it
9
+ @record = record
10
+ end
11
+
12
+ def to_h
13
+ normalise_timestamp!
14
+ normalise_ship!
15
+ normalise_country!
16
+ normalise_fields!
17
+
18
+ @record
19
+ end
20
+
21
+ def normalise_timestamp!
22
+ # The record format from pla has a 'date' field as dd/mm and a time as 'hhmm'
23
+ # We'd prefer a combined timestamp in a standard format
24
+
25
+ date = @record.delete :date
26
+ time = @record.delete :time
27
+ now = DateTime.now
28
+
29
+ year = now.year
30
+ day,month = date.split('/')
31
+ hour = time[0..1]
32
+ minute = time[2..3]
33
+
34
+ date = gen_date year, month, day, hour, minute
35
+ if now > date
36
+ date = gen_date year+1, month, day, hour, minute
37
+ end
38
+
39
+ @record['timestamp'] = date.to_s
40
+ end
41
+
42
+ def normalise_ship!
43
+ name = @record.delete(:'vessel name')
44
+ @record['vessel'] = PLA::Ship.new(name).to_h
45
+ end
46
+
47
+ def normalise_country!
48
+ country_code = @record.delete(:nationality)
49
+ begin
50
+ country = IsoCountryCodes.find(country_code).name
51
+ rescue IsoCountryCodes::UnknownCodeError
52
+ country = country_code
53
+ end
54
+
55
+ @record[:country] = country
56
+ end
57
+
58
+ def normalise_fields!
59
+ @record.delete(:at)
60
+ end
61
+
62
+ def gen_date year, month, day, hour, minute
63
+ # Because the pla doesn't provide a year we run the risk of providing
64
+ # a date in the past if the data wraps around the end of the year.
65
+ #
66
+ # The pla doesn't, luckily, have data years in advance or this'd be proper
67
+ # fucked
68
+ DateTime.new(year.to_i, month.to_i, day.to_i, hour.to_i, minute.to_i, 0)
69
+ end
70
+
71
+
72
+ end
73
+ end
@@ -0,0 +1,64 @@
1
+ require 'time'
2
+ require 'nokogiri'
3
+ require 'open-uri'
4
+
5
+ require 'pla/movement'
6
+
7
+ class PLA
8
+ class Movements
9
+ class << self
10
+ attr_accessor :url
11
+ def set_url u
12
+ @url = u
13
+ end
14
+ end
15
+
16
+ attr_reader :records, :headers
17
+
18
+ def initialize
19
+ @url = self.class.url
20
+ @root = 'div.newsMainArea table tr td table tr' # It gets worse
21
+
22
+ @records = []
23
+ @ships = get_ships_data
24
+
25
+ set_headers
26
+ set_records
27
+
28
+ @records
29
+ end
30
+
31
+ def get_ships_data
32
+ Nokogiri::HTML(open(@url)).css(@root)
33
+ end
34
+
35
+ def set_headers
36
+ @headers = normalize_and_map(@ships.first.css('th'))
37
+ @headers[-1] = 'Notes' # pla returns this as empty. We infer it means notes, though usually it says 'Pilot required'
38
+ end
39
+
40
+ def set_records
41
+ @ships[1..-1].each do |t|
42
+ record = {}
43
+ fields = normalize_and_map(t.css('td'))
44
+ fields.each_with_index do |f,i|
45
+ record[ @headers[i].to_sym ] = f
46
+ end
47
+
48
+ movement = PLA::Movement.new(record)
49
+ @records << movement.to_h
50
+ end
51
+ end
52
+
53
+ private
54
+ def normalize_and_map elem
55
+ # This is a mess- the pla website wraps a date with non-breaking spaces
56
+ # and some other stuff with normal whitespace. We don't want this because
57
+ # it is a mess. Thus we convert nbsp chars (ord 160) with spaces (ord 32)
58
+ # and then allow strip to tidy the rest up.
59
+
60
+ nbsp = Nokogiri::HTML("&nbsp;").text
61
+ elem.map{|c| c.text.gsub(nbsp, ' ').strip.downcase}
62
+ end
63
+ end
64
+ end
data/lib/pla/ship.rb ADDED
@@ -0,0 +1,14 @@
1
+ class PLA
2
+ class Ship
3
+ def initialize ship
4
+ @name = ship
5
+ end
6
+
7
+ def to_h
8
+ {
9
+ name: @name,
10
+ google: "https://www.google.co.uk/search?q=site:vesselfinder.com #{@name}"
11
+ }
12
+ end
13
+ end
14
+ end
data/lib/pla.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'pla/arrivals'
2
+ require 'pla/departures'
3
+
4
+ class PLA
5
+ def self.arrivals
6
+ PLA::Arrivals.new.records
7
+ end
8
+
9
+ def self.departures
10
+ PLA::Departures.new.records
11
+ end
12
+ end
data/pla.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "pla"
7
+ spec.version = '0.1.0'
8
+ spec.authors = ["jspc"]
9
+ spec.email = ["james@zero-internet.org.uk"]
10
+
11
+ spec.summary = 'Scrape and interact with the Port of London Authority Movements list'
12
+ spec.homepage = 'https://github.com/jspc/pla'
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.11"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ spec.add_development_dependency "rspec", "~> 3.0"
23
+
24
+ spec.add_dependency "iso_country_codes", "0.7.5"
25
+ spec.add_dependency "nokogiri", "~> 1.6"
26
+ end
metadata ADDED
@@ -0,0 +1,130 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pla
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - jspc
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-05-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: iso_country_codes
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.7.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.7.5
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.6'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.6'
83
+ description:
84
+ email:
85
+ - james@zero-internet.org.uk
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - lib/pla.rb
100
+ - lib/pla/arrivals.rb
101
+ - lib/pla/departures.rb
102
+ - lib/pla/movement.rb
103
+ - lib/pla/movements.rb
104
+ - lib/pla/ship.rb
105
+ - pla.gemspec
106
+ homepage: https://github.com/jspc/pla
107
+ licenses:
108
+ - MIT
109
+ metadata: {}
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubyforge_project:
126
+ rubygems_version: 2.5.1
127
+ signing_key:
128
+ specification_version: 4
129
+ summary: Scrape and interact with the Port of London Authority Movements list
130
+ test_files: []