From 6bc44ef9afef0173c6ec6fefd04f83fda8f6ae1d Mon Sep 17 00:00:00 2001 From: Bastien CHAMAGNE Date: Tue, 9 May 2023 12:44:13 +0200 Subject: [PATCH] Fix the CoinMarketCap provider. remove referer use TaskSupervisor handle non-200 http code --- .../uco_price/providers/coin_marketcap.ex | 60 ++++++++++--------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/archethic/oracle_chain/services/uco_price/providers/coin_marketcap.ex b/lib/archethic/oracle_chain/services/uco_price/providers/coin_marketcap.ex index 1ec3dfb11..50a18cdf6 100644 --- a/lib/archethic/oracle_chain/services/uco_price/providers/coin_marketcap.ex +++ b/lib/archethic/oracle_chain/services/uco_price/providers/coin_marketcap.ex @@ -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(%{})