Current section
Files
Jump to
Current section
Files
lib/site.ex
defmodule Tesourex.Site do
import Tesourex.Helper
def parse(tesouro_html) do
tesouro_html
|> Floki.find(".tabelaPrecoseTaxas")
|> merge_tables
|> add_last_updated_at(parse_last_updated_at(tesouro_html))
end
def add_last_updated_at(summary, last_updated_at) do
Enum.reduce(Map.keys(summary), %{}, fn bond_name, acc ->
Map.merge(acc, %{ bond_name => Map.merge(summary[bond_name], %{ last_updated_at: last_updated_at }) })
end)
end
def parse_last_updated_at(tesouro_html) do
tesouro_html
|> Floki.find(".precosetaxas-portlet .portlet-body b")
|> Enum.at(0)
|> Floki.text
|> String.split(" ")
|> Enum.at(0)
end
defp merge_tables([sell_table, buy_table]) do
Map.merge(parse_table(sell_table),
parse_table(buy_table),
fn _k, sell_info, buy_info -> Map.merge(sell_info, buy_info) end)
end
defp parse_table(table) do
table
|> Floki.find("tr.camposTesouroDireto")
|> Enum.map(&parse_row/1)
|> Enum.reduce(%{}, &Map.merge/2)
end
defp parse_row(tr) do
[original_name|info] = tr |> elem(2) |> Enum.map(&Floki.text/1)
name = without_legacy_name(original_name)
%{ name => parse_info([name|info]) }
end
defp parse_info([name,mature_at,sell_tax,sell_price]) do
%{
name: name,
mature_at: mature_at,
sell_rate: parse_value(sell_tax),
sell_price: parse_value(sell_price)
}
end
defp parse_info([name,mature_at,buy_tax,_,buy_price]) do
%{
name: name,
mature_at: mature_at,
buy_rate: parse_value(buy_tax),
buy_price: parse_value(buy_price)
}
end
defp without_legacy_name(name) do
name |> String.split(" (") |> Enum.at(0)
end
end