-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(route): add iknowwhatyoudownload daily stats (#17977)
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |