diff --git a/lib/routes/scoop/apps.ts b/lib/routes/scoop/apps.ts new file mode 100644 index 000000000..ca495a308 --- /dev/null +++ b/lib/routes/scoop/apps.ts @@ -0,0 +1,188 @@ +import { type Data, type DataItem, type Route, ViewType } from '@/types'; + +import { art } from '@/utils/render'; +import ofetch from '@/utils/ofetch'; +import { parseDate } from '@/utils/parse-date'; + +import { type CheerioAPI, load } from 'cheerio'; +import { type Context } from 'hono'; +import path from 'node:path'; + +const orderbys = (desc: string) => { + const base = { + 0: 'search.score() desc, Metadata/OfficialRepositoryNumber desc, NameSortable asc', + 1: 'NameSortable asc, Metadata/OfficialRepositoryNumber desc, Metadata/RepositoryStars desc, Metadata/Committed desc', + 2: 'Metadata/Committed desc, Metadata/OfficialRepositoryNumber desc, Metadata/RepositoryStars desc', + }; + + if (desc === '1') { + return base; + } + + const inverted = {}; + for (const key in base) { + const orderStr = base[key]; + inverted[key] = orderStr.replaceAll(/\b(desc|asc)\b/gi, (match) => (match.toLowerCase() === 'desc' ? 'asc' : 'desc')); + } + return inverted; +}; + +const filters = { + o: 'Metadata/OfficialRepositoryNumber eq 1', // offical buckets only + dm: 'Metadata/DuplicateOf eq null', // distinct manifests only +}; + +export const handler = async (ctx: Context): Promise => { + const { query = 's=2&d=1&n=true&dm=true&o=true' } = ctx.req.param(); + const limit: number = Number.parseInt(ctx.req.query('limit') ?? '50', 10); + + const baseUrl: string = 'https://scoop.sh'; + const apiBaseUrl: string = 'https://scoopsearch.search.windows.net'; + const targetUrl: string = new URL(`/#/apps?${query}`, baseUrl).href; + const apiUrl: string = new URL('indexes/apps/docs/search', apiBaseUrl).href; + + const targetResponse = await ofetch(targetUrl); + const $: CheerioAPI = load(targetResponse); + const language = $('html').attr('lang') ?? 'en'; + + const scriptRegExp: RegExp = /