-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from fedy97/feature/add-coinmarketcap-data-pro…
…vider feat: add coinmarketcap data provider
- Loading branch information
Showing
10 changed files
with
182 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
src/main/java/org/bot/providers/CoinMarketCapProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package org.bot.providers; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.apache.http.client.HttpResponseException; | ||
import org.bot.models.Coin; | ||
import org.bot.models.Portfolio; | ||
import org.bot.models.Trending; | ||
import org.bot.utils.exceptions.CoingeckoException; | ||
import org.bot.utils.exceptions.NotImplementedException; | ||
|
||
import java.util.Arrays; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
public class CoinMarketCapProvider implements DataProvider { | ||
private static CoinMarketCapProvider instance; | ||
|
||
private CoinMarketCapProvider() { | ||
// Private constructor to prevent instantiation | ||
} | ||
|
||
public static CoinMarketCapProvider getInstance() { | ||
if (instance == null) { | ||
instance = new CoinMarketCapProvider(); | ||
} | ||
return instance; | ||
} | ||
|
||
@Override | ||
public Portfolio getPortfolio(String url) { | ||
Map<String, Coin> coins = new LinkedHashMap<>(); | ||
Response response = null; | ||
OkHttpClient client = new OkHttpClient(); | ||
try { | ||
Request request = this.buildGetRequest(url); | ||
response = client.newCall(request).execute(); | ||
if (!response.isSuccessful()) | ||
throw new HttpResponseException(response.code(), response.toString()); | ||
assert response.body() != null; | ||
String responseBody = response.body().string(); | ||
String[] coinsRaw = responseBody.split("class=\"sc-4984dd93-0 kKpPOn\">"); | ||
for (int i = 1; i < coinsRaw.length; i = i + 1) { | ||
Coin coin = this.fromRawCoin(coinsRaw[i]); | ||
if (coin.getTicker() != null) coins.put(coin.getTicker().toUpperCase(), coin); | ||
} | ||
} catch (Exception e) { | ||
log.error(Arrays.toString(e.getStackTrace())); | ||
log.error(e.toString()); | ||
throw new CoingeckoException(); | ||
} finally { | ||
if (response != null) { | ||
response.close(); | ||
} | ||
} | ||
return new Portfolio(coins); | ||
} | ||
|
||
@Override | ||
public Trending getTrendingCoins() { | ||
throw new NotImplementedException(); | ||
} | ||
|
||
@Override | ||
public Coin fromRawCoin(String raw) { | ||
Coin coin = new Coin(); | ||
try { | ||
coin.setCoinName(raw.split("<")[0]); | ||
} catch (Exception e) { | ||
log.warn("name for token ignored: " + e); | ||
} | ||
try { | ||
coin.setTicker(raw.split("click=\"true\">")[1].split("<")[0]); | ||
} catch (Exception e) { | ||
log.warn("ticker for token ignored: " + e); | ||
} | ||
try { | ||
coin.setLink("https://coinmarketcap.com" + raw.split("<a href=\"")[1].split("\"")[0]); | ||
} catch (Exception e) { | ||
log.warn("link for token ignored: " + e); | ||
} | ||
try { | ||
coin.setPrice(Double.parseDouble(raw.split("\"cmc-link\"><span>\\$")[1].split("<")[0].replace(",", ""))); | ||
} catch (Exception e) { | ||
log.warn("price for token ignored: " + e); | ||
} | ||
try { | ||
coin.setChange24(raw.split("display:inline-block\"></span>")[2].split("<")[0]); | ||
} catch (Exception e) { | ||
log.warn("change24 for token ignored: " + e); | ||
} | ||
return coin; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.bot.providers; | ||
|
||
import okhttp3.Request; | ||
import org.bot.models.Coin; | ||
import org.bot.models.Portfolio; | ||
import org.bot.models.Trending; | ||
|
||
public interface DataProvider { | ||
Portfolio getPortfolio(String url); | ||
|
||
Trending getTrendingCoins(); | ||
|
||
default Request buildGetRequest(String url) { | ||
return new Request.Builder() | ||
.url(url) | ||
.get() | ||
.build(); | ||
} | ||
|
||
Coin fromRawCoin(String raw); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters