Class: Rakuten::Travel::Crawler::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/rakuten/travel/crawler/api.rb

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Api

Returns a new instance of Api.



5
6
7
8
9
10
# File 'lib/rakuten/travel/crawler/api.rb', line 5

def initialize params
  @config = {
    rakuten_hotel_id: params[:rakuten_hotel_id].to_s ||= 0,
    rakuten_api_key: params[:rakuten_api_key] ||= ENV["RT_API_KEY"]
  }
end

Instance Method Details

#get_page_num(detail_class_code) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rakuten/travel/crawler/api.rb', line 149

def get_page_num detail_class_code
  hotel = hotel_info
  url = "https://app.rakuten.co.jp/services/api/Travel/SimpleHotelSearch/20170426?applicationId=#{@config[:rakuten_api_key]}&largeClassCode=japan&middleClassCode=#{hotel[:middle_class_code]}&smallClassCode=#{hotel[:small_class_code]}&detailClassCode=#{detail_class_code}"
  uri = URI.parse(url)
  json = Net::HTTP.get(uri)
  result = JSON.parse(json)
  {
    small_class_code: hotel[:small_class_code],
    middle_class_code: hotel[:middle_class_code],
    detail_class_code: detail_class_code,
    area_name: hotel[:area_name].to_s,
    page_num: result["pagingInfo"]["pageCount"].to_i
  }
end

#get_price(rakuten_hotel_id, checkin_date, num_adults) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rakuten/travel/crawler/api.rb', line 12

def get_price(rakuten_hotel_id, checkin_date, num_adults)
  query_string = make_query_string(rakuten_hotel_id, checkin_date.to_s, num_adults)
  url = "https://app.rakuten.co.jp/services/api/Travel/VacantHotelSearch/20131024?#{query_string}"
  json = Net::HTTP.get(URI.parse(url))
  result = JSON.parse(json)
  if result["error"] == "not_found"
    {
      date: DateTime.now.strftime("%Y-%m-%d"),
      checkin_date: checkin_date,
      rakuten_hotel_id: @config[:rakuten_hotel_id],
      adult_num: num_adults,
      breakfast: "",
      plan_num: 0,
      min_price: 0
    }
  elsif result["error"] == "wrong_parameterd"
    "入力した値が正しくありません。"
  else
    {
      date: Time.now.strftime("%Y-%m-%d"),
      checkin_date: checkin_date,
      rakuten_hotel_id: rakuten_hotel_id,
      hotel_name: result["hotels"][0]["hotel"][0]["hotelBasicInfo"]["hotelName"],
      adult_num: num_adults,
      breakfast: "",
      plan_num: result["pagingInfo"]["recordCount"],
      room_name: result["hotels"][0]["hotel"][1]["roomInfo"][0]["roomBasicInfo"]["roomName"],
      plan_name: result["hotels"][0]["hotel"][1]["roomInfo"][0]["roomBasicInfo"]["planName"],
      min_price: result["hotels"][0]["hotel"][1]["roomInfo"][1]["dailyCharge"]["rakutenCharge"]
    }
  end
end

#hotel_infoObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rakuten/travel/crawler/api.rb', line 51

