Class: HotelPrice::Expedia::ExpediaScraper
- Inherits:
-
Object
- Object
- HotelPrice::Expedia::ExpediaScraper
- Defined in:
- lib/hotel_price/expedia/expedia_scraper.rb
Instance Method Summary collapse
- #get_price(expedia_hotel_id, checkin_date, num_adults) ⇒ Object
-
#initialize(mode = :chrome) ⇒ ExpediaScraper
constructor
A new instance of ExpediaScraper.
- #make_date_args(checkin_date) ⇒ Object
- #make_num_adults_arg(num_adults) ⇒ Object
- #make_query_string(checkin_date, num_adults) ⇒ Object
Constructor Details
#initialize(mode = :chrome) ⇒ ExpediaScraper
7 8 9 |
# File 'lib/hotel_price/expedia/expedia_scraper.rb', line 7 def initialize(mode = :chrome) @mode = mode end |
Instance Method Details
#get_price(expedia_hotel_id, checkin_date, num_adults) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/hotel_price/expedia/expedia_scraper.rb', line 11 def get_price(expedia_hotel_id, checkin_date, num_adults) date = DateTime.now.strftime("%Y-%m-%d") query_string = make_query_string(checkin_date.to_s, num_adults) url = "https://www.expedia.co.jp/ja/#{expedia_hotel_id}.Hotel-Information?#{query_string}" driver = HotelPrice.get_selenium_driver @mode driver.get(url) sleep 2 data = driver.find_elements(:xpath, "//span[@data-stid='content-hotel-lead-price']") return { date: date, min_price: 0 } if data.empty? price = data.first.text.delete("^0-9").to_i hotel_name = "" room_name = "" hotel_name_element = driver.find_elements(:xpath, "//h1[@data-stid='content-hotel-title']") hotel_name = hotel_name_element.first.text unless hotel_name_element.empty? room_name_element = driver.find_elements(:xpath, "//h3[@data-stid='room-info-title-heading']") room_name = room_name_element.first.text unless room_name_element.empty? { checkin_date: checkin_date, min_price: price, hotel_name: hotel_name, room_name: room_name } end |
#make_date_args(checkin_date) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/hotel_price/expedia/expedia_scraper.rb', line 41 def make_date_args checkin_date Date.parse checkin_date rescue return "" t = Date.parse(checkin_date) checkin_arg = t.strftime("chkin=%Y-%m-%d") checkout_arg = (t + 1).strftime("chkout=%Y-%m-%d") "#{checkin_arg}&#{checkout_arg}" end |
#make_num_adults_arg(num_adults) ⇒ Object
49 50 51 52 |
# File 'lib/hotel_price/expedia/expedia_scraper.rb', line 49 def make_num_adults_arg num_adults return "" if num_adults.to_i <= 1 "rm1=a#{num_adults}" end |
#make_query_string(checkin_date, num_adults) ⇒ Object
35 36 37 38 39 |
# File 'lib/hotel_price/expedia/expedia_scraper.rb', line 35 def make_query_string(checkin_date, num_adults) cd_args = make_date_args checkin_date na_args = make_num_adults_arg num_adults "#{cd_args}&#{na_args}&x_pwa=1&rfrr=HSR&pwa_ts=1583335742618&swpToggleOn=true®ionId=3250&destination=Sapporo%2C+Hokkaido%2C+Japan&destType=MARKET&neighborhoodId=6290332&selected=5224778&sort=recommended&top_dp=5686&top_cur=JPY" end |