Current section
Files
Jump to
Current section
Files
lib/parsers/weather_dot_com.ex
defmodule ElixirWeather.Parsers do
defmodule WeatherDotCom do
import Floki
@location_name_getter "h1.h4.today_nowcard-location"
@day_part_section_getter ".today-daypart"
@title_getter ".today-daypart-title"
@conditions_getter ".today-daypart-wxphrase"
@high_low_getter ".today-daypart-hilo"
@temp_getter ".today-daypart-temp"
@percipitation_getter ".today-daypart-precip"
# return error
def get_todays_weather(html) when html == [] do
{
:error,
%{
:todays_list => [],
:location_name => "Unable to get location"
}
}
end
# return results
def get_todays_weather(html) do
location_name = find(html, @location_name_getter) |> text
day_part = find(html, @day_part_section_getter)
todays_list = Enum.map(day_part, fn(dp) -> parse_todays_weather(dp) end)
{
:ok,
%{
:todays_list => todays_list,
:location_name => location_name
}
}
end
defp parse_todays_weather(dp) do
title = find(dp, @title_getter) |> text
conditions = find(dp, @conditions_getter) |> text
low_high = find(dp, @high_low_getter) |> text
temp = find(dp, @temp_getter) |> text
perc = find(dp, @percipitation_getter) |> text
%{
:title => title,
:conditions => conditions,
:low_high => low_high,
:temp => temp,
:percipitation => "Percipitation #{perc}"
}
end
end
end