Get GPC expanded category
const express = require("@runkit/runkit/express-endpoint/1.0.0")
const app = express(exports)
const got = require('got')
app.get("/", async (req, res) => {
const keyword = req.query.q
const locale = req.query.l || 'en-US'
if (!keyword) return res.status(400)
.header('Content-Type', 'application/json')
.send({ error: "No query parameter provided" })
const { body: categories } =
await got(`https://www.google.com/basepages/producttype/taxonomy-with-ids.${locale}.txt`)
const regex_exact = "\\d+ ?- ?(.*> " + keyword + "s?)quot;
const regex_similar = "\\d+ ?- ?.*>.*\\W" + keyword + "s?($|\\s[^>\\n]*)quot;
const exact = categories.match(new RegExp(regex_exact, "imu"))
const similar = categories.match(new RegExp(regex_similar, "gimu"))
res.header('Content-Type', 'application/json').send({
...(exact && {exact_category: exact[1]}),
...(similar && !exact &&
{categories: similar.map(m => m.replace(/\d+ ?- ?/, '')).filter(c => (exact && c !== exact[1]) || true)})
})
})
no comments