def hotel_info
  uri = URI.parse("https://app.rakuten.co.jp/services/api/Travel/HotelDetailSearch/20131024?hotelNo=" + @config[:rakuten_hotel_id].to_s + "&applicationId=" + @config[:rakuten_api_key].to_s + "&datumType=1&responseType=large")
  json = Net::HTTP.get(uri)
  result = JSON.parse(json)
  return "ホテル情報がありませんでした。" if result["error"] == "not_found"
  return result["error_description"] if result["error"] == "wrong_parameter"
  result["hotels"][0].each do |_key, field|
    field[0].each do |_, value|
      @data_hash = {
        rakuten_hotel_id: value["hotelNo"],
        hotel_name: value["hotelName"],
        room_price_min: value["hotelMinCharge"],
        lat: value["latitude"],
        lon: value["longitude"],
        tel: value["telephoneNo"],
        zip_code: value["postalCode"],
        prefecture: value["address1"],
        address1: value["address2"],
        fax: value["faxNo"],
        access: value["access"],
        parking_info: value["parkingInformation"],
        near_station: value["nearestStation"],
        hotel_img_url: value["hotelImageUrl"],
        rakuten_review_count: value["reviewCount"],
        rakuten_review_avg: value["reviewAverage"].to_f
      }
    end
  end
  result["hotels"][0].each do |_key, field|
    field[1].each do |_, value|
      @data_hash[:rakuten_service_review] = value["serviceAverage"].to_f
      @data_hash[:rakuten_location_review] = value["locationAverage"].to_f
      @data_hash[:rakuten_room_review] = value["roomAverage"].to_f
      @data_hash[:rakuten_equipment_review] = value["equipmentAverage"].to_f
      @data_hash[:rakuten_bath_review] = value["bathAverage"].to_f
      @data_hash[:rakuten_meal_review] = value["mealAverage"].to_f
    end
  end
  result["hotels"][0].each do |_key, field|
    field[2].each do |_, value|
      @data_hash[:middle_class_code] = value["middleClassCode"].to_s
      @data_hash[:small_class_code] = value["smallClassCode"].to_s
      @data_hash[:area_name] = value["areaName"].to_s
      @data_hash[:hotel_class_code] = value["hotelClassCode"].to_s
      @data_hash[:checkin_time] = value["checkinTime"].to_s
      @data_hash[:checkout_time] = value["checkoutTime"].to_s
      @data_hash[:last_checkin_time] = value["lastCheckinTime"].to_s
    end
  end
  result["hotels"][0].each do |_key, field|
    field[3].each do |_, value|
      @data_hash[:total_room_num] = value["hotelRoomNum"].to_s
      room_facilities = []
      value["roomFacilities"].each_with_index do |f, i|
        room_facilities[i] = f["item"]
      end
      @data_hash[:room_facilities] = room_facilities
    end
  end
  result["hotels"][0].each do |_key, field|
    field[4].each do |_, value|
      @data_hash[:hotel_policy_note] = value["note"].to_s
      @data_hash[:cancel_policy] = value["cancelPolicy"].to_s
    end
  end
  @data_hash
end

#make_query_string(rakuten_hotel_id, checkin_date, num_adults) ⇒ Object



45
46
47
48
# File 'lib/rakuten/travel/crawler/api.rb', line 45

def make_query_string(rakuten_hotel_id, checkin_date, num_adults)
  checkout_date = (Date.parse(checkin_date) + 1).strftime("%Y-%m-%d")
  "format=json&sort=%2BroomCharge&searchPattern=1&applicationId=#{@config[:rakuten_api_key]}&hotelNo=#{rakuten_hotel_id}&adultNum=#{num_adults}&checkinDate=#{checkin_date}&checkoutDate=#{checkout_date}&squeezeCondition="
end

#search_ranking(params) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rakuten/travel/crawler/api.rb', line 119

def search_ranking params
  body = {
    middle_class_code: params[:middle_class_code],
    small_class_code: params[:small_class_code],
    detail_class_code: params[:detail_class_code],
    page_num: params[:page_num]
  }
  url = "https://app.rakuten.co.jp/services/api/Travel/SimpleHotelSearch/20170426?applicationId=#{@config[:rakuten_api_key]}&largeClassCode=japan&middleClassCode=#{body[:middle_class_code]}&smallClassCode=#{body[:small_class_code]}&detailClassCode=#{body[:detail_class_code]}&page=#{body[:page_num]}"
  uri = URI.parse(url)
  json = Net::HTTP.get(uri)
  result = JSON.parse(json)
  i = 1
  result["hotels"].each do |key, _value|
    if @config[:rakuten_hotel_id] == key["hotel"][0]["hotelBasicInfo"]["hotelNo"].to_s
      return {
        status: "found",
        hotel_name: key["hotel"][0]["hotelBasicInfo"]["hotelName"],
        area_name: body[:area_name],
        page_num: params[:page_num],
        area_rank: i.to_i + ((body[:page_num].to_i - 1) * 30),
        middle_class_code: body[:middle_class_code],
        small_class_code: body[:small_class_code],
        detail_class_code: body[:detail_class_code]
      }
    end
    i += 1
  end
  { status: "not_found" }
end