Chapter 2. Querying the Play Store from Python

One listing, a filtered search, pagination and a polite retry loop, in about thirty lines of Python.

One listing

import os, requests

API = "https://playapi.info/api/v1"
H = {"X-API-Key": os.environ["API_KEY"]}

app = requests.get(f"{API}/app", params={"id": "com.duolingo"}, headers=H, timeout=30).json()["app"]
print(app["title"], app["installs"]["label"], app["rating"]["score"])

A filtered search

/search takes any mix of filters. Education apps with a million installs and a rating of 4.5 or better, best rated first:

params = {"category": "EDUCATION", "min_installs": 1000000,
          "min_score": 4.5, "sort": "score", "per": 100}
r = requests.get(f"{API}/search", params=params, headers=H, timeout=30).json()
print(r["total"], "listings")

Pagination

Answers carry page, per and pages. Walk pages until you reach pages; results stop at the 10,000th item, so split large sets by genre or install band.

def all_pages(path, params):
    page = 1
    while True:
        r = requests.get(f"{API}/{path}", params={**params, "page": page}, headers=H, timeout=30)
        if r.status_code == 429:
            time.sleep(2); continue
        d = r.json()
        yield from d["results"]
        if page >= d["pages"]: break
        page += 1

Costs

A single listing costs one credit, a search page two. The free plan's 1,000 monthly credits cover about 500 search pages. Answers are cached for hours, so repeating a query during development is cheap.