PageRenderTime 88ms CodeModel.GetById 23ms RepoModel.GetById 4ms app.codeStats 0ms

/code/GPAssist/src/main/java/org/tomstools/html/fetcher/AgencyDealDataFetcherApp.java

https://github.com/lotomer/tomstools
Java | 201 lines | 147 code | 17 blank | 37 comment | 44 complexity | 839e34c2e13bde9e469107b153c2a622 MD5 | raw file
  1. /**
  2. * copyright (a) 2010-2012 tomstools.org. All rights reserved.
  3. */
  4. package org.tomstools.html.fetcher;
  5. import java.net.MalformedURLException;
  6. import java.util.Calendar;
  7. import org.tomstools.common.log.Logger;
  8. import org.tomstools.common.util.Utils;
  9. import org.tomstools.html.data.AgencyDeal;
  10. import org.tomstools.html.data.AgencyDealDAO;
  11. import com.alibaba.fastjson.JSONArray;
  12. import com.alibaba.fastjson.JSONObject;
  13. /**
  14. * 机构交易数据获取
  15. *
  16. * @author lotomer
  17. * @date 2012-6-11
  18. * @time 下午02:09:32
  19. */
  20. public class AgencyDealDataFetcherApp {
  21. private static final Logger logger = Logger.getLogger(AgencyDealDataFetcherApp.class);
  22. /**
  23. * 获取指定机构指定日期的交易数据
  24. *
  25. * @param agencySymbol 机构标识
  26. * @param date 指定日期
  27. */
  28. public void fetchAgencyDealDataByDate(String agencySymbol, String date) {
  29. if (!Utils.isEmpty(date)) {
  30. fetchAgencyDealData(agencySymbol, Integer.valueOf(date.substring(5, 7)), date);
  31. } else {
  32. logger.warn("date cannot be empty!");
  33. }
  34. }
  35. /**
  36. * 获取指定机构指定月份的交易数据
  37. *
  38. * @param agencySymbol 结构标识
  39. * @param month 指定月份
  40. */
  41. public void fetchAgencyDealDataByMonth(String agencySymbol, String month) {
  42. if (!Utils.isEmpty(month)) {
  43. // 指定了月份,则使用月份
  44. fetchAgencyDealData(agencySymbol, Integer.valueOf(month), null);
  45. } else {
  46. // 没有指定月份,则取今年所有月份
  47. int currMonth = Calendar.getInstance().get(Calendar.MONTH) + 1;
  48. for (int i = 1; i <= currMonth; i++) {
  49. fetchAgencyDealData(agencySymbol, i, null);
  50. }
  51. }
  52. }
  53. private AgencyDealDAO agencyDeal;
  54. private HTMLFetcher fetcher;
  55. public AgencyDealDataFetcherApp() {
  56. fetcher = new HTMLFetcher();
  57. }
  58. public AgencyDealDataFetcherApp(String proxyHost, int proxyPort, String proxyScheme) {
  59. super();
  60. if (Utils.isEmpty(proxyHost)) {
  61. fetcher = new HTMLFetcher();
  62. } else {
  63. fetcher = new HTMLFetcher(proxyHost, proxyPort, proxyScheme);
  64. }
  65. }
  66. /**
  67. * 获取指定机构指定日期的交易数据
  68. *
  69. * @param agencySymbol 机构标识,不能为null或空字符串
  70. * @param month 月份,1-12,不能为null
  71. * @param date 日期,可以为null
  72. */
  73. public void fetchAgencyDealData(String agencySymbol, int month, String date) {
  74. if (Utils.isEmpty(agencySymbol)) {
  75. logger.warn("The agencySymbol cannot be null or empty!");
  76. return;
  77. }
  78. // 第三步 查询主力的交易明细
  79. // http://quotes.money.163.com/hs/marketdata/service/jglhb.php?host=/hs/marketdata/service/jglhb.php&page=0&query=agencysymbol:80138252;date:8&fields=NO,SYMBOL,SNAME,TDATE,SMEBTCOMPANY4,SMEBTCOMPANY5,SMEBTCOMPANY1&sort=TDATE&order=desc&count=25&type=query&req=11526
  80. String urlStep3 = "http://quotes.money.163.com/hs/marketdata/service/jglhb.php?host=/hs/marketdata/service/jglhb.php&page=0&"
  81. + "query=agencysymbol:%s;date:%s&fields=NO,SYMBOL,SNAME,TDATE,SMEBTCOMPANY4,SMEBTCOMPANY5,SMEBTCOMPANY1&sort=TDATE&order=desc&count=2500&type=query&req=11526";
  82. String htmlContent = fetcher.fetchHTMLContent(String.format(urlStep3, agencySymbol, month));
  83. // System.out.println(htmlContent);
  84. if (Utils.isEmpty(htmlContent)) {
  85. logger.info("no content!");
  86. return;
  87. }
  88. JSONObject obj = (JSONObject) JSONObject.parse(htmlContent);
  89. logger.info("total:" + obj.get("total"));
  90. JSONArray arr = obj.getJSONArray("list");
  91. agencyDeal = new AgencyDealDAO();
  92. for (int i = 0; i < arr.size(); ++i) {
  93. JSONObject o = (JSONObject) arr.get(i);
  94. if (!Utils.isEmpty(date)) {
  95. // 指定了日期,则只获取该日期的数据
  96. if (date.equals(o.get("TDATE"))) {
  97. agencyDeal.add(new AgencyDeal(agencySymbol, o.get("SYMBOL").toString(), o.get(
  98. "SNAME").toString(), o.get("TDATE").toString(), o.get("SMEBTCOMPANY4")
  99. .toString(), o.get("SMEBTCOMPANY5").toString()));
  100. }
  101. } else {
  102. agencyDeal.add(new AgencyDeal(agencySymbol, o.get("SYMBOL").toString(), o.get(
  103. "SNAME").toString(), o.get("TDATE").toString(), o.get("SMEBTCOMPANY4")
  104. .toString(), o.get("SMEBTCOMPANY5").toString()));
  105. }
  106. }
  107. agencyDeal.save();
  108. }
  109. private static void printHelp() {
  110. System.out.println("Usage: AgencyDealDataFetcherApp [options] agencySymbol");
  111. System.out.println("Options are:");
  112. System.out.println(" -m month 1 to 12.");
  113. System.out.println(" -d date yyyy-MM-dd. Like 2013-09-01.");
  114. System.out.println(" -host proxyHost The proxy host.");
  115. System.out.println(" -port proxyPort The proxy port. Default is 8087");
  116. System.out.println(" -scheme proxyScheme The proxy scheme. Default is http.");
  117. System.out.println(" -h help Print the help message.");
  118. }
  119. public static void main(String[] args) throws MalformedURLException {
  120. String agencySymbol = null;// "80138252";
  121. String month = null;// "9";
  122. String date = null;// "2013-09-06";
  123. String proxyHost = null;// "127.0.0.1";
  124. int proxyPort = 8087;
  125. String proxyScheme = "http";
  126. for (int i = 0; i < args.length; ++i) {
  127. if ("-m".equals(args[i])) {
  128. if (++i == args.length) {
  129. System.err.println("-m month. 1 to 12");
  130. System.exit(-1);
  131. } else {
  132. month = args[i];
  133. }
  134. } else if ("-d".equals(args[i])) {
  135. if (++i == args.length) {
  136. System.err.println("-d date. yyyy-MM-dd. Like 2013-09-01");
  137. System.exit(-1);
  138. } else {
  139. date = args[i];
  140. }
  141. } else if ("-host".equals(args[i])) {
  142. if (++i == args.length) {
  143. System.err.println("-host proxyHost. ");
  144. System.exit(-1);
  145. } else {
  146. proxyHost = args[i];
  147. }
  148. } else if ("-port".equals(args[i])) {
  149. if (++i == args.length) {
  150. System.err.println("-port proxyPort. Default is 8087");
  151. System.exit(-1);
  152. } else {
  153. proxyPort = Integer.valueOf(args[i]);
  154. }
  155. } else if ("-scheme".equals(args[i])) {
  156. if (++i == args.length) {
  157. System.err.println("-scheme proxyScheme. Default is http");
  158. System.exit(-1);
  159. } else {
  160. proxyScheme = args[i];
  161. }
  162. } else if ("-h".equals(args[i])) {
  163. printHelp();
  164. } else {
  165. agencySymbol = args[i];
  166. }
  167. }
  168. if (Utils.isEmpty(agencySymbol)) {
  169. System.err.println("The agencySymbol cannot be empty!");
  170. System.exit(-1);
  171. }
  172. AgencyDealDataFetcherApp fetcher = new AgencyDealDataFetcherApp(proxyHost, proxyPort,
  173. proxyScheme);
  174. long start = System.currentTimeMillis();
  175. if (!Utils.isEmpty(date)) {
  176. // 指定的具体的日期
  177. fetcher.fetchAgencyDealDataByDate(agencySymbol, date);
  178. } else {
  179. // 使用月份
  180. fetcher.fetchAgencyDealDataByMonth(agencySymbol, month);
  181. }
  182. System.out.println("Total cost: " + (System.currentTimeMillis() - start) + "ms.");
  183. }
  184. }