1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 04:30:31 +09:00

86 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-09 15:16:54 +09:00
import { VercelRequest, VercelResponse } from '@vercel/node';
2021-02-25 16:48:45 +09:00
import 'cross-fetch';
import { fetch, send, sendError } from '../../../util/http';
2020-08-31 09:44:05 +09:00
export default async function handler(
2022-03-09 15:16:54 +09:00
req: VercelRequest,
res: VercelResponse
2020-08-31 09:44:05 +09:00
): Promise<void> {
const { query } = req.query;
if (!query || typeof query !== 'string') {
return sendError(res, new Error('No query given'));
}
try {
const responseText = await fetch(
`https://play.google.com/store/search?c=apps&q=${encodeURIComponent(
query
)}`,
'GET'
).then((res) => res.text());
2021-02-25 16:48:45 +09:00
2020-08-31 09:44:05 +09:00
const response = JSON.parse(
responseText.match(
2022-06-07 12:18:29 +09:00
/AF_initDataCallback.+?hash: '7'.+?data:([\w\W]+?), sideChannel/m
2021-02-25 16:48:45 +09:00
)?.[1] ?? ''
2020-08-31 09:44:05 +09:00
);
2021-02-25 16:48:45 +09:00
2022-06-07 12:18:29 +09:00
console.log(JSON.stringify(response[0][1][0][22][0][0][0], null, 2));
const list = response[0][1][0][22][0];
const apps = list.map((entry: any) => ({
id: entry[0][0][0],
name: entry[0][3],
description: entry[0][13][1],
author: entry[0][14],
url: 'https://play.google.com' + entry[0][10][4][2],
2020-08-31 09:44:05 +09:00
}));
2022-06-04 18:25:58 +09:00
send(res, { result: apps.slice(0, 5) });
2022-03-09 15:16:54 +09:00
} catch (err: any) {
2020-08-31 09:44:05 +09:00
sendError(res, err);
}
}
2022-06-07 12:18:29 +09:00
class WeirdJSONReader {
p = 0;
c = 0;
buf: string;
constructor(buf: string) {
this.buf = buf;
}
parse() {
this.verify();
const jsons = [];
while (this.c < this.buf.length) {
jsons.push(JSON.parse(this.eatPayload()));
}
return jsons;
}
verify() {
if (!this.buf.startsWith(")]}'")) {
throw new Error('Invalid payload');
}
this.c = 6;
}
eatHeader() {
this.p = this.c;
while (this.buf[this.c] !== '\n' && this.c < this.buf.length) {
this.c += 1;
}
return Number(this.buf.slice(this.p, this.c));
}
eatPayload() {
const blen = this.eatHeader();
this.p = this.c;
this.c = this.c + blen;
return this.buf.slice(this.p, this.c);
}
}