从Google Play获取versionCode和VersionName

10
我正在寻找一种通过 Java 应用程序在 PC 上使用包名从 Google Play 获取应用程序 versionCode 和 VersionName 的方法。 我看过:https://androidquery.appspot.com/,但它已经不再起作用了,还有https://code.google.com/archive/p/android-market-api/,但开始出现问题并停止工作,而且需要设备 ID。 你能帮我提供一些简单的解决方案或 API 吗? 非常重要的是,我需要 versionCode 和 VersionName,而 VersionName 相对容易通过解析 Google Play 应用站点的 HTML 来获取,而 versionCode 非常重要。

你想要这个应用程序吗?还是在Play商店上的任何应用程序? - Devrim
对于我目前所在公司的应用程序,每个应用程序都使用不同的密钥进行签名。因此,这就像是 Play Store 上的任何应用程序一样。 - Dim
4个回答

14

Google Play没有官方API,Playstore使用的是未公开文档和不对外开放的内部protobuf API。我认为你可以:

  • 使用反向工程API的开源库
  • 爬取已经提取了这些信息(很可能是通过相同的protobuf Google Play API)的apk下载网站

请注意,有一个Google Play开发者API,但你不能列出你的apk、版本、应用程序。它主要用于管理应用程序分发、评论、编辑等。

Google Play内部API

play-store-api Java库

这个库使用Google Play Store protobuf API(未公开文档和封闭API),需要一个电子邮件/密码来生成一个可以重复使用的令牌来操作API:

GplaySearch googlePlayInstance = new GplaySearch();

DetailsResponse response = googlePlayInstance.getDetailResponse("user@gmail.com",
        "password", "com.facebook.katana");

AppDetails appDetails = response.getDocV2().getDetails().getAppDetails();

System.out.println("version name : " + appDetails.getVersionString());
System.out.println("version code : " + appDetails.getVersionCode());

使用这种方法:
public DetailsResponse getDetailResponse(String email,
                                         String password,
                                         String packageName) throws IOException, ApiBuilderException {
    // A device definition is required to log in
    // See resources for a list of available devices
    Properties properties = new Properties();
    try {
        properties.load(getClass().getClassLoader().getSystemResourceAsStream("device-honami" +
                ".properties"));
    } catch (IOException e) {
        System.out.println("device-honami.properties not found");
        return null;
    }
    PropertiesDeviceInfoProvider deviceInfoProvider = new PropertiesDeviceInfoProvider();
    deviceInfoProvider.setProperties(properties);
    deviceInfoProvider.setLocaleString(Locale.ENGLISH.toString());

    // Provide valid google account info
    PlayStoreApiBuilder builder = new PlayStoreApiBuilder()
            .setDeviceInfoProvider(deviceInfoProvider)
            .setHttpClient(new OkHttpClientAdapter())
            .setEmail(email)
            .setPassword(password);
    GooglePlayAPI api = builder.build();

    // We are logged in now
    // Save and reuse the generated auth token and gsf id,
    // unless you want to get banned for frequent relogins
    api.getToken();
    api.getGsfId();

    // API wrapper instance is ready
    return api.details(packageName);
}

device-honami.properties是设备属性文件,用于识别设备特性。你可以在这里找到一些device.properties文件示例。

OkHttpClientAdapter可以在这里找到。

运行此示例所使用的依赖项:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}
dependencies {
    compile 'com.github.yeriomin:play-store-api:0.19'
    compile 'com.squareup.okhttp3:okhttp:3.8.1'
}

爬取第三方apk下载站点

http://apk-dl.com

您可以通过使用jsoup来爬取需要的软件包名称,从而在(当然是非官方的)http://apk-dl.com上获取版本名称和版本代码:

String packageName = "com.facebook.katana";

Document doc = Jsoup.connect("http://apk-dl.com/" + packageName).get();
Elements data = doc.select(".file-list .mdl-menu__item");

if (data.size() > 0) {
    System.out.println("full text : " + data.get(0).text());
    Pattern pattern = Pattern.compile("(.*)\\s+\\((\\d+)\\)");
    Matcher matcher = pattern.matcher(data.get(0).text());
    if (matcher.find()) {
        System.out.println("version name : " + matcher.group(1));
        System.out.println("version code : " + matcher.group(2));
    }
}

https://apkpure.com

另一个可能性是抓取https://apkpure.com的内容:

String packageName = "com.facebook.katana";

Elements data = Jsoup.connect("https://apkpure.com/search?q=" + packageName)
        .userAgent("Mozilla")
        .get().select(".search-dl .search-title a");

