Class: Hostelworld

Inherits:
Object
  • Object
show all
Defined in:
lib/hostelify/hostelworld.rb

Constant Summary collapse

HW_SINGULAR_DETAIL_URL =

constants location list includes/indexjs.js

"http://www.hostelworld.com/hosteldetails.php?HostelNumber="
HW_SINGULAR_IMAGE_URL =
"http://www.hostelworld.com/hostelpictures.php?HostelNumber="
HW_SINGULAR_AVAILABILITY =
"http://www.hostelworld.com/availability.php/"
HW_SINGULAR_YOUTUBE_URL =
"http://www.hostelworld.com/youtubevideo.php?HostelNumber="
HW_PLURAL_HOSTELS_URL =
"http://www.hostelworld.com/findabed.php/"

Class Method Summary collapse

Class Method Details

.find_hostel_by_id(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
# File 'lib/hostelify/hostelworld.rb', line 22

def self.find_hostel_by_id(options)
  opts = { :directions => false, :images => false, :all => false }.merge options
  id = options[:id].to_s
  url = HW_SINGULAR_DETAIL_URL + id
  
  #coder = HTMLEntities.new
  hostel = Hostelify.new
  hostel.hostel_id = id
  
  if options[:date]
    options = @default_options.merge(options)
    date = Date.strptime(options[:date])
    data = setSearch(url, options[:date], options[:no_ppl], options[:no_days])
  else
    data = parse_html(url)
  end
  
  unless data == "Full" 
    data = data.search("//div[@id='content']")
    data.search("h3").remove #get rid of header
  
    #title, address, desc, facilities, ratings
    hostel.name = data.at("h2").inner_text.gsub(/( in  ).*$/,'')
    hostel.address = data.at('div[@style="padding-top: 5px"]').inner_text.lstrip
  
    if options[:date]
      hostel.availability = parse_availables(data)
    else
      hostel.description = data.at('div[@id="microDescription2]').inner_text
    end
  
    #optional
    no_photos = data.at('div[@id="microPicScroll"]/span/a').inner_text.to_i
    #no_photos = data.at('span/a[@id="picLink"]').inner_text.to_i
    video = data.at('div[@id="microVideo"]')
  
    facilities = []
    (data/"li.microFacilitiesBoomLi").each do |item|
      facilities << item.inner_text
    end
  
    ratings = []
    (data/'div[@id="ratingsBar2"]').each do |item|
      ratings << item.inner_text.to_i
    end
  
    hostel.facilities = facilities
    hostel.ratings = ratings
  
    if video #exists
      data = parse_html(HW_SINGULAR_YOUTUBE_URL + id)
      video_url = data.at('param[@name="movie"]')['value']
      hostel.video = video_url
      #video_url = data.at('tag')
    end
  
    if options[:directions] or options[:all]
      data = parse_html(HW_SINGULAR_DETAIL_URL + id + "/directions/")
  
      #directions, geo
      hostel.directions = data.at('div[@id="content"]').inner_text.gsub(/^[\d\D\n]*(DIRECTIONS)/,'')
      hostel.geo = data.to_s.scan(/-{0,1}\d{1,3}\.\d{7}/).uniq!
    end

    if no_photos and (options[:images] or options[:all])
      images = []
      (1..no_photos).each do |i|
        data = parse_html(HW_SINGULAR_IMAGE_URL + id + '&PicNO=' + i.to_s)
        images << (data/"img").first[:src].to_s
      end
      hostel.images = images
    end
  else
    hostel = nil
  end
  hostel # return
end

.find_hostels_by_location(options) ⇒ Object

location



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/hostelify/hostelworld.rb', line 100

def self.find_hostels_by_location(options) #location
  
  city = options[:location].split(',').first.gsub(' ','')
  country = options[:location].split(',').last.gsub(' ','')
  url = HW_PLURAL_HOSTELS_URL + "ChosenCity.#{city}/ChosenCountry.#{country}"
  
  if options[:date]
    options = @default_options.merge(options)
    date = Date.strptime(options[:date])
    data = setSearch2(url, options[:date], options[:no_ppl], options[:no_days])
  else
    data = parse_html(url)
  end
  
  data = data.search("//div[@id='content']")
  @results = HostelifyCollection.new

  (data/"div.hostelListing").each do |row|
    name = row.at("h3").inner_text
    desc = row.at("div.hostelEntry/p").inner_text.to_s.chop.gsub('more info','').squeeze('.')
    url = row.at("h3/a")['href']
    rating = row.at("h4/text()")
    rating = rating.to_s.to_i unless rating.nil?
    type = row.at("div.hostelListingImage/span").inner_text
    hostel_id = url.match(/[\d]*$/).to_s
    
    if options[:date]
      #price_USD = row.at("span.blueBeds").inner_text #need to fix float
      dorm = (row.at("p.hostelListingRate/span.blueBeds/text()")).to_s.gsub(/[A-Z$]*/,'')
      single = row.at("p.hostelListingPrivateRate/span.blueBeds/text()").to_s.gsub(/[A-Z$]*/,'')
      available = row/"ul.hostelListingDates/li.noAvail/text()"
      available = available.to_a.join(',').split(',')
      @results << Hostelify.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => rating, :dorm => dorm, :single => single, :unavailable => available)
    else
      @results << Hostelify.new(:hostel_id => hostel_id, :name => name, :description => desc, :rating => rating)
    end
  end
  return @results
end

.parse_html(url) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/hostelify/hostelworld.rb', line 14

def self.parse_html(url)
  f = open(url)
  f.rewind
  Retryable.try 3 do
    data = Hpricot(Iconv.conv('utf-8', f.charset, f.readlines.join("\n")))
  end
end