lonely_planet_tours 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +16 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +21 -0
- data/README.md +45 -0
- data/Rakefile +8 -0
- data/bin/lonely_planet_tours +19 -0
- data/lib/.DS_Store +0 -0
- data/lib/lonely_planet/lonelyplanet_scrap.rb +50 -0
- data/lib/lonely_planet/version.rb +5 -0
- data/lib/lonely_planet_tours.rb +2 -0
- data/lib/lonelyplanet_scrap_test.rb +52 -0
- data/lonely_planet_tours.gemspec +23 -0
- data/spec/fixtures/vcr_cassettes/taiwan_tours.yml +2576 -0
- data/spec/lonelyplanet_spec.rb +38 -0
- data/spec/support/vcr_setup.rb +4 -0
- data/spec/tours.yml +116 -0
- metadata +143 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 677cc5a7fdc5727128a5ff48e646da82ca1093ac
|
4
|
+
data.tar.gz: dc1b8c204fd73ebe75535688acfe4c6e67676e14
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b20b00e93b82abf3b256adc8db5c56a51fd0210693b57b7e1f74a3a8f5269a697beb62d0cefed6b02654c18c65fff3d20d4bf8d0a71671705798c20d78584802
|
7
|
+
data.tar.gz: f9da365db0e0be8e142eb8cfcf8661b2f88881a40d43fc92eeedaa3e07140c3e0f11ffb578d5031f348f02d3c2d5efa860645a5b2d913571e6e81814e5b7c45b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- ruby-head
|
4
|
+
- "2.2.3"
|
5
|
+
- "2.1.0"
|
6
|
+
- "1.9.3"
|
7
|
+
- jruby-head
|
8
|
+
- jruby-19mode
|
9
|
+
matrix:
|
10
|
+
allow_failures:
|
11
|
+
- rvm: jruby-head
|
12
|
+
branches:
|
13
|
+
only:
|
14
|
+
- master
|
15
|
+
# uncomment this line if your project needs to run something other than `rake`:
|
16
|
+
# script: bundle exec rspec spec
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2015 ZhongMeiZhou https://github.com/ZhongMeiZhou
|
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,45 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
# lonely_planet_tours [](https://badge.fury.io/rb/taiwan_tours) [](https://travis-ci.org/ZhongMeiZhou/scraper_project)
|
4
|
+
|
5
|
+
The web scraper service we built provides listings of tour packages from Lonelyplanet based on a specified country. We have bundled this service into the lonely_planet_tours gem for you to use.
|
6
|
+
|
7
|
+
Note that we respect Lonelyplanet's 'robots.txt' file.
|
8
|
+
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
Install at the command line:
|
13
|
+
|
14
|
+
```sh
|
15
|
+
$ gem install lonely_planet_tours
|
16
|
+
```
|
17
|
+
|
18
|
+
or include it in your 'Gemfile' as:
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem lonely_planet_tours
|
22
|
+
```
|
23
|
+
|
24
|
+
## Try it yourself!
|
25
|
+
Run it from the command line as:
|
26
|
+
|
27
|
+
```sh
|
28
|
+
$ lonely_planet_tours
|
29
|
+
```
|
30
|
+
|
31
|
+
or include it in your code:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'lonely_planet_tours'
|
35
|
+
taiwan = LonelyPlanetScrape::LonelyPlanetTours.new(country)
|
36
|
+
tours = taiwan.tours
|
37
|
+
```
|
38
|
+
|
39
|
+
## Want to make improvements?
|
40
|
+
|
41
|
+
1. Fork it ( https://github.com/ZhongMeiZhou/scraper_project )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'json'
|
3
|
+
require '../lib/lonely_planet/lonelyplanet_scrap.rb'
|
4
|
+
|
5
|
+
begin
|
6
|
+
fail ArgumentError, "usage: tours [country]\n" if ARGV.count == 0
|
7
|
+
country = ARGV[0]
|
8
|
+
|
9
|
+
taiwan_tours = LonelyPlanetScrape::LonelyPlanetTours.new(country)
|
10
|
+
tour_arr = JSON.parse(taiwan_tours.tours)
|
11
|
+
puts "According to LonelyPlanet, these are the best tour packages in #{country}:"
|
12
|
+
|
13
|
+
tour_arr.each do |hash|
|
14
|
+
puts "- #{hash['title']} for $#{hash['price']}"
|
15
|
+
end
|
16
|
+
|
17
|
+
rescue => e
|
18
|
+
puts "Error occured - see details: #{e}"
|
19
|
+
end
|
data/lib/.DS_Store
ADDED
Binary file
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'oga'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
# Module defines LonelyPlanetTours class which handles scraping of lonelyplanet Taiwan tours page
|
6
|
+
module LonelyPlanetScrape
|
7
|
+
class LonelyPlanetTours
|
8
|
+
LONELYPLANET_URL = 'http://www.lonelyplanet.com'
|
9
|
+
TOUR_RELATIVE_DIR = 'tours'
|
10
|
+
|
11
|
+
TOUR_XPATH_CARD = "//article[contains(@class,'card')]"
|
12
|
+
CARD_IMGLINK_XPATH = ".//img[contains(@class,'card__figure__img')]/@src"
|
13
|
+
CARD_TITLE_XPATH = './/h1'
|
14
|
+
CARD_CONTENT_XPATH = ".//div[contains(@class,'card__content__desc')]//p"
|
15
|
+
CARD_LINK_XPATH = ".//div[contains(@class,'card__mask')]//a"
|
16
|
+
CARD_LOCATION_XPATH = ".//div[contains(@class,'card__footer__locale')]"
|
17
|
+
CARD_PRICE_CURRENCY_XPATH = ".//span[contains(@class,'js-currency')]"
|
18
|
+
CARD_PRICE_AMOUNT_XPATH = ".//span[contains(@class,'js-price')]"
|
19
|
+
|
20
|
+
def initialize(country)
|
21
|
+
parse_html(country)
|
22
|
+
end
|
23
|
+
|
24
|
+
def tours
|
25
|
+
@tours ||= extract_tours
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse_html(country)
|
31
|
+
url = "#{LONELYPLANET_URL}/#{country}/#{TOUR_RELATIVE_DIR}"
|
32
|
+
@document = Oga.parse_html(open(url))
|
33
|
+
end
|
34
|
+
|
35
|
+
def extract_tours
|
36
|
+
result = []
|
37
|
+
@document.xpath(TOUR_XPATH_CARD).map do |card|
|
38
|
+
element = {}
|
39
|
+
element['img'] = card.xpath(CARD_IMGLINK_XPATH).text
|
40
|
+
element['title'] = card.xpath(CARD_TITLE_XPATH).text.strip
|
41
|
+
element['content'] = card.xpath(CARD_CONTENT_XPATH).text.strip
|
42
|
+
element['location'] = card.xpath(CARD_LOCATION_XPATH).text
|
43
|
+
element['price_currency'] = card.xpath(CARD_PRICE_CURRENCY_XPATH).text
|
44
|
+
element['price'] = card.xpath(CARD_PRICE_AMOUNT_XPATH).text
|
45
|
+
result << element
|
46
|
+
end
|
47
|
+
result.to_json
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'oga'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
require './lib/lonely_planet/lonelyplanet_scrap'
|
5
|
+
# Test class practice for handling errors and checking for HTML structure changes
|
6
|
+
# can be used to test functionality before implementing it in Scraper Class
|
7
|
+
class LonelyPlanetToursTest < LonelyPlanetScrape::LonelyPlanetTours
|
8
|
+
# override super class initialize function
|
9
|
+
def initialize(country)
|
10
|
+
test_parse_html(country)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_tour
|
14
|
+
@tours ||= test_html_extraction
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
# test connection to external site
|
20
|
+
def test_parse_html(country)
|
21
|
+
parse_html(country)
|
22
|
+
rescue OpenURI::HTTPError => e
|
23
|
+
puts "HTTP request error: #{e}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# test scraping service and also test for changes in structure of elements being traversed
|
27
|
+
def test_html_extraction
|
28
|
+
result = []
|
29
|
+
fail 'OOPS, root article may have been changed or removed' if @document.xpath(TOUR_XPATH_CARD).text.empty?
|
30
|
+
fail 'Title h1 tag may have been changed or removed' if @document.xpath(CARD_TITLE_XPATH).text.empty?
|
31
|
+
fail 'Price span tag may have been changed or removed' if @document.xpath(CARD_PRICE_AMOUNT_XPATH).text.empty?
|
32
|
+
fail 'Content div tag may have been changed or removed' if @document.xpath(CARD_CONTENT_XPATH).text.empty?
|
33
|
+
|
34
|
+
@document.xpath(TOUR_XPATH_CARD).map do |card|
|
35
|
+
element = {}
|
36
|
+
element['img'] = card.xpath(CARD_IMGLINK_XPATH).text
|
37
|
+
element['title'] = card.xpath(CARD_TITLE_XPATH).text.strip
|
38
|
+
element['content'] = card.xpath(CARD_CONTENT_XPATH).text.strip
|
39
|
+
element['location'] = card.xpath(CARD_LOCATION_XPATH).text
|
40
|
+
element['price_currency'] = card.xpath(CARD_PRICE_CURRENCY_XPATH).text
|
41
|
+
element['price'] = card.xpath(CARD_PRICE_AMOUNT_XPATH).text
|
42
|
+
result << element
|
43
|
+
end
|
44
|
+
result.to_json
|
45
|
+
rescue StandardError => e
|
46
|
+
puts e.message
|
47
|
+
puts e.backtrace.inspect
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
test_run = LonelyPlanetToursTest.new('belize')
|
52
|
+
puts test_run.test_tour
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'lonely_planet/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'lonely_planet_tours'
|
6
|
+
s.version = LonelyPlanetScrape::VERSION
|
7
|
+
s.date = LonelyPlanetScrape::DATE
|
8
|
+
s.executables << 'lonely_planet_tours'
|
9
|
+
s.summary = 'The lonelyplanet web scraper service traverses details of tour packages based on specified country'
|
10
|
+
s.description = 'Get listings of tour packages from Lonelyplanet'
|
11
|
+
s.authors = ['Bayardo Salgado', 'Cesar Ordoñez','Eduardo Salazar','Nicole Weatherburne']
|
12
|
+
s.email = ['bayardo_salgado@yahoo.com','cesar','esalazar922@gmail.com','nikkiweat@gmail.com']
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files spec/*`.split("\n")
|
15
|
+
s.homepage = 'https://github.com/ZhongMeiZhou/scraper_project'
|
16
|
+
s.license = 'MIT'
|
17
|
+
|
18
|
+
s.add_development_dependency 'minitest'
|
19
|
+
s.add_development_dependency 'vcr'
|
20
|
+
s.add_development_dependency 'webmock'
|
21
|
+
s.add_runtime_dependency 'oga'
|
22
|
+
s.add_runtime_dependency 'json'
|
23
|
+
end
|