-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
55 lines (43 loc) · 2.02 KB
/
Copy pathsearch.py
File metadata and controls
55 lines (43 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
App Store Search - Chocodata App Store Scraper API
Runnable example. It calls the LIVE API and prints the real JSON response.
pip install requests
export CHOCODATA_API_KEY="your_key" # free: 1,000 requests, one-time
python app_store_scraper_api_codes/search.py
Docs: https://chocodata.com/docs
"""
import json
import os
import sys
import requests
API = "https://api.chocodata.com/api/v1/appstore/search"
KEY = os.environ.get("CHOCODATA_API_KEY")
if not KEY:
sys.exit("Set CHOCODATA_API_KEY first. Free key (1,000 requests, one-time): https://chocodata.com")
def _check(r) -> None:
"""Map the API's documented errors onto actionable messages instead of a traceback."""
if r.status_code == 400:
sys.exit(f"400 invalid_params: {json.dumps(r.json().get('issues', r.text))[:200]}")
if r.status_code == 401:
sys.exit("401 INVALID_API_KEY: key missing or not recognised. Get one: https://chocodata.com")
if r.status_code == 402:
sys.exit("402 INSUFFICIENT_CREDITS: balance exhausted. Top up or upgrade: https://chocodata.com/pricing")
if r.status_code == 429:
sys.exit("429 RATE_LIMITED: over your plan's concurrency. Back off and retry.")
if r.status_code == 502:
sys.exit("502 extraction_failed: Apple returned no usable data for this request. "
"Retryable. You were not charged.")
r.raise_for_status()
def search(term: str, country: str = "us", limit: int = 10) -> dict:
"""Search the App Store and return ranked results as structured JSON."""
params = {"api_key": KEY, "term": term, "country": country, "limit": limit}
r = requests.get(API, params=params, timeout=90)
_check(r)
return r.json()
if __name__ == "__main__":
data = search("spotify")
print(json.dumps(data, indent=2, ensure_ascii=False)[:2000])
print()
top = data["results"][0]
print(f"{len(data['results'])} apps | #1: {top['title'][:44]} | "
f"{top['formatted_price']} | {top['rating']:.2f} stars from {top['reviews_count']:,} ratings")