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: use html parsing for github stars badge #644

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions libs/create-badgen-handler-next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ function simpleDecode (str: string): string {
}

export class BadgenError {
public status: string // error badge param: status (required)
public color: string // error badge param: color
public code: number // status code for response
/** The "status" param for error badge (required) */
public status: string
/** The "color" param for error badge, red by default */
public color: string
/** The status code for error badge response, 500 by default */
public code: number
/** The error message */
public message: string

constructor ({ status, color = 'grey', code = 500, message = '' }) {
Expand Down
8 changes: 8 additions & 0 deletions libs/query-html.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import got from 'got'
import { parse } from 'node-html-parser'

export default async function queryHTML(url: string, cssSelector: string) {
const htmlString = await got(url).text()
const root = parse(htmlString)
return root.querySelector(cssSelector)
}
111 changes: 111 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"measurement-protocol": "^0.1.1",
"millify": "^6.1.0",
"my-way": "^2.0.0",
"node-html-parser": "^6.1.5",
"original-url": "^1.2.3",
"semver": "^7.3.5",
"serve-handler": "^6.1.3",
Expand Down
19 changes: 15 additions & 4 deletions pages/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import got from 'libs/got'
import { restGithub, queryGithub } from 'libs/github'
import { createBadgenHandler, PathArgs, BadgenError } from 'libs/create-badgen-handler-next'
import { coverageColor, millify, version } from 'libs/utils'
import queryHTML from 'libs/query-html'

type DependentsType = 'REPOSITORY' | 'PACKAGE'

Expand Down Expand Up @@ -297,7 +298,7 @@ const makeRepoQuery = (topic, owner, repo, restArgs) => {
case 'last-commit':
queryBody = `
${
restArgs.ref ?
restArgs.ref ?
`branch: ref(qualifiedName: "${restArgs.ref}")` :
'defaultBranchRef'
} {
Expand Down Expand Up @@ -364,10 +365,20 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
color: 'blue'
}
case 'stars':
const { stargazers_count } = await meta({ owner, repo })
// const { stargazers_count } = await meta({ owner, repo })
const starsElem = await queryHTML(`https://github.com/${owner}/${repo}`, '#repo-stars-counter-star')
const stargazers_count = starsElem?.innerText

if (stargazers_count === undefined) {
throw new BadgenError({
status: 'not found',
message: `Could not find stargazers_count for ${owner}/${repo}`
})
}

return {
subject: topic,
status: millify(stargazers_count),
status: stargazers_count,
color: 'blue'
}
case 'license':
Expand Down Expand Up @@ -451,7 +462,7 @@ async function repoStats ({topic, owner, repo, ...restArgs}: PathArgs) {
return {
subject: topic,
status: millify(
result.branch ?
result.branch ?
result.branch.target.history.totalCount :
result.defaultBranchRef.target.history.totalCount
),
Expand Down