package cputils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jsoup.ConnectionManager;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import common.Combination;
import common.Utils;
public class CPHMACService {
private static final Logger logger = Logger.getLogger(CPHMACService.class);
// private static DateFormat format = new SimpleDateFormat("MM-dd HH:mm:ss");
public static void main(String[] args) throws IOException {
CPHMACService service = new CPHMACService();
service.writeAicaiHMData("2014043");
// writeAicaiSSQHMData("2014025");
// service.writeTaobaoHMData("SSQ","2014028",false);
/*for (int i = 2014020; i <= 2014028; i++) {
Thread t = new Thread(new ServiceThread("SSQ",i+"",false));
t.start();
}*/
//System.out.println(service.tjHM4Aicai("14032"));
}
public Map<String, Map<Integer, Integer>> tjHM4Aicai(String issueNo) throws IOException{
Map<String, Map<Integer, Integer>> res = new HashMap<String, Map<Integer,Integer>>();
List<String> list = XmlUtils.getHMData("ac_"+issueNo);
Map<Integer, Integer> bMap = new HashMap<Integer, Integer>();
Map<Integer, Integer> aMap = new HashMap<Integer, Integer>();
for (int i = 1; i <= 35; i++) {
bMap.put(i, 0);
}
for (int i = 1; i <= 16; i++) {
aMap.put(i, 0);
}
Combination comb = new Combination();
for (String s1 : list) {
List<String> items = comb.getAll(s1, issueNo);
for (String s : items) {
String[] sArr = s.split("\\:");
if (sArr!=null&&sArr.length==2) {
String[] bArr = sArr[0].split(" ");
String[] aArr = sArr[1].split(" ");
for (int i = 0; i < bArr.length; i++) {
if (StringUtils.isNotBlank(bArr[i])) {
int num = Integer.parseInt(bArr[i].trim());
bMap.put(num, bMap.get(num)+1);
}
}
for (int i = 0; i < aArr.length; i++) {
if (StringUtils.isNotBlank(aArr[i])) {
int num = Integer.parseInt(aArr[i].trim());
aMap.put(num, aMap.get(num)+1);
}
}
}
}
}
res.put("b", bMap);
res.put("a", aMap);
Utils.print(issueNo+" line:"+list.size());
res = XmlUtils.makeSortedMap(res, true);
return res;
}
/*public void writeAicaiSSQHMData(String issueNo){
writeAicaiHMData("FC_SSQ", issueNo);
}
public void writeAicaiDLTHMData(String issueNo){
writeAicaiHMData("TC_DLT", issueNo);
}*/
public void writeAicaiHMData(String issueNo){
String game = "";
switch (issueNo.length()) {
case 5:
game = "TC_DLT";
break;
case 7:
game = "FC_SSQ";
break;
}
Utils.print("****AC "+game+" "+issueNo+" HMDetailLinks begin...");
long b1 = System.currentTimeMillis();
int total = getTotalPageNum(game, issueNo);
List<String> idList = new ArrayList<String>();
for (int i = 1; i <= total; i++) {
Map<String, String> idMap = readDataAicai(game,issueNo, i);
idList.addAll(idMap.keySet());
}
Utils.print("****AC "+game+" "+issueNo+" HMDetailLinks ok..."+idList.size()+"...time:"+(System.currentTimeMillis()-b1)+"ms");
Utils.print("****AC "+game+" "+issueNo+" HMData begin...");
StringBuilder cBuilder = new StringBuilder();
int totalRec = 0;
long b3 = System.currentTimeMillis();
for(int i =0,size=idList.size();i<size;i++){
String id = idList.get(i);
List<String> list = readDataHMAicai(game,issueNo,id);
if((i+1)%300==0){
Utils.print("********AC "+game+" "+issueNo+" HMData..."+(i+1)+"("+list.size()+")->"+idList.size());
}
for (String con : list) {
totalRec++;
con = con.replaceAll(" ", "").replaceAll(",", " ").replaceAll("\\|", ":")
.replaceAll("胆", "(").replaceAll("拖", ")");
cBuilder.append(con).append("\r\n");
}
}
Utils.print("****AC "+game+" "+issueNo+" HMData ok..."+totalRec+".time:"+(System.currentTimeMillis()-b3));
Utils.print("****AC "+game+" "+issueNo+" HMData write begin...");
XmlUtils.writeConfigFile(cBuilder.toString(), "ac_"+issueNo+".txt", false);
Utils.print("****AC "+game+" "+issueNo+" finished...users:"+idList.size()+" records:"+totalRec);
}
private List<String> readDataHMAicai(String game,String issueNo,String id) {
//String url = "http://www.aicai.com/hemai-"+id+".html";
String url = "http://www.aicai.com/hm/hmdetail!queryPlanDetailContent.jhtml?"
+ "planNo="+id+"&game="+game+"&issueNo="+issueNo+"&selectType=MANUAL";
Document doc = null;
try {
doc = ConnectionManager.getInstance().getDocument(url);
} catch (Exception e) {
logger.error("connect fail..", e);
return null;
}
List<String> resList = new ArrayList<String>();
Elements div = doc.getElementsByAttributeValue("class", "facont_table");
if (div.size()>0) {
Elements tbody =div.get(0).getElementsByTag("tbody");
Elements tr1 = tbody.get(0).getElementsByAttributeValue("class", "tpleft");
for (Element e : tr1) {
//System.out.println(e.ownText());
resList.add(e.ownText());
}
}
return resList;
}
private int getTotalPageNum(String game,String issueNo){
// String url = "http://www.aicai.com/hemai/tcdlt/";
String url = "http://www.aicai.com/hm/hmlottery!hemailPlan.jhtml?&game="+game+""
+"&issueNo="+issueNo+"&groupSqlBuildType=TOP&version=2&encryptBoolean=true"
+"&hmActionPageUrl=/hm/hmlottery!hemailPlan.jhtml¤t_page=1&showPageSize=30";
Document doc = null;
try {
doc = ConnectionManager.getInstance().getDocument(url);
} catch (Exception e) {
logger.error("connect fail..", e);
return 0;
}
Element e = doc.getElementById("jq_page_total");
String num = e.attr("value");
return Integer.parseInt(num);
}
private Map<String, String> readDataAicai(String game,String issueNo,int current_page) {
String url = "http://www.aicai.com/hm/hmlottery!hemailPlan.jhtml?&game="+game+""
+"&issueNo="+issueNo+"&groupSqlBuildType=TOP&version=2&encryptBoolean=true"
+"&hmActionPageUrl=/hm/hmlottery!hemailPlan.jhtml¤t_page="+current_page+"&showPageSize=30";
Document doc = null;
try {
doc = ConnectionManager.getInstance().getDocument(url);
} catch (Exception e) {
logger.error("connect fail..", e);
return null;
}
Map<String, String> resMap = new HashMap<String, String>();
Elements elements = doc.getElementsByAttributeValue("class", "table_list");
Elements tr = elements.get(0).getElementsByTag("tr");
for (Element element : tr) {
String id = element.attr("id");
String id1 = id.substring(id.lastIndexOf("_")+1);
resMap.put(id1, id1);
}
return resMap;
}
}
class ACServiceThread implements Runnable{
private String issueNo;
public ACServiceThread(String issueNo){
this.issueNo = issueNo;
}
@Override
public void run() {
CPHMACService service = new CPHMACService();
service.writeAicaiHMData(issueNo);
}
}