Getting versionCode and VersionName from Google Play

Viewed 13678

I am looking for a way how to get app versionCode and VersionName from google play with package name via java app in PC. I have seen: https://androidquery.appspot.com/ but it not working anymore and also https://code.google.com/archive/p/android-market-api/ started to making problems and also stopped working, and it requer device ID. Can you help me with some simple solution or API for this? Very important, i need versionCode and VersionName and VersionName is relatively easy to get by parsing html google play app site. The versionCode is very important.

4 Answers

Apart from using JSoup, we can alternatively do pattern matching for getting the app version from playStore.

To match the latest pattern from google playstore ie <div class="BgcNfc">Current Version</div><span class="htlgb"><div><span class="htlgb">X.X.X</span></div> we first have to match the above node sequence and then from above sequence get the version value. Below is the code snippet for same:

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

I got this solved through this. Hope that helps.

Jsoup takes too long, its inefficient, for short easy way with pattermatching:

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


}
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();
    }
}

Imports

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;

This script will return

Category (Personalization for example) Developer Name App Icon App Name Screenshots (Thumbnail and Full preview) Description

You can also check the full source code here

Related