use of suite.streamlet.As in project suite by stupidsing.
the class HttpUtil method httpJre.
@SuppressWarnings("unused")
private static HttpResult httpJre(String method, URL url, Outlet<Bytes> in, Map<String, String> headers) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod(method);
headers.entrySet().forEach(e -> conn.setRequestProperty(e.getKey(), e.getValue()));
try (OutputStream os = conn.getOutputStream()) {
Bytes_.copy(in, os::write);
}
int responseCode = conn.getResponseCode();
Outlet<Bytes> out = To.outlet(conn.getInputStream());
if (//
responseCode == HttpURLConnection.HTTP_MOVED_PERM || //
responseCode == HttpURLConnection.HTTP_MOVED_TEMP || responseCode == HttpURLConnection.HTTP_SEE_OTHER) {
String cookies1 = conn.getHeaderField("Set-Cookie");
URL url1 = To.url(conn.getHeaderField("Location"));
Map<String, String> headers1 = new HashMap<>(headers);
if (cookies1 != null)
headers1.put("Cookie", cookies1);
return http(method, url1, in, headers1);
} else if (responseCode == HttpURLConnection.HTTP_OK)
return new HttpResult(responseCode, out);
else
throw new IOException(//
"HTTP returned " + responseCode + ": " + //
url + ": " + //
conn.getResponseMessage() + ": " + out.collect(As::string));
}
use of suite.streamlet.As in project suite by stupidsing.
the class Lister method leaves.
private Streamlet<IList<Node>> leaves(Node node, IList<Node> prefix) {
NodeRead nr = NodeRead.of(node);
Streamlet<IList<Node>> st;
if (nr.type == ReadType.TUPLE)
st = //
Read.from(//
nr.children).index().map(//
(i, p) -> leaves(p.t1, IList.cons(Int.of(i), prefix))).collect(As::concat);
else if (nr.type != ReadType.TERM)
st = Read.from(nr.children).concatMap(p -> leaves(p.t1, IList.cons(p.t0, prefix)));
else
st = Read.from(List.of(IList.cons(nr.terminal, prefix)));
if (nr.op != null)
st = st.cons(IList.cons(Atom.of(nr.op.toString()), prefix));
return st;
}
use of suite.streamlet.As in project suite by stupidsing.
the class Hkex method queryCompanies_.
private List<Asset> queryCompanies_(int pageNo) {
JsonNode json = query(//
"" + //
"https://www.hkex.com.hk/eng/csm/ws/Result.asmx/GetData" + //
"?location=companySearch" + //
"&SearchMethod=2" + //
"&LangCode=en" + //
"&StockCode=" + //
"&StockName=" + //
"&Ranking=ByMC" + //
"&StockType=MB" + //
"&mkt=hk" + "&PageNo=" + //
(pageNo + 1) + //
"&ATypeSHEx=" + //
"&AType=" + //
"&FDD=" + //
"&FMM=" + //
"&FYYYY=" + //
"&TDD=" + //
"&TMM=" + "&TYYYY=");
CompanySearch companySearch = mapper.convertValue(json, CompanySearch.class);
Streamlet<List<String>> data0;
if (Boolean.TRUE)
data0 = //
Read.each(//
companySearch).flatMap(//
cs -> cs.data).concatMap(Data::tableEntries);
else
data0 = //
Read.each(//
json).flatMap(//
json_ -> json_.path("data")).flatMap(//
json_ -> json_.path("content")).flatMap(//
json_ -> json_.path("table")).flatMap(//
json_ -> json_.path("tr")).filter(//
json_ -> !json_.path("thead").asBoolean()).flatMap(//
json_ -> json_.path("td")).map(json_ -> Read.from(json_).map(JsonNode::asText).toList());
Streamlet<List<String>> data1 = data0.collect(As::streamlet);
Map<String, Integer> lotSizeBySymbol = queryLotSizeBySymbol_(data1.map(this::toSymbol));
return data1.map(datum -> toAsset(datum, lotSizeBySymbol)).toList();
}
use of suite.streamlet.As in project suite by stupidsing.
the class Yahoo method dataSourceYql.
public DataSource dataSourceYql(String symbol, TimeRange period) {
String yql = //
"select *" + //
" from yahoo.finance.historicaldata" + " where symbol = \"" + symbol + //
"\"" + " and startDate = \"" + period.from.ymd() + //
"\"" + " and endDate = \"" + period.to.ymd() + "\"";
String urlString = //
"http://query.yahooapis.com/v1/public/yql" + "?q=" + //
encode(yql) + //
"&format=json" + //
"&diagnostics=true" + //
"&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys" + "&callback=";
return Rethrow.ex(() -> {
try (InputStream is = Singleton.me.storeCache.http(urlString).collect(To::inputStream)) {
JsonNode json = mapper.readTree(is);
Streamlet<JsonNode> quotes = //
Read.each(json).flatMap(//
json_ -> json_.path("query")).flatMap(//
json_ -> json_.path("results")).flatMap(//
json_ -> json_.path("quote")).collect(As::streamlet);
Streamlet<String[]> arrays = //
quotes.map(json_ -> new String[] { //
json_.path("Date").textValue(), //
json_.path("Open").textValue(), //
json_.path("Close").textValue(), //
json_.path("Low").textValue(), //
json_.path("High").textValue() }).collect(As::streamlet);
long[] ts = arrays.collect(Obj_Lng.lift(array -> closeTs(array[0]))).toArray();
float[] opens = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[1]))).toArray();
float[] closes = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[2]))).toArray();
float[] lows = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[3]))).toArray();
float[] highs = arrays.collect(Obj_Flt.lift(array -> Float.parseFloat(array[4]))).toArray();
float[] volumes = new float[ts.length];
return DataSource.ofOhlcv(ts, opens, closes, lows, highs, volumes);
}
});
}
use of suite.streamlet.As in project suite by stupidsing.
the class Summarize method summarize_.
private //
Summarize_ summarize_(//
Streamlet<Trade> trades_, //
Map<String, Float> priceBySymbol, Iterate<String> infoFun) {
Streamlet<Trade> trades0 = trades_;
Streamlet<Trade> trades1 = sellAll(trades0, priceBySymbol);
Streamlet<String> details = //
Read.from2(//
Trade_.portfolio(trades0)).map((symbol, nShares) -> {
Asset asset = cfg.queryCompany(symbol);
float price = priceBySymbol.get(symbol);
String info = infoFun.apply(symbol);
return //
asset + ": " + price + " * " + //
nShares + " = " + //
((long) (nShares * price)) + (info != null ? " \t(" + info + ")" : "");
}).sort(//
Object_::compare).collect(As::streamlet);
return new Summarize_(details, trades0, trades1);
}
Aggregations