api/src/routes/private/android.ts
93 lines · 2847 bytes
9 symbols in this file
1// Exodus tracker analysis merged with Plexus de-Googled compatibility
2import { createRoute, z } from '@hono/zod-openapi'
3import { fetchJson } from '@/lib/fetch'
4import { ApiError } from '@/lib/errors'
5import { freshSeconds } from '@/lib/cache/freshness'
6import { newApp } from '@/lib/openapi'
7import { summarizeRatings, type PlexusRating } from '@/lib/plexus'
8import { AndroidReportSchema, ErrorResponse, Ok } from '@/schemas'
9
10const app = newApp()
11
12const FRESH_TTL = freshSeconds('android')
13
14interface AppDetails {
15 trackers: number[]
16 [key: string]: unknown
17}
18interface TrackerLookup {
19 trackers: Record<string, unknown>
20}
21
22const EXODUS_BASE = 'https://reports.exodus-privacy.eu.org/api'
23const PLEXUS_BASE = 'https://plexus.techlore.tech/api/v1'
24
25// Exodus apk details with tracker ids resolved to records, null if unavailable
26const getExodus = async (pkg: string, token: string) => {
27 try {
28 const headers = { Authorization: `Token ${token}` }
29 const details = await fetchJson<AppDetails[]>(
30 `${EXODUS_BASE}/search/${encodeURIComponent(pkg)}/details`,
31 { headers, timeoutMs: 10000 },
32 )
33 const appInfo = details?.[0]
34 if (!appInfo) return null
35 const { trackers } = await fetchJson<TrackerLookup>(`${EXODUS_BASE}/trackers`, {
36 headers,
37 timeoutMs: 10000,
38 })
39 return { ...appInfo, trackers: appInfo.trackers.map((id) => trackers[String(id)]) }
40 } catch {
41 return null
42 }
43}
44
45// Plexus crowdsourced de-Googled compatibility, null if the app has no ratings
46const getDegoogled = async (pkg: string) => {
47 try {
48 const { data } = await fetchJson<{ data: PlexusRating[] }>(
49 `${PLEXUS_BASE}/apps/${encodeURIComponent(pkg)}/ratings`,
50 )
51 return data?.length ? summarizeRatings(data) : null
52 } catch {
53 return null
54 }
55}
56
57const route = createRoute({
58 method: 'get',
59 path: '/enrich/android/{pkg}',
60 tags: ['Enrichment'],
61 summary: 'Android app info',
62 request: { params: z.object({ pkg: z.string() }) },
63 responses: {
64 200: Ok(AndroidReportSchema),
65 401: ErrorResponse,
66 404: ErrorResponse,
67 500: ErrorResponse,
68 },
69})
70
71// Merge Exodus trackers with Plexus de-Googled status, 404 only if both miss
72app.openapi(route, async (c) => {
73 const { pkg } = c.req.valid('param')
74 const token = c.env.EXODUS_TOKEN
75 if (!token) throw new ApiError('INTERNAL', 'EXODUS_TOKEN not configured', 500)
76 const data = await c.var.storage.fetch(`android:${pkg}`, FRESH_TTL, async () => {
77 const [exodus, degoogled] = await Promise.all([
78 getExodus(pkg, token),
79 getDegoogled(pkg),
80 ])
81 if (!exodus && !degoogled) {
82 throw new ApiError(
83 'NOT_FOUND',
84 `Package '${pkg}' not found on Exodus or Plexus`,
85 404,
86 )
87 }
88 return { ...exodus, degoogled: degoogled ?? null }
89 })
90 return c.json(data, 200)
91})
92
93export default app