背景

在谷歌上面发布包,有时候要看看评分,有时候会因为总总原因被下架,希望后台能够对评分进行预警,和下架预警

实现

测试地址: https://play.google.com/store/apps/details?id=com.tencent.mm 通过jsoup解析页面,然后获取评分; 这是获取评分的: image.png

而判断包是否下架就直接判断返回的code码是否大于300,就算作下架了;

是否下架预警

public static void offline(String url) {
        // 1.解析网页(jsoup 解析返回的对象是浏览器Document对象)
        HttpResponse response = null;
        try {
            response = getHttpResponse(url);

            if (response.getStatusLine().getStatusCode() >= 300) {
                // 下架通知
                log.error("谷歌App检测下架: {} ", url);
            }
            log.error("谷歌App检测下架: code码{} ", response.getStatusLine().getStatusCode());
        } catch (Exception e) {
            log.error("谷歌App检测下架!!!url:{},异常:{}", url, e);
            //throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(response)) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    //throw new RuntimeException(e);
                }
            }
        }
    }

评分

public static Integer score(String url) {
        // 1.解析网页(jsoup 解析返回的对象是浏览器Document对象)
        HttpResponse response = null;
        try {
            response = getHttpResponse(url);
            if (response.getStatusLine().getStatusCode() < 300) {
                Document document = Jsoup.parse(EntityUtils.toString(response.getEntity(), Charsets.UTF_8));
                // google商店的评分class
                Elements tt9eCd = document.getElementsByClass("TT9eCd");
                if (CollectionUtils.isEmpty(tt9eCd)) {
                    log.debug("google商店评分数据监控没有评分app:{}", url);
                    return null;
                }
                return (int) (Double.parseDouble(tt9eCd.get(0).textNodes().get(0).text()) * 10);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("google商店评分数据监控异常!!!url:{},异常:{}", url, e.toString());
            //throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(response)) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    //throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

总的工具类,测试

package com.study.springbootplus.util;

import com.google.common.base.Charsets;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.Objects;

/**
 * @ClassName GooglePlayUtil
 * @Author yida
 * @Date 2023-08-21 17:04
 * @Description GooglePlayUtil
 */
@Slf4j
public class GooglePlayUtil {

    private static final HttpClient httpClient = HttpClients.custom()
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setConnectTimeout(3000)
                    .setSocketTimeout(3000).build())
            .build();

    public static void main(String[] args) {
        System.setProperty("java.net.useSystemProxies", "true");

        offline("https://play.google.com/store/apps/details?id=com.tencent.mm");
        System.out.println("返回的分数:" + score("https://play.google.com/store/apps/details?id=com.tencent.mm"));
    }

    public static void offline(String url) {
        // 1.解析网页(jsoup 解析返回的对象是浏览器Document对象)
        HttpResponse response = null;
        try {
            response = getHttpResponse(url);

            if (response.getStatusLine().getStatusCode() >= 300) {
                // 下架通知
                log.error("谷歌App检测下架: {} ", url);
            }
            log.error("谷歌App检测下架: code码{} ", response.getStatusLine().getStatusCode());
        } catch (Exception e) {
            log.error("谷歌App检测下架!!!url:{},异常:{}", url, e);
            //throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(response)) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    //throw new RuntimeException(e);
                }
            }
        }
    }

    public static Integer score(String url) {
        // 1.解析网页(jsoup 解析返回的对象是浏览器Document对象)
        HttpResponse response = null;
        try {
            response = getHttpResponse(url);
            if (response.getStatusLine().getStatusCode() < 300) {
                Document document = Jsoup.parse(EntityUtils.toString(response.getEntity(), Charsets.UTF_8));
                // google商店的评分class
                Elements tt9eCd = document.getElementsByClass("TT9eCd");
                if (CollectionUtils.isEmpty(tt9eCd)) {
                    log.debug("google商店评分数据监控没有评分app:{}", url);
                    return null;
                }
                return (int) (Double.parseDouble(tt9eCd.get(0).textNodes().get(0).text()) * 10);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("google商店评分数据监控异常!!!url:{},异常:{}", url, e.toString());
            //throw new RuntimeException(e);
        } finally {
            if (Objects.nonNull(response)) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                    //throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    public static HttpResponse getHttpResponse(String url) throws Exception {
        return httpClient.execute(new HttpGet(url));
    }

}

测试结果: 返回的分数:36