View Javadoc
1   package org.oxerr.huobi.rest.valuereader;
2   
3   import java.math.BigDecimal;
4   
5   import org.oxerr.huobi.rest.LoginRequiredException;
6   import org.oxerr.huobi.rest.domain.Funds;
7   import org.oxerr.huobi.rest.domain.LoginResult;
8   import org.slf4j.Logger;
9   import org.slf4j.LoggerFactory;
10  import org.w3c.dom.Element;
11  import org.w3c.dom.NodeList;
12  import org.w3c.dom.html.HTMLDocument;
13  import org.w3c.dom.html.HTMLElement;
14  import org.w3c.dom.html.HTMLFormElement;
15  
16  public class LoginResultReader extends HTMLReader<LoginResult> {
17  
18  	private final Logger log = LoggerFactory.getLogger(LoginResult.class);
19  
20  	/**
21  	 * {@inheritDoc}
22  	 */
23  	@Override
24  	protected LoginResult parse(HTMLDocument document) throws LoginRequiredException {
25  		Element loginForm = document.getElementById("login");
26  		if (loginForm instanceof HTMLFormElement) {
27  			throw new LoginRequiredException();
28  		}
29  
30  		final LoginResult loginResult = new LoginResult();
31  		final Funds funds = new Funds();
32  		loginResult.setFunds(funds);
33  
34  		boolean total = false,
35  				netAsset = false,
36  				availableCny = false,
37  				frozenCny = false,
38  				availableBtc = false,
39  				frozenBtc = false,
40  				availableLtc = false,
41  				frozenLtc = false,
42  				loanCny = false,
43  				loanBtc = false,
44  				loanLtc = false;
45  
46  		NodeList spanNodeList = document.getElementsByTagName("span");
47  		for (int i = 0; i < spanNodeList.getLength(); i++) {
48  			HTMLElement element = (HTMLElement) spanNodeList.item(i);
49  			String className = element.getClassName();
50  			String textContent = element.getTextContent();
51  			log.debug("className: {}, textContent: {}.", className, textContent);
52  			switch (className) {
53  			case "user-info-level pull-left":
54  				loginResult.setLevel(textContent.trim());
55  				break;
56  			}
57  		}
58  
59  		NodeList bNodeList = document.getElementsByTagName("b");
60  		for (int i = 0; i < bNodeList.getLength(); i++) {
61  			HTMLElement element = (HTMLElement) bNodeList.item(i);
62  			String className = element.getClassName();
63  			String textContent = element.getTextContent();
64  			log.debug("className: {}, textContent: {}.", className, textContent);
65  			switch (className) {
66  			case "user_total":
67  				if (!total) {
68  					funds.setTotal(new BigDecimal(textContent));
69  					total = true;
70  				}
71  				break;
72  			case "user_net_asset":
73  				if (!netAsset) {
74  					funds.setNetAsset(new BigDecimal(textContent));
75  					netAsset = true;
76  				}
77  				break;
78  			case "user_available_cny":
79  				if (!availableCny) {
80  					funds.setAvailableCny(new BigDecimal(textContent));
81  					availableCny = true;
82  				}
83  				break;
84  			case "user_frozen_cny":
85  				if (!frozenCny) {
86  					funds.setFrozenCny(new BigDecimal(textContent));
87  					frozenCny = true;
88  				}
89  				break;
90  			case "user_available_btc":
91  				if (!availableBtc) {
92  					funds.setAvailableBtc(new BigDecimal(textContent));
93  					availableBtc = true;
94  				}
95  				break;
96  			case "user_frozen_btc":
97  				if (!frozenBtc) {
98  					funds.setFrozenBtc(new BigDecimal(textContent));
99  					frozenBtc = true;
100 				}
101 				break;
102 			case "user_available_ltc":
103 				if (!availableLtc) {
104 					funds.setAvailableLtc(new BigDecimal(textContent));
105 					availableLtc = true;
106 				}
107 				break;
108 			case "user_frozen_ltc":
109 				if (!frozenLtc) {
110 					funds.setFrozenLtc(new BigDecimal(textContent));
111 					frozenLtc = true;
112 				}
113 				break;
114 			case "user_loan_cny":
115 				if (!loanCny) {
116 					funds.setLoanCny(new BigDecimal(textContent));
117 					loanCny = true;
118 				}
119 				break;
120 			case "user_loan_btc":
121 				if (!loanBtc) {
122 					funds.setLoanBtc(new BigDecimal(textContent));
123 					loanBtc = true;
124 				}
125 				break;
126 			case "user_loan_ltc":
127 				if (!loanLtc) {
128 					funds.setLoanLtc(new BigDecimal(textContent));
129 					loanLtc = true;
130 				}
131 				break;
132 			default:
133 				break;
134 			}
135 		}
136 
137 		return loginResult;
138 	}
139 
140 }