1 package org.oxerr.okcoin.rest.dto.valuereader;
2
3 import static java.util.Collections.emptyMap;
4
5 import java.math.BigDecimal;
6 import java.util.LinkedHashMap;
7 import java.util.Map;
8
9 import org.apache.commons.lang3.StringUtils;
10 import org.oxerr.okcoin.rest.dto.Funds;
11 import org.oxerr.okcoin.rest.service.web.LoginRequiredException;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.NodeList;
16 import org.w3c.dom.html.HTMLDivElement;
17 import org.w3c.dom.html.HTMLDocument;
18 import org.w3c.dom.html.HTMLElement;
19 import org.w3c.dom.html.HTMLLIElement;
20 import org.w3c.dom.html.HTMLUListElement;
21
22 public class IndexHtmlPageReader extends HtmlPageReader<Funds> {
23
24 private final Logger log = LoggerFactory.getLogger(IndexHtmlPageReader.class);
25
26
27
28
29 @Override
30 public Funds read(HTMLDocument doc) throws LoginRequiredException {
31 BigDecimal cny = null;
32 BigDecimal cnyFrozen = null;
33 BigDecimal btc = null;
34 BigDecimal btcFrozen = null;
35 BigDecimal ltc = null;
36 BigDecimal ltcFrozen = null;
37 BigDecimal netAsset = null;
38 BigDecimal allAsset = null;
39
40 NodeList divNodeList = doc.getElementsByTagName("div");
41 log.debug("divNodeList.length: {}", divNodeList.getLength());
42 for (int i = 0; i < divNodeList.getLength(); i++) {
43 HTMLDivElement divNode = (HTMLDivElement) divNodeList.item(i);
44 String className = divNode.getClassName();
45 log.debug("className: {}", className);
46 if (StringUtils.equals(className, "accountinfo1")) {
47 log.debug("accountinfo1 div: {}", divNode);
48
49 NodeList divs = divNode.getElementsByTagName("div");
50 HTMLDivElement nav2up1 = (HTMLDivElement) divs.item(0);
51
52 log.debug("nav2-up1: {}", nav2up1);
53 log.debug("nav2-center: {}", nav2up1);
54
55 HTMLUListElement ul = (HTMLUListElement) nav2up1
56 .getElementsByTagName("ul").item(0);
57 NodeList liNodeList = ul.getElementsByTagName("li");
58 for (int j = 0; j < liNodeList.getLength(); j++) {
59 HTMLLIElement li = (HTMLLIElement) liNodeList.item(j);
60 NodeList spanNodeList = li.getElementsByTagName("span");
61 String previousText = StringUtils.EMPTY;
62 for (int k = 0; k < spanNodeList.getLength(); k++) {
63 HTMLElement span = (HTMLElement) spanNodeList.item(k);
64 String text = span.getTextContent();
65 switch (previousText) {
66 case "CNY:":
67 if (cny == null) {
68 cny = new BigDecimal(text);
69 } else {
70 cnyFrozen = new BigDecimal(text);
71 }
72 break;
73 case "BTC:":
74 if (btc == null) {
75 btc = new BigDecimal(text);
76 } else {
77 btcFrozen = new BigDecimal(text);
78 }
79 break;
80 case "LTC:":
81 if (ltc == null) {
82 ltc = new BigDecimal(text);
83 } else {
84 ltcFrozen = new BigDecimal(text);
85 }
86 break;
87 default:
88 break;
89 }
90 previousText = text;
91 }
92 }
93
94 break;
95 }
96 }
97
98 Element e = doc.getElementById("netasset");
99 if (e != null) {
100 netAsset = new BigDecimal(e.getTextContent().replace(",", ""));
101 }
102
103 e = doc.getElementById("allasset");
104 if (e != null) {
105 allAsset = new BigDecimal(e.getTextContent().replace(",", ""));
106 }
107
108 if (cny == null && cnyFrozen == null && btc == null
109 && btcFrozen == null && ltc == null && ltcFrozen == null
110 && netAsset == null && allAsset == null) {
111 throw new LoginRequiredException();
112 }
113
114 Map<String, BigDecimal> free = new LinkedHashMap<>();
115 Map<String, BigDecimal> frozen = new LinkedHashMap<>();
116 Map<String, BigDecimal> asset = new LinkedHashMap<>();
117 free.put("btc", btc);
118 free.put("ltc", ltc);
119 free.put("cny", cny);
120 frozen.put("btc", btcFrozen);
121 frozen.put("ltc", ltcFrozen);
122 frozen.put("cny", cnyFrozen);
123 asset.put("net", netAsset);
124 asset.put("total", allAsset);
125 return new Funds(asset, emptyMap(), free, frozen, emptyMap());
126 }
127
128 }