if (data.size() > 0) {

    Elements data2 = Jsoup.connect("https://apkpure.com" + data.attr("href"))
            .userAgent("Mozilla")
            .get().select(".faq_cat dd p");

    if (data2.size() > 0) {
        System.out.println(data2.get(0).text());

        Pattern pattern = Pattern.compile("Version:\\s+(.*)\\s+\\((\\d+)\\)");
        Matcher matcher = pattern.matcher(data2.get(0).text());
        if (matcher.find()) {
            System.out.println("version name : " + matcher.group(1));
            System.out.println("version code : " + matcher.group(2));
        }
    }
}

https://api-apk.evozi.com

此外,https://api-apk.evozi.com 还有一个内部的 JSON API,但:

  • 有时候它不起作用(返回 Ops, APK Downloader got access denied when trying to download),主要是针对不受欢迎的应用程序
  • 它有防止爬虫机器人的机制(使用随机变量名生成的 JS 随机令牌)

以下内容将使用 https://api-apk.evozi.com 返回版本名称和代码:

String packageName = "com.facebook.katana";

String data = Jsoup.connect("https://apps.evozi.com/apk-downloader")
        .userAgent("Mozilla")
        .execute().body();

String token = "";
String time = "";

Pattern varPattern = Pattern.compile("dedbadfbadc:\\s+(\\w+),");
Pattern timePattern = Pattern.compile("t:\\s+(\\w+),");

Matcher varMatch = varPattern.matcher(data);
Matcher timeMatch = timePattern.matcher(data);

if (varMatch.find()) {
    Pattern tokenPattern = Pattern.compile("\\s*var\\s*" + varMatch.group(1) + "\\s*=\\s*'(.*)'.*");
    Matcher tokenMatch = tokenPattern.matcher(data);

    if (tokenMatch.find()) {
        token = tokenMatch.group(1);
    }
}

if (timeMatch.find()) {
    time = timeMatch.group(1);
}

HttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost("https://api-apk.evozi.com/download");

List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("t", time));
params.add(new BasicNameValuePair("afedcfdcbdedcafe", packageName));
params.add(new BasicNameValuePair("dedbadfbadc", token));
params.add(new BasicNameValuePair("fetch", "false"));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

HttpResponse response = httpclient.execute(httppost);

JsonElement element = new JsonParser().parse(EntityUtils.toString(response.getEntity()));
JsonObject result = element.getAsJsonObject();

if (result.has("version") && result.has("version_code")) {
    System.out.println("version name : " + result.get("version").getAsString());
    System.out.println("version code : " + result.get("version_code").getAsInt());
} else {
    System.out.println(result);
}

实现

您可以在与Java应用程序直接通信的后端上实现它,这样您就可以维护检索版本代码/名称的过程,以防上述任何一种方法失败。

如果您只对自己的应用程序感兴趣,更清晰的解决方案是:

  • 设置一个后端来存储所有当前应用程序版本名称/版本代码
  • 您公司中的所有开发人员/发布者都可以共享一个发布任务(gradle task),该任务将使用Google Play开发者API来发布apk,并且gradle任务将包括调用您的后端以在应用程序发布时存储版本代码/版本名称条目。主要目标是自动化整个发布过程并在您这一侧存储应用程序元数据。

非常感谢您的回复,但是有没有不使用第三方网站的方法呢?或者您知道他们是如何做到的吗? - Dim
非常感谢您的帮助,第一个解决方案完全符合我们的需求。再次感谢! - Dim

0

除了使用JSoup,我们还可以通过模式匹配获取PlayStore中的应用版本。

