Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def process_response_chunk(body) do
- # The goal here is to grab the table on the wiki page
- # And then convert it into a tuple (name, value)
- props = body
- |> IO.iodata_to_binary
- |> Floki.find("tr.smwb-propvalue")
- # Getting the property names is easy
- # It's just the innerHTML of the first a element in the table header
- prop_names = props
- |> Floki.find("th>a")
- |> Enum.map(&Floki.text/1)
- # It's a little more complex to get the property value
- # The best way to do this is to grab the innerHTML of the property value column
- # And remove the text that isn't the value
- # The property will look like prop_value  +
- # So we have to remove the trailing characters
- prop_values = props
- |> Floki.find("span.smwb-value")
- |> Enum.map(&Floki.text/1)
- # Remove the "+" character from the value
- |> Enum.map(fn (val) -> String.replace(val, "+", "") end)
- # String.strip doesn't seem to work as expected
- # So it's easier to just convert to a char list
- # And remove the character 160, which is  
- # Otherwise known as
- |> Enum.map(&String.to_char_list/1)
- |> Enum.map(fn (val) -> Enum.reject(val, fn (v) -> v == 160 end) end)
- Enum.zip(prop_names, prop_values)
- |> Enum.map(&Tuple.to_list/1)
- |> Enum.map(fn (l) -> Enum.join(l, "\t") end)
- |> Enum.join(",\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement