-
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.
- Loading branch information
1 parent
f0c2cc3
commit 7fe2e23
Showing
6 changed files
with
254 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5" inherit-compiler-output="false"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.0" level="project" /> | ||
<orderEntry type="library" name="Maven: org.json:json:20160810" level="project" /> | ||
</component> | ||
</module> |
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,25 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<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/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<groupId>StockFetcher</groupId> | ||
<artifactId>stock-fetcher</artifactId> | ||
<version>1.0-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>com.google.code.gson</groupId> | ||
<artifactId>gson</artifactId> | ||
<version>2.8.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.json</groupId> | ||
<artifactId>json</artifactId> | ||
<version>20160810</version> | ||
</dependency> | ||
</dependencies> | ||
|
||
|
||
</project> |
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,104 @@ | ||
package model; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
/** | ||
* Created by Gaurav on 12-Mar-17. | ||
*/ | ||
public class Stock { | ||
|
||
@SerializedName("t") | ||
private String ticker; | ||
@SerializedName("e") | ||
private String exchange; | ||
@SerializedName("ltt") | ||
private String lastTradeTime; | ||
@SerializedName("l") | ||
private String price; | ||
@SerializedName("lt") | ||
private String lastTradeTimeFormatted; | ||
@SerializedName("lt_dts") | ||
private String lastTradeDateTime; | ||
@SerializedName("c") | ||
private String change; | ||
@SerializedName("cp") | ||
private String changePercentage; | ||
|
||
public String getTicker() { | ||
return ticker; | ||
} | ||
|
||
public void setTicker(String ticker) { | ||
this.ticker = ticker; | ||
} | ||
|
||
public String getExchange() { | ||
return exchange; | ||
} | ||
|
||
public void setExchange(String exchange) { | ||
this.exchange = exchange; | ||
} | ||
|
||
public String getLastTradeTime() { | ||
return lastTradeTime; | ||
} | ||
|
||
public void setLastTradeTime(String lastTradeTime) { | ||
this.lastTradeTime = lastTradeTime; | ||
} | ||
|
||
public String getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(String price) { | ||
this.price = price; | ||
} | ||
|
||
public String getLastTradeTimeFormatted() { | ||
return lastTradeTimeFormatted; | ||
} | ||
|
||
public void setLastTradeTimeFormatted(String lastTradeTimeFormatted) { | ||
this.lastTradeTimeFormatted = lastTradeTimeFormatted; | ||
} | ||
|
||
public String getLastTradeDateTime() { | ||
return lastTradeDateTime; | ||
} | ||
|
||
public void setLastTradeDateTime(String lastTradeDateTime) { | ||
this.lastTradeDateTime = lastTradeDateTime; | ||
} | ||
|
||
public String getChange() { | ||
return change; | ||
} | ||
|
||
public void setChange(String change) { | ||
this.change = change; | ||
} | ||
|
||
public String getChangePercentage() { | ||
return changePercentage; | ||
} | ||
|
||
public void setChangePercentage(String changePercentage) { | ||
this.changePercentage = changePercentage; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "Stock{" + | ||
"ticker='" + ticker + '\'' + | ||
", exchange='" + exchange + '\'' + | ||
", lastTradeTime='" + lastTradeTime + '\'' + | ||
", price='" + price + '\'' + | ||
", lastTradeTimeFormatted='" + lastTradeTimeFormatted + '\'' + | ||
", lastTradeDateTime='" + lastTradeDateTime + '\'' + | ||
", change='" + change + '\'' + | ||
", changePercentage='" + changePercentage + '\'' + | ||
'}'; | ||
} | ||
} |
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,14 @@ | ||
package service; | ||
|
||
import model.Stock; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Gaurav on 12-Mar-17. | ||
*/ | ||
public interface GoogleFinance { | ||
Stock getStockDetails(String exchange, String ticker) throws IOException; | ||
List<Stock> getStockDetails(String exchange, String[] tickers) throws IOException; | ||
} |
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,90 @@ | ||
package service; | ||
|
||
import com.google.gson.Gson; | ||
import model.Stock; | ||
import org.json.JSONObject; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Gaurav on 12-Mar-17. | ||
*/ | ||
public class GoogleFinanceImpl implements GoogleFinance { | ||
public Stock getStockDetails(String exchange, String ticker) throws IOException { | ||
Stock stock = new Stock(); | ||
//Rest call to google finance API | ||
URL url = new URL("http://finance.google.com/finance/info?client=ig&q="+exchange+"%3A"+ticker); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Accept", "application/json"); | ||
if (conn.getResponseCode() != 200) { | ||
throw new RuntimeException("Failed : HTTP error code : " | ||
+ conn.getResponseCode()); | ||
} | ||
StringBuilder stringBuilder = new StringBuilder(readFullyAsString(conn.getInputStream(), "UTF-8")); | ||
for (int index = 0; index < stringBuilder.length(); index++) { | ||
if (stringBuilder.charAt(index) == '/' || stringBuilder.charAt(index) == ']' || stringBuilder.charAt(index) == '['){ | ||
stringBuilder.setCharAt(index, ' '); | ||
} | ||
} | ||
String stockJson = stringBuilder.toString(); | ||
Gson gson = new Gson(); | ||
stock = gson.fromJson(stockJson, Stock.class); | ||
return stock; | ||
} | ||
|
||
public List<Stock> getStockDetails(String exchange, String[] tickers) throws IOException { | ||
List<Stock> stocks = new ArrayList<Stock>(); | ||
Stock stock = new Stock(); | ||
//Rest call to google finance API | ||
for(int i=1;i<tickers.length;i++) | ||
tickers[0]+=","+tickers[i]; | ||
URL url = new URL("http://finance.google.com/finance/info?client=ig&q="+exchange+"%3A"+tickers[0]); | ||
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | ||
conn.setRequestMethod("GET"); | ||
conn.setRequestProperty("Accept", "application/json"); | ||
if (conn.getResponseCode() != 200) { | ||
throw new RuntimeException("Failed : HTTP error code : " | ||
+ conn.getResponseCode()); | ||
} | ||
StringBuilder stringBuilder = new StringBuilder(readFullyAsString(conn.getInputStream(), "UTF-8")); | ||
for (int index = 0; index < stringBuilder.length(); index++) { | ||
if (stringBuilder.charAt(index) == '/' || stringBuilder.charAt(index) == ']' || stringBuilder.charAt(index) == '['){ | ||
stringBuilder.setCharAt(index, ' '); | ||
} | ||
} | ||
String string = stringBuilder.toString(); | ||
string = string.replaceAll("\\s",""); | ||
String s[] = string.split("},"); | ||
for (int o=0;o<s.length-1;o++) | ||
{ | ||
s[o]+="}"; //add the } we removed in the previous step | ||
} | ||
Gson gson = new Gson(); | ||
for (String str:s) { | ||
stock = gson.fromJson(str,Stock.class); | ||
stocks.add(stock); | ||
} | ||
return stocks; | ||
} | ||
|
||
public String readFullyAsString(InputStream inputStream, String encoding) throws IOException { | ||
return readFully(inputStream).toString(encoding); | ||
} | ||
|
||
private ByteArrayOutputStream readFully(InputStream inputStream) throws IOException { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
byte[] buffer = new byte[1024]; | ||
int length = 0; | ||
while ((length = inputStream.read(buffer)) != -1) { | ||
baos.write(buffer, 0, length); | ||
} | ||
return baos; | ||
} | ||
} |
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,4 @@ | ||
#Enter exchange | ||
exchange=NASDAQ | ||
#Enter StockTicker | ||
ticker=AAPL |