From 03f830978ae9b7fd4a3e9b373fb80f97fc86d187 Mon Sep 17 00:00:00 2001 From: Bubu Date: Wed, 25 Dec 2024 16:18:03 +0800 Subject: [PATCH] feat(route): add iknowwhatyoudownload daily stats --- lib/routes/iknowwhatyoudownload/daily.ts | 110 ++++++++++++++++++ lib/routes/iknowwhatyoudownload/namespace.ts | 8 ++ .../iknowwhatyoudownload/templates/daily.art | 34 ++++++ 3 files changed, 152 insertions(+) create mode 100644 lib/routes/iknowwhatyoudownload/daily.ts create mode 100644 lib/routes/iknowwhatyoudownload/namespace.ts create mode 100644 lib/routes/iknowwhatyoudownload/templates/daily.art diff --git a/lib/routes/iknowwhatyoudownload/daily.ts b/lib/routes/iknowwhatyoudownload/daily.ts new file mode 100644 index 00000000000000..589d92b3950ce3 --- /dev/null +++ b/lib/routes/iknowwhatyoudownload/daily.ts @@ -0,0 +1,110 @@ +import { Route } from '@/types'; +import cache from '@/utils/cache'; +import { load } from 'cheerio'; +import dayjs from 'dayjs'; +import got from '@/utils/got'; +import { art } from '@/utils/render'; +import path from 'path'; + +interface TableData { + key: string; + count: string; + percent: string; +} + +export const route: Route = { + path: '/stats/daily/:country', + categories: ['other'], + example: '/iknowwhatyoudownload/stats/daily/CN', + url: 'iknowwhatyoudownload.com', + name: 'Daily Torrents Statistics', + maintainers: ['p3psi-boo'], + parameters: { country: 'the country of the stats. ISO 3166-1 alpha-2 code.' }, + handler, +}; + +async function handler(ctx) { + const { country } = ctx.req.param(); + const baseUrl = `https://iknowwhatyoudownload.com/en/stat/${country}/daily/q?statDate=`; + + const dates = Array.from({ length: 7 }, (_, i) => dayjs().subtract(i, 'day')); + + const items = ( + await Promise.all( + dates.map((dateObj) => { + const dateFormatted = dateObj.format('YYYY-MM-DD'); + const cacheKey = `${dateFormatted}-${country}`; + return cache.tryGet(cacheKey, async () => { + const url = `${baseUrl}${dateFormatted}`; + const response = await got({ + method: 'get', + url, + }); + + if (!response) { + return {}; + } + + const $ = load(response.data); + + const numStats: { percent: string; desc: string }[] = []; + $('.usePercent').each((_, elem) => { + numStats.push({ + percent: $(elem).text(), + desc: $(elem).parent().find('span').last().text(), + }); + }); + + const tableData: TableData[] = []; + const dataMatch = response.data.match(/data:\s*\[([\d",\s]+)\]/); + const labelsMatch = response.data.match(/labels:\s*\[(.*?)\]/); + + if (dataMatch?.[1] && labelsMatch?.[1]) { + const dataList = dataMatch[1].split(',').map((s) => s.trim().replaceAll('"', '')); + const labelsList = labelsMatch[1] + .split(',') + .map((s) => s.replaceAll('"', '').trim()) + .filter((i) => i !== ''); + + for (const index in labelsList) { + const label = labelsList[index]; + const count = dataList[index]; + const [key, percent] = label.split(' '); + tableData.push({ + key, + count, + percent, + }); + } + } + + const topList = $('.tab-pane') + .toArray() + .map((item) => ({ + title: $(item).attr('id')?.toUpperCase(), + content: $(item).find('ul').toString(), + })); + + const content = art(path.join(__dirname, 'templates/daily.art'), { + numStats, + tableData, + topList, + }); + + return { + title: `Daily Torrents Statistics in ${country} for ${dateFormatted}`, + link: url, + description: content, + pubDate: dateObj.toDate(), + }; + }); + }) + ) + ).filter((item) => Object.keys(item).length > 0); + + return { + title: `Daily Torrents Statistics in ${country} - iknownwhatyoudownload`, + link: 'https://iknowwhatyoudownload.com', + item: items, + }; +} diff --git a/lib/routes/iknowwhatyoudownload/namespace.ts b/lib/routes/iknowwhatyoudownload/namespace.ts new file mode 100644 index 00000000000000..2074a1397e53cc --- /dev/null +++ b/lib/routes/iknowwhatyoudownload/namespace.ts @@ -0,0 +1,8 @@ +import type { Namespace } from '@/types'; + +export const namespace: Namespace = { + name: 'I Know What You Download', + url: 'iknowwhatyoudownload.com', + description: '', + lang: 'en', +}; diff --git a/lib/routes/iknowwhatyoudownload/templates/daily.art b/lib/routes/iknowwhatyoudownload/templates/daily.art new file mode 100644 index 00000000000000..1466aea09c3374 --- /dev/null +++ b/lib/routes/iknowwhatyoudownload/templates/daily.art @@ -0,0 +1,34 @@ +
+
+

Torrent download statistics

+
    + {{each numStats}} +
  • {{$value.percent}} {{$value.desc}}
  • + {{/each}} +
+
+ +
+

Table View

+ {{if tableData}} + + + {{each tableData}} + + + + + + {{/each}} +
CategoryCountPercent
{{$value.key}}{{$value.count}}{{$value.percent}}
+ {{/if}} +
+ +
+

Top List

+ {{each topList}} +

{{$value.title}}

+ {{@ $value.content}} + {{/each}} +
+