Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): add iknowwhatyoudownload daily stats #17977

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions lib/routes/iknowwhatyoudownload/daily.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please include namespace in the cache key

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]+)\]/);
Copy link
Preview

Copilot AI Dec 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dataMatch regex pattern is used without checking if it matches the expected format. Add a check to ensure it matches before proceeding.

Copilot is powered by AI, so mistakes are possible. Review output carefully before use.

Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
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>
Loading