Skip to content

Commit e1f7154

Browse files
committed
feat: use html parsing for github stars badge
1 parent 0ca768c commit e1f7154

File tree

5 files changed

+142
-7
lines changed

5 files changed

+142
-7
lines changed

libs/create-badgen-handler-next.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,13 @@ function simpleDecode (str: string): string {
116116
}
117117

118118
export class BadgenError {
119-
public status: string // error badge param: status (required)
120-
public color: string // error badge param: color
121-
public code: number // status code for response
119+
/** The "status" param for error badge (required) */
120+
public status: string
121+
/** The "color" param for error badge, red by default */
122+
public color: string
123+
/** The status code for error badge response, 500 by default */
124+
public code: number
125+
/** The error message */
122126
public message: string
123127

124128
constructor ({ status, color = 'grey', code = 500, message = '' }) {

libs/query-html.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import got from 'got'
2+
import { parse } from 'node-html-parser'
3+
4+
export default async function queryHTML(url: string, cssSelector: string) {
5+
const htmlString = await got(url).text()
6+
const root = parse(htmlString)
7+
return root.querySelector(cssSelector)
8+
}

package-lock.json

Lines changed: 111 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"measurement-protocol": "^0.1.1",
3535
"millify": "^6.1.0",
3636
"my-way": "^2.0.0",
37+
"node-html-parser": "^6.1.5",
3738
"original-url": "^1.2.3",
3839
"semver": "^7.3.5",
3940
"serve-handler": "^6.1.3",

pages/api/github.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import got from 'libs/got'
44
import { restGithub, queryGithub } from 'libs/github'
55
import { createBadgenHandler, PathArgs, BadgenError } from 'libs/create-badgen-handler-next'
66
import { coverageColor, millify, version } from 'libs/utils'
7+
import queryHTML from 'libs/query-html'
78

89
type DependentsType = 'REPOSITORY' | 'PACKAGE'
910

@@ -297,7 +298,7 @@ const makeRepoQuery = (topic, owner, repo, restArgs) => {
297298
case 'last-commit':
298299
queryBody = `
299300
${
300-
restArgs.ref ?
301+
restArgs.ref ?
301302
`branch: ref(qualifiedName: "${restArgs.ref}")` :
302303
'defaultBranchRef'
303304
} {
@@ -364,10 +365,20 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
364365
color: 'blue'
365366
}
366367
case 'stars':
367-
const { stargazers_count } = await meta({ owner, repo })
368+
// const { stargazers_count } = await meta({ owner, repo })
369+
const starsElem = await queryHTML(`https://github.com/${owner}/${repo}`, '#repo-stars-counter-star')
370+
const stargazers_count = starsElem?.innerText
371+
372+
if (stargazers_count === undefined) {
373+
throw new BadgenError({
374+
status: 'not found',
375+
message: `Could not find stargazers_count for ${owner}/${repo}`
376+
})
377+
}
378+
368379
return {
369380
subject: topic,
370-
status: millify(stargazers_count),
381+
status: stargazers_count,
371382
color: 'blue'
372383
}
373384
case 'license':
@@ -451,7 +462,7 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
451462
return {
452463
subject: topic,
453464
status: millify(
454-
result.branch ?
465+
result.branch ?
455466
result.branch.target.history.totalCount :
456467
result.defaultBranchRef.target.history.totalCount
457468
),

0 commit comments

Comments
 (0)