Class: ESpider::Front::Qunar::Hotel

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

Constant Summary

Constants inherited from Base

Base::BASEURI

Instance Method Summary collapse

Methods inherited from Base

#parse_mixkey

Constructor Details

#initialize(hotelId) ⇒ Hotel

Returns a new instance of Hotel.



7
8
9
10
11
# File 'lib/espider/front/qunar/hotel.rb', line 7

def initialize(hotelId)
	super
	uri = URI(File.join(BaseUri, 'city', @cityurl, "dt-#{@hotelcode}"))
	@hotel = Nokogiri::HTML HTTParty.get(uri.to_s)
end

Instance Method Details

#abstract_descObject

简略描述



69
70
71
72
73
# File 'lib/espider/front/qunar/hotel.rb', line 69

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

#addressObject



49
50
51
52
53
# File 'lib/espider/front/qunar/hotel.rb', line 49

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

#brandObject



74
75
76
77
# File 'lib/espider/front/qunar/hotel.rb', line 74

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

#citynameCnObject



39
40
41
42
43
44
45
# File 'lib/espider/front/qunar/hotel.rb', line 39

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



36
37
38
# File 'lib/espider/front/qunar/hotel.rb', line 36

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

#commentsObject



132
133
134
135
136
137
# File 'lib/espider/front/qunar/hotel.rb', line 132

def comments
	url = "http://review.qunar.com/api/h/#{@qunar_id}/detail/rank/v1/page/1"
	comments = HTTParty.get url
	comments = JSON.parse comments
	comments['data']['list'].map{|comment| JSON.parse(comment['content'])['feedContent'] }
end

#decorate_dateObject



127
128
129
130
131
# File 'lib/espider/front/qunar/hotel.rb', line 127

def decorate_date
	match_data = @hotel.to_s.match(/最后装修时间:\d+年/)
	return "" if match_data.nil?
	match_data[0].strip
end

#descObject



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

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:]]/, '')
end

#facilitiesObject

设施



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/espider/front/qunar/hotel.rb', line 94

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

#good_commentObject

好评



147
148
149
150
151
152
153
# File 'lib/espider/front/qunar/hotel.rb', line 147

def good_comment
	url = "http://review.qunar.com/api/h/#{@qunar_id}/detail/rank/page/1?rate=positive"
	body = HTTParty.get url
	match_data = body.match(/"count":\d+/)
	return "0" if match_data.nil?
	match_data[0].sub(/\"count\":/,"")
end

#idObject



46
47
48
# File 'lib/espider/front/qunar/hotel.rb', line 46

def id
	@qunar_id
end

#imagesObject



154
155
156
157
158
159
# File 'lib/espider/front/qunar/hotel.rb', line 154

def images
	url = "http://hotel.qunar.com/render/hotelDetailAllImage.jsp?hotelseq=#{@qunar_id}"
	body = HTTParty.get url
	body = JSON.parse(body)
	return body['data']["all"] if(body['ret'])
end

#insttimeObject

开业时间



64
65
66
67
# File 'lib/espider/front/qunar/hotel.rb', line 64

def insttime
	date ||= @hotel.search("p[@class='insttime']/cite").text.sub("开业时间:","")
	date.strip
end

#nameObject



12
13
14
15
16
# File 'lib/espider/front/qunar/hotel.rb', line 12

def name
	name_tag = @hotel.search('//dl[@class="eriefinfo"]//dt').first
	return nil if name_tag.nil?
	name_tag.text[0..name_tag.text.index("\n",1)].strip
end

#phoneObject



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

def phone
	phone ||= @hotel.search("//li[contains(text(),'电话:')]").text.sub('电话:','').strip
	phone
end

#reference_priceObject



78
79
80
81
82
83
84
# File 'lib/espider/front/qunar/hotel.rb', line 78

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

#scoreObject



160
161
162
163
164
165
166
167
168
169
# File 'lib/espider/front/qunar/hotel.rb', line 160

def score
	url = "http://review.qunar.com/api/h/#{@qunar_id}/detail"
	body = HTTParty.get url
	begin
		body = JSON.parse body
		return body["data"]["score"].strip
	rescue Exception => e
		return "0.0"
	end	
end

#starObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/espider/front/qunar/hotel.rb', line 17

def star
	name_tag = @hotel.search('//dl[@class="eriefinfo"]//dt').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

#total_commentObject

评论总数



139
140
141
142
143
144
145
# File 'lib/espider/front/qunar/hotel.rb', line 139

def total_comment
	url = "http://review.qunar.com/api/h/#{@qunar_id}/detail/rank/page/1?rate=all"
	body = HTTParty.get url
	match_data = body.match(/"count":\d+/)
	return "0" if match_data.nil?
	match_data[0].sub(/\"count\":/,"").strip
end

#trafficObject

交通信息



113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/espider/front/qunar/hotel.rb', line 113

def traffic
	traffic = "http://hotel.qunar.com/detail/detailMapData.jsp?seq=#{@qunar_id}&type=traffic,canguan,jingdian,ent"
	traffic = HTTParty.get traffic
	traffic_data = Array.new()
	if traffic['ret'] == true
	traffic['data']['ent'].each do |line|
		ele = Hash.new
		ele['name'] = line['name']
		ele['distance'] = line['distance']
		traffic_data << ele
		end
	end
	traffic_data
end

#xyObject



85
86
87
88
89
90
91
92
# File 'lib/espider/front/qunar/hotel.rb', line 85

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