Ruby Bom Fetcher (location weather)


SUBMITTED BY: Guest

DATE: May 22, 2015, 7:13 a.m.

FORMAT: Ruby

SIZE: 3.3 kB

HITS: 780

  1. require 'nokogiri'
  2. require 'open-uri'
  3. # Bom Fetcher Module
  4. # ------------------
  5. # Fetches the locations from BoM or updates the location weather
  6. # from BoM.
  7. module BomFetcher
  8. # The source name, and URL to fetch the data from.
  9. SOURCE = "bom"
  10. URL = "http://www.bom.gov.au/vic/observations/melbourne.shtml"
  11. def self.get_location_dom
  12. # Opens the HTML document (give it the URL of the HTML file)
  13. doc = Nokogiri::HTML(open(URL))
  14. # Fetches the DOM for the locations
  15. return doc.css("#tMELBOURNE tbody tr")
  16. end
  17. # get_locations
  18. # -------------
  19. # Deletes all the current locations in the database (if any) and
  20. # creates new records with the locations on the HTML page specified by
  21. # the URL.
  22. def self.get_locations
  23. # Delete all locations from the db.
  24. Location.delete_all
  25. locations = get_location_dom
  26. # For each location DOM, scrape the name and create a new
  27. # location.
  28. locations.each do |location|
  29. name = location.css('th a').text
  30. location = Location.new(name: name)
  31. location.save
  32. puts "Location #{name} successfully fetched"
  33. end
  34. end
  35. # get_location_weather
  36. # --------------------
  37. # Scrapes the location weather data from the HTML page given by the URL.
  38. def self.get_location_weather
  39. locations = get_location_dom
  40. # For each location DOM, scrape the relevant weather data and then store it
  41. # into the database.
  42. locations.each do |location|
  43. # Initialise a weather data hash
  44. weather_data = {}
  45. # Get the name of the location
  46. name = location.css('th a').text
  47. # Get the tag of the location
  48. location_tag = location.css('th')[0]["id"]
  49. # Get the location data from the database
  50. location_data = Location.where(name: name).first
  51. # Set the weather data to the relevant values
  52. weather_data[:time] = Time.current
  53. weather_data[:date] = "#{weather_data[:time].day}-#{weather_data[:time].month}-#{weather_data[:time].year}"
  54. weather_data[:curr_temp] = location.css("td").at("td[headers='obs-temp #{location_tag}']").text.to_f
  55. weather_data[:dew_point] = location.css("td").at("td[headers='obs-dewpoint #{location_tag}']").text.to_f
  56. weather_data[:rainfall] = Rainfall.from9am_mm_to_mmhr(location.css("td").at("td[headers='obs-rainsince9am #{location_tag}']").text.to_f)
  57. weather_data[:wind_dir] = location.css("td").at("td[headers='obs-wind obs-wind-dir #{location_tag}']").text
  58. weather_data[:wind_speed] = location.css("td").at("td[headers='obs-wind obs-wind-spd-kph #{location_tag}']").text.to_f
  59. # Insert the weather data into the database
  60. location_data.insert_weather_data(SOURCE, weather_data)
  61. end
  62. end
  63. end

comments powered by Disqus