forked from playsearch.kaki87.net/v2-api
134 lines
No EOL
4 KiB
JavaScript
134 lines
No EOL
4 KiB
JavaScript
const { spawn } = require('node:child_process');
|
|
|
|
if(!process.env.NO_RESPAWN) process.on(
|
|
'uncaughtException',
|
|
error => {
|
|
console.error(error);
|
|
process.once(
|
|
'exit',
|
|
() => spawn(
|
|
process.argv.shift(),
|
|
process.argv,
|
|
{
|
|
cwd: process.cwd(),
|
|
detached: true,
|
|
stdio: 'inherit'
|
|
}
|
|
)
|
|
);
|
|
process.exit();
|
|
}
|
|
);
|
|
process.on('unhandledRejection', error => console.error(error));
|
|
|
|
require('@lu.se/console');
|
|
|
|
const
|
|
Sentry = require('@sentry/node'),
|
|
fastify = require('fastify')({ trustProxy: true }),
|
|
{ Rybbit } = require('@rybbit/node'),
|
|
{ FastifySSEPlugin } = require('fastify-sse-v2'),
|
|
{ EventIterator } = require('event-iterator'),
|
|
googlePlay = require('google-play-scraper').default.memoized(),
|
|
{ default: PQueue } = require('p-queue');
|
|
|
|
const {
|
|
port,
|
|
sentryDsn,
|
|
rybbitHost,
|
|
rybbitSiteId,
|
|
rybbitApiKey
|
|
} = require('./config.js');
|
|
|
|
if(sentryDsn){
|
|
Sentry.init({ dsn: sentryDsn });
|
|
Sentry.setupFastifyErrorHandler(fastify);
|
|
}
|
|
|
|
if(rybbitHost && rybbitSiteId && rybbitApiKey){
|
|
const rybbit = new Rybbit({
|
|
analyticsHost: `https://${rybbitHost}/api`,
|
|
siteId: rybbitSiteId,
|
|
apiKey: rybbitApiKey
|
|
});
|
|
fastify.addHook(
|
|
'onRequest',
|
|
(
|
|
request,
|
|
reply,
|
|
done
|
|
) => {
|
|
done();
|
|
const url = new URL(request.url, `https://${request.hostname}`);
|
|
rybbit
|
|
.pageview({
|
|
hostname: url.hostname,
|
|
pathname: url.pathname,
|
|
querystring: url.search,
|
|
referrer: request.headers['referer'],
|
|
user_agent: request.headers['user-agent'],
|
|
ip_address: request.ip
|
|
})
|
|
.catch(Sentry.captureException);
|
|
}
|
|
);
|
|
}
|
|
|
|
fastify.register(require('fastify-cors'));
|
|
fastify.register(FastifySSEPlugin);
|
|
|
|
fastify.get(
|
|
'/search',
|
|
(
|
|
request,
|
|
reply
|
|
) => {
|
|
const query = request.query.query;
|
|
if(query){
|
|
reply.sse(new EventIterator(({ push: _push }) => {
|
|
const push = data => _push({ data: JSON.stringify(data) });
|
|
(async () => {
|
|
const
|
|
search = (await googlePlay.search({
|
|
'term': request.query.query,
|
|
'num': 250
|
|
})).map((item, index) => ({ ...item, index })),
|
|
queue = new PQueue();
|
|
await queue.addAll([
|
|
...search.map(item => () => push(item)),
|
|
...search.map(({ appId }) => async () => push(await googlePlay.app({ appId }))),
|
|
...search.map(({ appId }) => async () => push({ appId, permissions: await googlePlay.permissions({ appId }) }))
|
|
]);
|
|
})().catch(error => {
|
|
if([
|
|
'Error requesting Google Play',
|
|
'No results for'
|
|
].every(message => !error.toString().includes(message)))
|
|
throw error;
|
|
}).finally(() => push({ status: 'DONE' }));
|
|
}));
|
|
}
|
|
else
|
|
reply.code(400);
|
|
}
|
|
);
|
|
|
|
fastify.get(
|
|
'/link/:linkId',
|
|
(
|
|
request,
|
|
reply
|
|
) => {
|
|
const url = {
|
|
'about': 'https://git.kaki87.net/playsearch.kaki87.net/v2-web/src/branch/master/README.md',
|
|
'support': 'https://discord.gg/XZ8Ja8d',
|
|
'x3Free': 'https://gist.github.com/KaKi87/d64d7185a8ccb7823d89164c709a6851'
|
|
}[request.params['linkId']];
|
|
if(url)
|
|
reply.redirect(301, url);
|
|
else
|
|
reply.code(404).send();
|
|
}
|
|
);
|
|
|
|
fastify.listen(port).then(() => console.log(`Server listening to port ${port}`)).catch(console.error); |