Skip to content

Commit

Permalink
Fix the CoinMarketCap provider.
Browse files Browse the repository at this point in the history
remove referer
use TaskSupervisor
handle non-200 http code
  • Loading branch information
bchamagne authored and samuelmanzanera committed May 9, 2023
1 parent 0ee687e commit 6bc44ef
Showing 1 changed file with 33 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,37 +26,43 @@ defmodule Archethic.OracleChain.Services.UCOPrice.Providers.CoinMarketCap do
]

returned_prices =
Task.async_stream(pairs, fn pair ->
headers = [
{'user-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'},
{'accept', 'text/html'},
{'accept-language', 'en-US,en;q=0.9,es;q=0.8'},
{'upgrade-insecure-requests', '1'},
{'referer', 'https://archethic.net/'},
{'Cookie', 'currency=#{pair}'}
]
Task.Supervisor.async_stream_nolink(
Archethic.TaskSupervisor,
pairs,
fn pair ->
headers = [
{'user-agent',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'},
{'accept', 'text/html'},
{'accept-language', 'en-US,en;q=0.9,es;q=0.8'},
{'upgrade-insecure-requests', '1'},
{'Cookie', 'currency=#{pair}'}
]

with {:ok, {{_, 200, 'OK'}, _headers, body}} <-
:httpc.request(:get, {query, headers}, httpc_options, []),
{:ok, document} <- Floki.parse_document(body) do
price =
Floki.find(document, "div.priceTitle > div.priceValue > span")
|> Floki.text()
|> String.graphemes()
|> Enum.filter(&(&1 in [".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]))
|> Enum.into("")
|> String.to_float()

with {:ok, {{_, 200, 'OK'}, _headers, body}} <-
:httpc.request(:get, {query, headers}, httpc_options, []),
{:ok, document} <- Floki.parse_document(body) do
price =
Floki.find(document, "div.priceTitle > div.priceValue > span")
|> Floki.text()
|> String.graphemes()
|> Enum.filter(&(&1 in [".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]))
|> Enum.into("")
|> String.to_float()
{:ok, {pair, [price]}}
else
{:ok, {{_, _, status}, _, _}} ->
{:error, status}

{:ok, {pair, [price]}}
else
:error ->
{:error, "invalid content"}
:error ->
{:error, "invalid content"}

{:error, _} = e ->
e
{:error, _} = e ->
e
end
end
end)
)
|> Stream.filter(&match?({:ok, {:ok, _}}, &1))
|> Stream.map(fn {:ok, {:ok, val}} -> val end)
|> Enum.into(%{})
Expand Down

0 comments on commit 6bc44ef

Please sign in to comment.