Skip to content

Commit

Permalink
feat(route): add iknowwhatyoudownload daily stats (#17977)
Browse files Browse the repository at this point in the history
  • Loading branch information
p3psi-boo authored Jan 10, 2025
1 parent eeae0ea commit d572e55
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
112 changes: 112 additions & 0 deletions lib/routes/iknowwhatyoudownload/daily.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
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';
import { getCurrentPath } from '@/utils/helpers';

const __dirname = getCurrentPath(import.meta.url);

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 url = `${baseUrl}${dateFormatted}`;
return cache.tryGet(url, async () => {
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,
};
}
8 changes: 8 additions & 0 deletions lib/routes/iknowwhatyoudownload/namespace.ts
Original file line number Diff line number Diff line change
@@ -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',
};
34 changes: 34 additions & 0 deletions lib/routes/iknowwhatyoudownload/templates/daily.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<article>
<div class="stats">
<h1>Torrent download statistics</h1>
<ul>
{{each numStats}}
<li><span>{{$value.percent}}</span> {{$value.desc}}</li>
{{/each}}
</ul>
</div>

<div class="table-view">
<h1>Table View</h1>
{{if tableData}}
<table>
<tr><th>Category</th><th>Count</th><th>Percent</th></tr>
{{each tableData}}
<tr>
<td>{{$value.key}}</td>
<td>{{$value.count}}</td>
<td>{{$value.percent}}</td>
</tr>
{{/each}}
</table>
{{/if}}
</div>

<div class="top-list">
<h1>Top List</h1>
{{each topList}}
<h2>{{$value.title}}</h2>
{{@ $value.content}}
{{/each}}
</div>
</article>

0 comments on commit d572e55

Please sign in to comment.