Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

WangScaler: A writer with heart.

Declaration: uneducated, if there is a mistake, kindly correct.

The event came to my building to explore the lottery, lottery address. We have won the skyscraper prize and this post is for the purpose of fair drawing. If you think there is any unfair behavior in the drawing process, please comment on this post and propose a fair solution.

Analysis of the

If we want to get data on building users, we’re going to get it from the gold mining interface, First of all, we found that interface https://api.juejin.cn/interact_api/v1/comment/list?aid=2608&uuid=6950557314831795745 we found that the parameters of the need to pass the following

{
    "item_id": "7017671938038333471"."item_type": 4."cursor": "0"."limit": 20."sort": 0."client_type": 2608
}
Copy the code

Through analysis, it is found that cursor is the number of pages, and limit is the number of queries on the current page. After testing, the maximum number of queries supported by limit is 50. By responding to the data

You can see that there are 274, which means we have to ask multiple times to get all the data. Has_more determines whether to obtain all data.

Get all data

Through the above analysis, the code is as follows:

String url = "https://api.juejin.cn/interact_api/v1/comment/list?aid=2608&uuid=6950557314831795745";
HashMap<String, Object> map = new HashMap<String, Object>(6);
map.put("item_id"."7017671938038333471");
map.put("item_type".4);
map.put("cursor"."0");
map.put("limit".50);
map.put("sort".0);
map.put("client_type".2608);
Boolean condition = Boolean.TRUE;
do {
    System.out.println("The current cursor." + map.get("cursor"));
    ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, map, String.class);
    if (responseEntity.getStatusCode().value() == 200) {
        LuckDto data = JSONObject.parseObject(responseEntity.getBody(), LuckDto.class);
        condition = data.getHas_more();
        map.put("cursor", data.getCursor());
        List<JSONObject> ReplyList = data.getData();
        for (int i = 0; i < ReplyList.size(); i++) {
            if (ReplyList.get(i).get("is_author").equals(Boolean.FALSE)) {
                JSONObject userInfo = (JSONObject) ReplyList.get(i).get("user_info");
                redisTemplate.boundSetOps("luckdraw").add(userInfo.getString("user_id")); }}}}while (condition);
Copy the code

Because the members of Redis’s set collection are unique, multiple comments from the same user can be excluded. Print system.out. println(” current cursor:” + map.get(“cursor”)); We can determine whether the number of requests, and the parameters of each request, meet our expectations.

User data

In the end, Redis obtained 262 pieces of user data. We entered these user ids into a lottery.

Lucky draw

Take the ready – made online lottery software, this lottery address, we commonly used to ensure the reliability of the results of the lottery.

Original number of draw 262, draw three times, the final number of remaining draw 262-3=259.

Get the nickname of the winner

if (userInfo.getString("user_id").equals("4441682709316647") || userInfo.getString("user_id").equals("2277843826120318") || userInfo.getString("user_id").equals("3403743732709197")) {
    System.out.println(userInfo.getString("user_name"));
}
Copy the code

The last

Congratulations to the above three friends.

  • 1, smars1990
  • 2, zangeci
  • 3. Magic Lemon

Due to the questionnaire submission on Wednesday night, this post will be published until midnight on Wednesday, and there is no doubt that the three friends will get the official badges. If in doubt, re-draw.

Come all come, click “like” and then go!

Follow WangScaler and wish you a promotion, a raise and no bucket!