class ESpider::Front::Qunar::Hotel

Public Class Methods

new(hotelId) click to toggle source
Calls superclass method ESpider::Front::Qunar::Base::new
# 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

Public Instance Methods

abstract_desc() click to toggle source

简略描述

# 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
address() click to toggle source
# 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
brand() click to toggle source
# File lib/espider/front/qunar/hotel.rb, line 74
def brand
        brand ||= @hotel.search("//li[contains(text(),'所属品牌:')]").text.sub('所属品牌:','').strip
        brand.strip
end
citynameCn() click to toggle source
# 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
citynameEn() click to toggle source
# File lib/espider/front/qunar/hotel.rb, line 36
def citynameEn
        @cityurl.sub('_city','')
end
comments() click to toggle source
# 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_date() click to toggle source
# 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
desc() click to toggle source
# 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
facilities() click to toggle source

设施

# 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_comment() click to toggle source

好评

# 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
id() click to toggle source
# File lib/espider/front/qunar/hotel.rb, line 46
def id
        @qunar_id
end
images() click to toggle source
# 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
insttime() click to toggle source

开业时间

# File lib/espider/front/qunar/hotel.rb, line 64
def insttime
        date ||= @hotel.search("p[@class='insttime']/cite").text.sub("开业时间:","")
        date.strip
end
name() click to toggle source
# 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
phone() click to toggle source
# File lib/espider/front/qunar/hotel.rb, line 54
def phone
        phone ||= @hotel.search("//li[contains(text(),'电话:')]").text.sub('电话:','').strip
        phone
end
reference_price() click to toggle source
# 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
score() click to toggle source
# 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
star() click to toggle source
# 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_comment() click to toggle source

评论总数

# 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
traffic() click to toggle source

交通信息

# 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
xy() click to toggle source
# 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