// App Store keyword rank check through Apple's public iTunes Search API (no account, no scraping). // storebound aso rank [--storefronts us,de=de-DE] [--keywords "a,b"] [--only-keywords] [--competitors 3] [--limit 200] // [--delay 3500] [--dry] [--json] // Keywords: the keyword field of each storefront's locale in store/appstore.md (us -> the primary locale unless // "us=en-GB"), plus --keywords (--only-keywords: just those). One request per keyword and storefront, at most one // every 3.5 s (Apple allows about 20 a minute); a 403 / 429 stops the run with what was read. Rank = position of the // app (apple.appId, else the bundle id) in the first --limit results, "-" when not there; plus the first competitors // (title, developer, rating). Subtitles are not in the public API and are not shown. Writes // .storebound/aso/.json (and compares with the previous one) unless --dry. Pure parts: cli/lib/aso-rank.ts. import { existsSync, mkdirSync, readdirSync, readFileSync, writeFileSync } from 'node:fs'; import { join } from 'node:path'; import type { Ctx } from './lib/ctx'; import { done, log, printJson, warn } from './lib/log'; import { competitors, keywordList, MIN_DELAY_MS, parseStorefronts, rankDelta, rankOf, type RankRow, type SearchApp, searchUrl } from './lib/aso-rank'; import { appleSections } from './lib/project-files'; import { textTable } from './lib/table'; const TAG = 'aso'; export async function run(ctx: Ctx): Promise { const { config, resolve } = ctx.loaded; const a = ctx.args; if (a._[0] !== 'rank') throw new Error('usage: storebound aso rank [--storefronts us,de=de-DE] [--keywords "a,b"] [--dry] [--json]'); if (!config.apple) throw new Error('aso rank checks App Store search: the config has no apple section'); const primary = config.apple.primaryLocale ?? config.languages.find(l => l.apple)?.apple ?? 'en-US'; const listingFile = resolve(config.apple.listing ?? 'store/appstore.md'); const listing = existsSync(listingFile) ? appleSections(readFileSync(listingFile, 'utf8')) : new Map>(); const extra = a.list('keywords'); const fronts = parseStorefronts(a.list('storefronts'), primary); const n = a.num('competitors', 3); const limit = a.num('limit', 200); const delay = Math.max(MIN_DELAY_MS, a.num('delay', MIN_DELAY_MS)); const app = { appId: config.apple.appId, bundleId: config.apple.bundleId }; const plan = fronts.flatMap(f => keywordList(a.has('only-keywords') ? '' : listing.get(f.locale)?.keywords, extra).map(k => ({ ...f, keyword: k }))); if (!plan.length) throw new Error(`no keywords: ${listingFile} has no keywords for ${fronts.map(f => f.locale).join(', ')}; pass --keywords`); log(TAG, `${plan.length} quer${plan.length === 1 ? 'y' : 'ies'} (${fronts.map(f => f.storefront).join(', ')}), one every ${delay / 1000}s: about ${Math.ceil(plan.length * delay / 60_000)} min`); if (a.has('dry')) { if (a.has('json')) printJson({ dryRun: true, queries: plan.map(p => ({ storefront: p.storefront, keyword: p.keyword, url: searchUrl(p.keyword, p.storefront, limit) })) }); else for (const p of plan) log(TAG, `would GET ${searchUrl(p.keyword, p.storefront, limit)}`); return; } const rows: RankRow[] = []; for (const [i, p] of plan.entries()) { if (i) await Bun.sleep(delay); const r = await fetch(searchUrl(p.keyword, p.storefront, limit), { headers: { Accept: 'application/json' } }); if (r.status === 403 || r.status === 429) { warn(TAG, `Search API answered ${r.status} (rate limit): stopped after ${rows.length} of ${plan.length}; try later or with a larger --delay`); break; } if (!r.ok) { warn(TAG, `${p.storefront} "${p.keyword}": HTTP ${r.status}`); continue; } const results = ((await r.json()) as { results?: SearchApp[] }).results ?? []; const row: RankRow = { storefront: p.storefront, locale: p.locale, keyword: p.keyword, rank: rankOf(results, app), results: results.length, competitors: competitors(results, app, n) }; rows.push(row); log(TAG, `${i + 1}/${plan.length} ${p.storefront} "${p.keyword}": ${row.rank ?? `not in the first ${results.length}`}`); } const dir = resolve('.storebound/aso'); const prevFile = existsSync(dir) ? readdirSync(dir).filter(f => /^\d{4}-\d{2}-\d{2}\.json$/.test(f)).sort().at(-1) : undefined; const prev = prevFile ? (JSON.parse(readFileSync(join(dir, prevFile), 'utf8')) as { rows?: RankRow[] }).rows : undefined; const delta = rankDelta(rows, prev); const date = new Date().toISOString().slice(0, 10); if (a.has('json')) printJson({ date, previous: prevFile ?? null, rows: rows.map(r => ({ ...r, change: delta.get(`${r.storefront}|${r.keyword.toLowerCase()}`) ?? null })) }); else console.log(textTable(['store', 'keyword', 'rank', 'change', 'top competitors'], rows.map(r => { const d = delta.get(`${r.storefront}|${r.keyword.toLowerCase()}`); return [r.storefront, r.keyword, r.rank ?? '-', d === null || d === undefined ? '' : d > 0 ? `+${d}` : String(d), r.competitors.map(c => `${c.rank}. ${c.title.slice(0, 28)} (${c.developer.slice(0, 18)})`).join('; ')]; }))); mkdirSync(dir, { recursive: true }); writeFileSync(join(dir, `${date}.json`), JSON.stringify({ date, app: { appId: app.appId ?? null, bundleId: app.bundleId }, rows }, null, 2) + '\n'); done(TAG, `${rows.length} rank(s) saved to ${join(dir, `${date}.json`)}${prevFile ? ` (change vs ${prevFile})` : ''}; Search API order is close to App Store search, read it as a trend`); }