-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
p3psi-boo
wants to merge
1
commit into
DIYgod:master
Choose a base branch
from
p3psi-boo:iknowwhatyoudownload
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+152
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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]+)\]/); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
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> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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