为了匹配Google Play Store最新的模式,即 <div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div> 我们首先必须匹配上述节点序列,然后从上述序列中获取版本值。以下是同样的代码片段:

    private String getAppVersion(String patternString, String inputString) {
        try{
            //Create a pattern
            Pattern pattern = Pattern.compile(patternString);
            if (null == pattern) {
                return null;
            }

            //Match the pattern string in provided string
            Matcher matcher = pattern.matcher(inputString);
            if (null != matcher && matcher.find()) {
                return matcher.group(1);
            }

        }catch (PatternSyntaxException ex) {

            ex.printStackTrace();
        }

        return null;
    }


    private String getPlayStoreAppVersion(String appUrlString) {
        final String currentVersion_PatternSeq = "<div[^>]*?>Current\\sVersion</div><span[^>]*?>(.*?)><div[^>]*?>(.*?)><span[^>]*?>(.*?)</span>";
        final String appVersion_PatternSeq = "htlgb\">([^<]*)</s";
        String playStoreAppVersion = null;

        BufferedReader inReader = null;
        URLConnection uc = null;
        StringBuilder urlData = new StringBuilder();

        final URL url = new URL(appUrlString);
        uc = url.openConnection();
        if(uc == null) {
           return null;
        }
        uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; WindowsNT 5.1; en-US; rv1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
        inReader = new BufferedReader(new InputStreamReader(uc.getInputStream()));
        if (null != inReader) {
            String str = "";
            while ((str = inReader.readLine()) != null) {
                           urlData.append(str);
            }
        }

        // Get the current version pattern sequence 
        String versionString = getAppVersion (currentVersion_PatternSeq, urlData.toString());
        if(null == versionString){ 
            return null;
        }else{
            // get version from "htlgb">X.X.X</span>
            playStoreAppVersion = getAppVersion (appVersion_PatternSeq, versionString);
        }

        return playStoreAppVersion;
    }

我通过这个解决了问题。希望能帮到你。


0

Jsoup太慢了,效率低下。如果想要一种简短易用的方式,可以使用模式匹配:

public class PlayStoreVersionChecker {

public String playStoreVersion = "0.0.0";

OkHttpClient client = new OkHttpClient();

private String execute(String url) throws IOException {
    okhttp3.Request request = new Request.Builder()
            .url(url)
            .build();

    Response response = client.newCall(request).execute();
    return response.body().string();
}

public String getPlayStoreVersion() {
    try {
        String html = execute("https://play.google.com/store/apps/details?id=" + APPIDHERE!!! + "&hl=en");
        Pattern blockPattern = Pattern.compile("Current Version.*([0-9]+\\.[0-9]+\\.[0-9]+)</span>");
        Matcher blockMatch = blockPattern.matcher(html);
        if(blockMatch.find()) {
            Pattern versionPattern = Pattern.compile("[0-9]+\\.[0-9]+\\.[0-9]+");
            Matcher versionMatch = versionPattern.matcher(blockMatch.group(0));
            if(versionMatch.find()) {
                playStoreVersion = versionMatch.group(0);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return playStoreVersion;
}


}

'okhttp3'是什么? - Debora Carnevali
@DeboraCarnevali okhttp3是一个用于Android的HTTP请求库,非常简单易用。 - Leandro
我应该写什么作为APPID?这是应用程序的包名吗? - H Hooshyar

-1
public class Store {

    private Document document;
    private final static String baseURL = "https://play.google.com/store/apps/details?id=";

    public static void main(String[] args) {

    }

    public Store(String packageName) {
        try {
            document = Jsoup.connect(baseURL + packageName).userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0").get();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    public String getTitle() {
        return document.select("h1.AHFaub > span").text();
    }

    public String getDeveloper() {
        return document.selectFirst("span.UAO9ie > a").text();
    }

    public String getCategory() {
        Elements elements = document.select("span.UAO9ie > a");
        for (Element element : elements) {
            if (element.hasAttr("itemprop")) {
                return element.text();
            }
        }
        return null;
    }

    public String getIcon() {
        return document.select("div.xSyT2c > img").attr("src");
    }

    public String getBigIcon() {
        return document.select("div.xSyT2c > img").attr("srcset").replace(" 2x", "");
    }

    public List<String> getScreenshots() {
        List<String> screenshots = new ArrayList<>();
        Elements img = document.select("div.u3EI9e").select("button.Q4vdJd").select("img");
        for (Element src : img) {
            if (src.hasAttr("data-src")) {
                screenshots.add(src.attr("data-src"));
            } else {
                screenshots.add(src.attr("src"));
            }
        }
        return screenshots;
    }

    public List<String> getBigScreenshots() {
        List<String> screenshots = new ArrayList<>();
        Elements img = document.select("div.u3EI9e").select("button.Q4vdJd").select("img");
        for (Element src : img) {
            if (src.hasAttr("data-src")) {
                screenshots.add(src.attr("data-srcset").replace(" 2x", ""));
            } else {
                screenshots.add(src.attr("srcset").replace(" 2x", ""));
            }
        }
        return screenshots;
    }

    public String getDescription() {
        return document.select("div.DWPxHb > span").text();
    }

    public String getRatings() {
        return document.select("div.BHMmbe").text();
    }
}

导入

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

此脚本将返回以下内容:
分类(例如个性化) 开发者名称 应用图标 应用名称 屏幕截图(缩略图和完整预览) 描述
您也可以在此处查看完整源代码。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接