Last active
March 5, 2018 18:39
-
-
Save zzuummaa/235eb31fb83af3cd10e9ba8489439f39 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.squareup.okhttp.OkHttpClient; | |
import com.squareup.okhttp.Request; | |
import com.squareup.okhttp.Response; | |
import java.io.IOException; | |
public class BitfinexAccessor { | |
private static String urlOverHttps = "https://api.bitfinex.com/v1/pubticker/btcusd"; | |
private OkHttpClient client; | |
private Exception exception; | |
private double usdbtc; | |
private long lastUSDBTCRespTime; | |
public BitfinexAccessor() { | |
this.client = new OkHttpClient();; | |
} | |
public synchronized double getUSDBTC() { | |
if (lastUSDBTCRespTime + 60 * 1000 < System.currentTimeMillis()) { | |
lastUSDBTCRespTime = System.currentTimeMillis(); | |
try { | |
usdbtc = requestUSDBTC(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
exception = e; | |
} | |
exception = null; | |
} | |
return usdbtc; | |
} | |
protected double requestUSDBTC() throws IOException { | |
Request request = new Request.Builder() | |
.url(urlOverHttps) | |
.build(); | |
Response response = client.newCall(request).execute(); | |
ObjectMapper objectMapper = new ObjectMapper(); | |
JsonNode jsonNode = objectMapper.readTree(response.body().string()); | |
return jsonNode.get("last_price").asDouble(); | |
} | |
public synchronized long getUSDBTCUpdateTime() { | |
return lastUSDBTCRespTime; | |
} | |
public synchronized Exception getException() { | |
return exception; | |
} | |
} |
This file contains hidden or 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
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>ru.zuma</groupId> | |
<artifactId>BitfinexArtem</artifactId> | |
<packaging>war</packaging> | |
<version>1.0-SNAPSHOT</version> | |
<name>BitfinexArtem Maven Webapp</name> | |
<url>http://maven.apache.org</url> | |
<dependencies> | |
<dependency> | |
<groupId>junit</groupId> | |
<artifactId>junit</artifactId> | |
<version>3.8.1</version> | |
<scope>test</scope> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.0.1</version> | |
<scope>provided</scope> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp/okhttp --> | |
<dependency> | |
<groupId>com.squareup.okhttp</groupId> | |
<artifactId>okhttp</artifactId> | |
<version>2.7.5</version> | |
</dependency> | |
<dependency> | |
<groupId>com.fasterxml.jackson.core</groupId> | |
<artifactId>jackson-databind</artifactId> | |
<version>2.9.4</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<finalName>bitfinex</finalName> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<configuration> | |
<source>1.8</source> | |
<target>1.8</target> | |
</configuration> | |
</plugin> | |
<plugin> | |
<groupId>org.apache.tomcat.maven</groupId> | |
<artifactId>tomcat7-maven-plugin</artifactId> | |
<version>2.2</version> | |
<configuration> | |
<url>http://zzuummaa.sytes.net:8070/manager/text</url> | |
<server>MyAmazingServer</server> | |
<path>/bitfinex</path> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment