Skip to content

Commit

Permalink
Allow to force a country
Browse files Browse the repository at this point in the history
  • Loading branch information
pavi2410 committed Nov 17, 2023
1 parent e46b408 commit e5953d8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 20 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -71,11 +71,14 @@ app.get('/health', (c) => {
// })


// GET /badge/downloads?id=<appId>&pretty
// GET /badge/downloads?id=<appId>&pretty&country=<countryCode>
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)

Expand All @@ -90,11 +93,14 @@ app.get('/badge/downloads', async (c) => {
}))
})

// GET /badge/ratings?id=<appId>&pretty
// GET /badge/ratings?id=<appId>&pretty&country=<countryCode>
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)

Expand All @@ -109,11 +115,15 @@ app.get('/badge/ratings', async (c) => {
}))
})

// GET /badge/full?id=<appId>
// GET /badge/full?id=<appId>&country=<countryCode>
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)
Expand Down
15 changes: 15 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}

0 comments on commit e5953d8

Please sign in to comment.