yujian
2019-07-29 a1175313094799efcdbbecf2840a90350d3159a7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.yeshi.fanli.util;
 
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
 
import javax.annotation.Resource;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.springframework.stereotype.Component;
 
import com.yeshi.fanli.entity.bus.user.ShamUser;
import com.yeshi.fanli.entity.bus.user.UserInfo;
import com.yeshi.fanli.service.inter.user.ShamUserService;
import com.yeshi.fanli.service.inter.user.UserInfoService;
 
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
@Component
public class ShamHongBaoUtil {
 
    @Resource
    private ShamUserService shamUserService;
 
    @Resource
    private UserInfoService userInfoService;
 
    public void addShamUserInfo() {
        List<ShamUser> list = shamUserService.getAll();
        for (ShamUser shamUser : list) {
            UserInfo form = new UserInfo();
            form.setNickName(shamUser.getName());
            form.setPortrait(shamUser.getPicUrl());
            form.setLoginType(-1);
            form.setOpenid(UUID.randomUUID().toString().replaceAll("-", "") + "-1");
            userInfoService.addUser(form, "23649898");
        }
    }
 
    public void deleteShamUser(String path) {
        try {
            Workbook workbook = Workbook.getWorkbook(new File(path));
            Sheet sheet = workbook.getSheet(0);
            for (int ii = 0; ii < sheet.getRows(); ii++) {
                Cell cell = sheet.getCell(0, ii);
                if (cell.getContents() == null || "".equals(cell.getContents().trim())) {
                    continue;
                }
                shamUserService.deleteByPrimaryKey(Long.parseLong(cell.getContents()));
            }
        } catch (BiffException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
 
    }
 
    public void addShamUser(String pid) throws Exception {
        HttpClient client = new HttpClient();
        HttpMethod method = getPostMethod(pid);
        client.executeMethod(method);
        java.lang.System.out.println(method.getStatusLine()); // 打印结果页面
        String response = new String(method.getResponseBodyAsString().getBytes("UTF-8"));
        // 打印返回的信息
        method.releaseConnection();
        List<ShamUser> list = parseQQresult(response);
        for (ShamUser shamUser : list) {
            shamUserService.addShamUser(shamUser);
        }
    }
 
    private static List<ShamUser> parseQQresult(String response) throws Exception {
        JSONObject json = JSONObject.fromObject(response);
        List<ShamUser> list = new ArrayList<ShamUser>();
        String code = String.valueOf(json.opt("retcode"));
        if ("0".equals(code)) {
            JSONArray jsonArray = json.optJSONObject("result").optJSONObject("buddy").optJSONArray("info_list");
            ShamUser user = null;
            String patterns = "[^\u4e00-\u9fa5\\w\\s]+";
            if (jsonArray == null) {
                return list;
            }
            for (Object object : jsonArray) {
                JSONObject data = (JSONObject) object;
                String name = (String) data.opt("nick");
                name = name.replaceAll(patterns, "").replaceAll(" ", "");
                if (name.length() < 2) {
                    continue;
                }
                String picUrl = (String) data.opt("url");
                user = new ShamUser();
                user.setName(new String(name.getBytes(), "utf-8"));
                if (picUrl.contains("pub.idqqimg.com")) {
                    picUrl = "https://gw.alicdn.com/tps/i3/TB1yeWeIFXXXXX5XFXXuAZJYXXX-210-210.png_160x160.jpg";
                }
                user.setPicUrl(picUrl);
                list.add(user);
            }
        }
        return list;
    }
 
    private static HttpMethod getPostMethod(String pid) {
        PostMethod post = new PostMethod("http://cgi.find.qq.com/qqfind/buddy/search_v3"); // recommendDetails
                                                                                            // voteGoods
                                                                                            // recommendReplys
                                                                                            // makePublic
                                                                                            // replys
        NameValuePair num = new NameValuePair("num", "100");
        NameValuePair page = new NameValuePair("page", "0");
        NameValuePair sessionid = new NameValuePair("sessionid", "0");
        NameValuePair agerg = new NameValuePair("agerg", "13");
        NameValuePair sex = new NameValuePair("sex", "0");
        NameValuePair firston = new NameValuePair("firston", "0");
        NameValuePair video = new NameValuePair("video", "0");
        NameValuePair country = new NameValuePair("country", "1");
        NameValuePair province = new NameValuePair("province", pid);
        NameValuePair city = new NameValuePair("city", "0");
        NameValuePair district = new NameValuePair("district", "0");
        NameValuePair hcountry = new NameValuePair("hcountry", "1");
        NameValuePair hprovince = new NameValuePair("hprovince", "0");
        NameValuePair hcity = new NameValuePair("hcity", "0");
        NameValuePair hdistrict = new NameValuePair("hdistrict", "0");
        NameValuePair online = new NameValuePair("online", "0");
        NameValuePair ldw = new NameValuePair("ldw", "814539287");
        post.setRequestHeader("Cookie",
                "RK=7dVSTqdaSf; pt2gguin=o0424539852; ptisp=ctc; ptcz=555762bb1a6cef860451fe47677c82eaf80a1388eee045b8cf2d546ed1c96534; _qpsvr_localtk=tk70; pgv_info=ssid=s852802188; pgv_pvid=9584824304; uin=o424539852; skey=Z9PWyiIOuI; itkn=2002088896");
        post.setRequestBody(new NameValuePair[] { num, page, sessionid, agerg, sex, firston, video, country, province,
                city, district, hcountry, hprovince, hcity, hdistrict, online, ldw });
        return post;
    }
 
}