From e5953d86db2d44b7896645e75ffb45cdb12f4bab Mon Sep 17 00:00:00 2001 From: Pavitra Golchha Date: Fri, 17 Nov 2023 22:51:17 +0530 Subject: [PATCH] Allow to force a country --- README.md | 1 + src/index.ts | 30 ++++++++++++++++++++---------- src/utils.ts | 15 +++++++++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index e527ec4..bd26333 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ Show off your Play Store™ app's downloads and rating in your repo ### Options - `pretty`: Shows the numbers prettily (default = disabled; add the flag to enable, remove to disable) +- `country`: Country code (default = `us` or wherever the request is coming from) ## Credits diff --git a/src/index.ts b/src/index.ts index c279185..ebb3d4e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import {Hono} from "hono" import {fetchAppDetails} from './google-play-scraper.js' import {fullBadge} from './full-badge.js' import {shieldsBadge} from "./shields-badge"; -import {compactNumberFormatter, makeStars} from "./utils"; +import {compactNumberFormatter, findCountryCode, makeStars} from "./utils"; type Bindings = { NODE_ENV: string @@ -71,11 +71,14 @@ app.get('/health', (c) => { // }) -// GET /badge/downloads?id=&pretty +// GET /badge/downloads?id=&pretty&country= app.get('/badge/downloads', async (c) => { - const {id: appId, pretty} = c.req.query() + const {id: appId, pretty, country} = c.req.query() const isPretty = pretty !== undefined - const countryCode = (c.req.raw.cf?.country as string | undefined) ?? 'US' + const countryCode = findCountryCode( + country, + c.req.raw.cf?.country as string | undefined + ); const appDetails = await fetchAppDetails(appId, countryCode) @@ -90,11 +93,14 @@ app.get('/badge/downloads', async (c) => { })) }) -// GET /badge/ratings?id=&pretty +// GET /badge/ratings?id=&pretty&country= app.get('/badge/ratings', async (c) => { - const {id: appId, pretty} = c.req.query() + const {id: appId, pretty, country} = c.req.query() const isPretty = pretty !== undefined - const countryCode = (c.req.raw.cf?.country as string | undefined) ?? 'US' + const countryCode = findCountryCode( + country, + c.req.raw.cf?.country as string | undefined + ); const appDetails = await fetchAppDetails(appId, countryCode) @@ -109,11 +115,15 @@ app.get('/badge/ratings', async (c) => { })) }) -// GET /badge/full?id= +// GET /badge/full?id=&country= app.get('/badge/full', async (c) => { - const {id: appId} = c.req.query() + const {id: appId, country} = c.req.query() + const countryCode = findCountryCode( + country, + c.req.raw.cf?.country as string | undefined + ); - const appDetails = await fetchAppDetails(appId) + const appDetails = await fetchAppDetails(appId, countryCode) if (!appDetails) { return c.text('App not found', 404) diff --git a/src/utils.ts b/src/utils.ts index ef965aa..c30a9b4 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -9,3 +9,18 @@ export function makeStars(score: number): string { const right = 5 - left return ('★').repeat(left) + ('☆').repeat(right) } + +export function findCountryCode( + country: string | undefined, + requestCountry: string | undefined +): string { + if (country) { + return country; + } + + if (requestCountry) { + return requestCountry; + } + + return "US"; +} \ No newline at end of file