web/src/utils/fetch-plexus-info.ts
154 lines · 4228 bytes
14 symbols in this file
1import { error } from './logger';
2import { safeFetch } from './safe-fetch';
3
4const extractPackage = (str: string): string =>
5 str.includes('id=') ? str.split('id=')[1] : str;
6
7const BASE = 'https://plexus.techlore.tech';
8const RATINGS_LIMIT = 100;
9const TOP_ROMS = 6;
10const MIN_RATINGS_PER_ROM = 2;
11
12export interface PlexusScore {
13 numerator: number;
14 denominator: number;
15 total_count: number;
16}
17
18export interface PlexusRomSummary {
19 rom_name: string;
20 rating_type: 'native' | 'micro_g';
21 avg: number;
22 count: number;
23 denominator: number;
24 sources: Record<string, number>;
25}
26
27export interface PlexusInfo {
28 package: string;
29 name: string;
30 updated_at: string;
31 scores: { native?: PlexusScore; micro_g?: PlexusScore };
32 roms: PlexusRomSummary[];
33 androidVersions?: { min: string; max: string };
34 link: string;
35}
36
37interface RawAppResponse {
38 data: {
39 package: string;
40 name: string;
41 updated_at: string;
42 scores?: { native?: PlexusScore; micro_g?: PlexusScore };
43 };
44}
45
46interface RawRating {
47 rom_name?: string;
48 rating_type: 'native' | 'micro_g';
49 score: { numerator: number; denominator: number };
50 installation_source?: string;
51 android_version?: string;
52}
53
54interface RawRatingsResponse {
55 data: RawRating[];
56}
57
58const summariseRoms = (ratings: RawRating[]): PlexusRomSummary[] => {
59 const groups = new Map<
60 string,
61 {
62 rom_name: string;
63 rating_type: 'native' | 'micro_g';
64 total: number;
65 count: number;
66 denominator: number;
67 sources: Record<string, number>;
68 }
69 >();
70 for (const r of ratings) {
71 if (!r.rom_name || !r.score) continue;
72 const key = `${r.rom_name}|${r.rating_type}`;
73 const cur = groups.get(key);
74 if (cur) {
75 cur.total += r.score.numerator;
76 cur.count += 1;
77 if (r.installation_source)
78 cur.sources[r.installation_source] =
79 (cur.sources[r.installation_source] || 0) + 1;
80 } else {
81 const sources: Record<string, number> = {};
82 if (r.installation_source) sources[r.installation_source] = 1;
83 groups.set(key, {
84 rom_name: r.rom_name,
85 rating_type: r.rating_type,
86 total: r.score.numerator,
87 count: 1,
88 denominator: r.score.denominator,
89 sources,
90 });
91 }
92 }
93 return [...groups.values()]
94 .filter((g) => g.count >= MIN_RATINGS_PER_ROM)
95 .map((g) => ({
96 rom_name: g.rom_name,
97 rating_type: g.rating_type,
98 avg: g.total / g.count,
99 count: g.count,
100 denominator: g.denominator,
101 sources: g.sources,
102 }))
103 .sort((a, b) => b.count - a.count)
104 .slice(0, TOP_ROMS);
105};
106
107const summariseAndroidVersions = (
108 ratings: RawRating[],
109): { min: string; max: string } | undefined => {
110 const majors = ratings
111 .map((r) => parseInt(r.android_version || '', 10))
112 .filter((n) => Number.isFinite(n));
113 if (!majors.length) return undefined;
114 return { min: String(Math.min(...majors)), max: String(Math.max(...majors)) };
115};
116
117export const fetchPlexusInfo = async (
118 androidPackage: string,
119): Promise<PlexusInfo | null> => {
120 const pkg = extractPackage(androidPackage);
121 try {
122 const [appRes, ratingsRes] = await Promise.all([
123 safeFetch(`${BASE}/api/v1/apps/${pkg}?scores=true`),
124 safeFetch(`${BASE}/api/v1/apps/${pkg}/ratings?limit=${RATINGS_LIMIT}`),
125 ]);
126 if (!appRes.ok) {
127 if (appRes.status !== 404) {
128 error('Plexus', `HTTP ${appRes.status} for ${pkg}`);
129 }
130 return null;
131 }
132 const app: RawAppResponse = await appRes.json();
133 let roms: PlexusRomSummary[] = [];
134 let androidVersions: { min: string; max: string } | undefined;
135 if (ratingsRes.ok) {
136 const ratings: RawRatingsResponse = await ratingsRes.json();
137 const data = ratings.data || [];
138 roms = summariseRoms(data);
139 androidVersions = summariseAndroidVersions(data);
140 }
141 return {
142 package: app.data.package,
143 name: app.data.name,
144 updated_at: app.data.updated_at,
145 scores: app.data.scores || {},
146 roms,
147 androidVersions,
148 link: `https://plexus.techlore.tech/?app=${encodeURIComponent(pkg)}`,
149 };
150 } catch (err) {
151 error('Plexus', `Network error for ${pkg}: ${err}`);
152 return null;
153 }
154};