Skip to content

Commit 041714e

Browse files
authored
chore: Add API route for dependents (#503)
1 parent cbbe37d commit 041714e

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

packages/docs/src/app/(pages)/_landing/dependents/crawler.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type Result = {
99
repo: string
1010
stars: number
1111
pkg: string
12-
img: string
12+
avatarID: string
1313
}
1414

1515
export async function crawlDependents() {
@@ -51,7 +51,8 @@ async function crawlDependentsPage(url: string) {
5151
const $ = cheerio.load(html)
5252
const results: Result[] = []
5353
$('[data-test-id="dg-repo-pkg-dependent"]').each((index, element) => {
54-
const img = $(element).find('img').attr('src')?.replace('s=40', 's=64')
54+
const img = $(element).find('img').attr('src') // ?.replace('s=40', 's=64')
55+
const avatarID = getAvatarID(img ?? '')
5556
const repoLink = $(element).find('a[data-hovercard-type="repository"]')
5657
const starsStr = $(element)
5758
.find('a[data-hovercard-type="repository"]')
@@ -65,15 +66,28 @@ async function crawlDependentsPage(url: string) {
6566
.text()
6667
.trim()
6768
const stars = parseInt(starsStr.replace(/,/g, ''), 10)
68-
if (!isNaN(stars) && repoLink.length > 0 && ownerStr.length > 0 && img) {
69+
if (
70+
!isNaN(stars) &&
71+
repoLink.length > 0 &&
72+
ownerStr.length > 0 &&
73+
avatarID
74+
) {
6975
const repoName = repoLink.text()
7076
const repo = `${ownerStr}/${repoName}`
7177
if (!['franky47', '47ng'].includes(ownerStr)) {
72-
results.push({ repo, stars, pkg, img })
78+
results.push({ repo, stars, pkg, avatarID })
7379
}
7480
}
7581
})
7682
const nextButton = $('div.paginate-container a:contains(Next)')
7783
const nextPage = nextButton?.attr('href') ?? null
7884
return { results, nextPage }
7985
}
86+
87+
const gitHubAvatarURLRegExp =
88+
/^https:\/\/avatars\.githubusercontent\.com\/u\/(\d+)\?/
89+
90+
function getAvatarID(src: string) {
91+
const match = src.match(gitHubAvatarURLRegExp)
92+
return match ? match[1] : undefined
93+
}

packages/docs/src/app/(pages)/_landing/dependents/dependents.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export async function DependentsSection() {
2929
className="relative h-8 w-8 rounded-full"
3030
>
3131
<Image
32-
src={dep.img}
32+
src={`https://avatars.githubusercontent.com/u/${dep.avatarID}?s=64&v=4`}
3333
alt={dep.repo}
3434
className="rounded-full"
3535
width={64}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { NextResponse } from 'next/server'
2+
import { crawlDependents } from '../../(pages)/_landing/dependents/crawler'
3+
4+
export const revalidate = 86_400 // 24 hours
5+
6+
export async function GET() {
7+
const dependents = await crawlDependents()
8+
return NextResponse.json(dependents)
9+
}

0 commit comments

Comments
 (0)