1
0
mirror of https://github.com/uetchy/namae.git synced 2025-03-17 20:40:32 +09:00

41 lines
1.1 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(
2021-06-20 19:57:28 +09:00
/AF_initDataCallback.+?hash: '6'.+?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
const apps = response[0][1][0][0][0].map((entry: any) => ({
2020-08-31 09:44:05 +09:00
id: entry[12][0],
name: entry[2],
2022-04-13 15:24:26 +09:00
description: entry[4][1][1][1][1],
2020-08-31 09:44:05 +09:00
author: entry[4][0][0][0],
url: 'https://play.google.com' + entry[9][4][2],
}));
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);
}
}