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

41 lines
1.0 KiB
TypeScript
Raw Normal View History

2020-08-31 09:44:05 +09:00
import { NowRequest, NowResponse } 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(
req: NowRequest,
res: NowResponse
): 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(
/AF_initDataCallback.+?hash: '5'.+?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],
author: entry[4][0][0][0],
rating: entry[6][0][2][1][1],
url: 'https://play.google.com' + entry[9][4][2],
}));
send(res, { result: apps.slice(0, 10) });
} catch (err) {
sendError(res, err);
}
}