Class: Qunar::Hotel

Inherits:
Object
  • Object
show all
Defined in:
lib/qunar/hotel.rb

Constant Summary collapse

BASEURI =
"http://hotel.qunar.com"

Instance Method Summary collapse

Constructor Details

#initialize(hotelId) ⇒ Hotel

Returns a new instance of Hotel.



6
7
8
9
10
11
12
13
14
15
# File 'lib/qunar/hotel.rb', line 6

def initialize(hotelId)
  #super   
  raise "id should be string" if !hotelId.instance_of?(String)
  @HotelSEQ = hotelId             #hotelId is "cityurl_hotelcode"
  @cityurl = hotelId[0..hotelId.rindex("_",-1)-1]
  @hotelcode = hotelId[hotelId.rindex("_",-1)+1..hotelId.length]
  uri = URI(File.join(BASEURI, 'city', @cityurl, "dt-#{@hotelcode}"))
  page = HTTParty.get(uri.to_s)
  @hotel = Nokogiri::HTML page
end

Instance Method Details

#addressObject



70
71
72
73
74
# File 'lib/qunar/hotel.rb', line 70

def address               
  @hotel.search("//p[@class='adress']/span").first['title'].strip
  #return "" if address.nil?
  #return address['title'].strip
end

#citynameCnObject



58
59
60
61
62
63
64
# File 'lib/qunar/hotel.rb', line 58

def citynameCn
  match_data = @hotel.to_s.match(/var\s*cityName\s*=\s*\'\p{Han}+\'/u)
  return "" if match_data.nil?
  match_data = match_data[0].match(/\p{Han}+/u)
  return "" if match_data.nil?
  match_data[0].strip
end

#citynameEnObject



54
55
56
# File 'lib/qunar/hotel.rb', line 54

def citynameEn
  @cityurl.sub('_city','')
end

#coordinateObject



116
117
118
119
120
121
122
123
124
# File 'lib/qunar/hotel.rb', line 116

def coordinate
  xy = @hotel.to_s.match(/hotelPoint=\[(\d+\.\d+),(\d+\.\d+)\];/)
  puts xy
  if xy.nil?
    return [0.0,0.0]
  else
    return [xy[1],xy[2]]
  end
end

#descObject

the description of the hotel



82
83
84
85
86
87
# File 'lib/qunar/hotel.rb', line 82

def desc     
  #desc = @hotel.search("p[@class='summery less_summery']").text
  #desc = @hotel.search("p[@class='summery expan_summery']").text if desc.empty?
  #desc.gsub(/[[:space:]]/, '')
  @hotel.search("div[@class='introduce']/p").text
end

#facilitiesObject

设施



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/qunar/hotel.rb', line 126

def facilities
  facility = @hotel.search("//div[@class=\"roundmilieu htintroborder\"]").first
  facilities = Array.new()
  unless facility.nil?
    facility.search("dl").each do |dl|
      ele = Hash.new
      facility_cata = dl.search("dt").first.text.delete(':')
      ele1 = Array.new
      dl.search("dd/ul/li").each do |facility_type|
        ele1 << facility_type.text
      end
      ele['name'] = facility_cata
      ele['info'] = ele1
      facilities << ele
    end
  end
  facilities
end

#idObject



66
67
68
# File 'lib/qunar/hotel.rb', line 66

def id
  @hotelcode = @hotelcode
end

#nameObject

get the name of the hotel



26
27
28
29
30
31
# File 'lib/qunar/hotel.rb', line 26

def name          #get the name of the hotel
  @hotel.search("//div[@class='htl-info fr']//span").first.text.strip
  # name_tag = @hotel.search("//div[@class='htl-info fr']/h2/span").first
  # return nil if name_tag.nil?
  # name_tag.text[0..name_tag.text.index("\n",1)].strip
end

#opentimeObject

开业时间



90
91
92
93
94
# File 'lib/qunar/hotel.rb', line 90

def opentime
  #date ||= @hotel.search("p[@class='insttime']/cite").text.sub("开业时间:","")
  #date.strip
  @hotel.search("dl[@class='inform-list']//cite")[-2].text.sub("年开业",'')
end

#parse_mixkeyObject

get some kind of code unkown



17
18
19
20
21
22
23
24
# File 'lib/qunar/hotel.rb', line 17

def parse_mixkey                            #get some kind of code unkown
  uri = URI(File.join(BASEURI, 'city', @cityurl, "dt-#{@hotelcode}"))
  http_res = HTTParty.get uri.to_s
  res = Nokogiri::HTML http_res
  node = res.search('//span[@id="eyKxim"]').first
  return nil if node.nil?
  return node.text
end

#phoneObject



76
77
78
79
# File 'lib/qunar/hotel.rb', line 76

def phone
  #phone ||= @hotel.search("//li[contains(text(),'电话:')]").text.sub('电话:','').strip
  @hotel.search("//dl[@class='inform-list']//cite").first.text.sub('电话','').strip
end

#reference_priceObject

#简略描述

def abstract_desc
  abstract_desc = @hotel.search("p[@class='h_desc']").first
  return "" if abstract_desc.nil?
  return abstract_desc.text.sub('描述:','').strip
end

def brand
  brand ||= @hotel.search("//li[contains(text(),'所属品牌:')]").text.sub('所属品牌:','').strip
  brand.strip
end


108
109
110
111
112
113
114
# File 'lib/qunar/hotel.rb', line 108

def reference_price      
  match_data = @hotel.to_s.match(/var\s*miniRetailPrice\s*=\s*\'\d+\';/)
  return 0 if match_data.nil?
  match_data = match_data[0].match(/[\d]+/)
  return 0 if match_data.nil?
  match_data[0].strip
end

#starObject

get the level of the hotel



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/qunar/hotel.rb', line 33

def star         #get the level of the hotel
  #name_tag = @hotel.search('//div[@class="eriefinfo"]//dt').first
  name_tag = @hotel.search('//div[@class="htl-info fr"]//em[@class="dangci"]').first
  return 0 if name_tag.nil?
  if name_tag.text.include?'经济型'
    1
  elsif name_tag.text.include?'舒适型'
    3
  elsif name_tag.text.include?'高档型'
    4
  elsif name_tag.text.include?'豪华型'
    5
  elsif name_tag.text.include?'star20'
    2
  elsif name_tag.text.include?'star50'
    5
  else
    0
  end
end