从Google Play获取versionCode和VersionName

时间:2017-07-16 13:45:56

标签: java android google-play

我正在寻找一种方法如何通过PC中的Java应用程序从谷歌播放应用程序版本代码和版本名称。 我已经看到:https://androidquery.appspot.com/但它不再起作用了https://code.google.com/archive/p/android-market-api/开始出现问题而且也停止了工作,并且它征服了设备ID。 你能帮我解决一些简单的解决方案或API吗? 非常重要,我需要 versionCode和VersionName ,并且通过解析html google play app网站可以比较容易地获得VersionName。 versionCode非常重要。

4 个答案:

答案 0 :(得分:8)

没有正式的Google Play API,Playstore使用内部protobuf API,该API未记录且未打开。恕我直言,你可以:

  • 使用对API进行反向工程的开源库
  • scrap apk下载已经提取此信息的网站(最有可能来自同一个protobuf Google Play API)

请注意,有一个Google Play developer API,但您无法列出您的apks,版本,应用。它主要用于管理应用分发,评论,编辑等。

Google Play内部API

play-store-api Java library

此库使用Google Play商店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文件示例here

OkHttpClientAdapter可以找到here

用于运行此示例的依赖项:

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'
}

Scrap第三部分apk下载网站

http://apk-dl.com

您可以获取版本名称&来自http://apk-dl.com的版本代码(当然是非官方的)通过使用jsoup抓取页面来获取所需的包名称:

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 FWIW返回版本名称和代码:

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任务),该任务将使用Google Play developer API发布apk,该gradle任务将包含对后端的调用以存储版本代码/版本名称应用程序发布时的条目。主要目标是通过存储应用程序元数据来自动化整个出版物。

答案 1 :(得分:0)

除了使用JSoup之外,我们还可以进行模式匹配,以便从playStore获取应用版本。

匹配google playstore的最新模式,即      <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;
    }

我通过这个解决了这个问题。希望有所帮助。

答案 2 :(得分: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;
}


}

答案 3 :(得分:0)

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;

此脚本将返回

类别(例如个性化) 开发者名称 应用程式图示 应用名称 屏幕截图(缩略图和完整预览) 说明

您还可以查看完整的源代码here