From 7233ec9e15299ebb04889bf625dda82cb69161de Mon Sep 17 00:00:00 2001 From: platane Date: Mon, 17 Jul 2023 22:10:14 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20update=20contribution=20parser?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/github-user-contribution/index.ts | 48 ++++++++++------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/packages/github-user-contribution/index.ts b/packages/github-user-contribution/index.ts index 9d9b9e5..33fcf81 100644 --- a/packages/github-user-contribution/index.ts +++ b/packages/github-user-contribution/index.ts @@ -39,40 +39,36 @@ export const getGithubUserContribution = async ( }; const parseUserPage = (content: string) => { - // take roughly the svg block + // take roughly the table block const block = content - .split(`class="js-calendar-graph-svg"`)[1] - .split("")[0]; + .split(`aria-describedby="contribution-graph-description"`)[1] + .split("")[1] + .split("")[0]; - let x = 0; - let lastYAttribute = 0; + const cells = block.split("").flatMap((inside, y) => + inside.split("").flatMap((m) => { + const date = m.match(/data-date="([^"]+)"/)?.[1]; - const rects = Array.from(block.matchAll(/]*>[^<]*<\/rect>/g)).map( - ([m]) => { - const date = m.match(/data-date="([^"]+)"/)![1]; - const level = +m.match(/data-level="([^"]+)"/)![1]; - const yAttribute = +m.match(/y="([^"]+)"/)![1]; + const literalLevel = m.match(/data-level="([^"]+)"/)?.[1]; + const literalX = m.match(/data-ix="([^"]+)"/)?.[1]; + const literalCount = m.match(/(No|\d+) contributions? on/)?.[1]; - const literalCount = m.match(/(No|\d+) contributions? on/)![1]; - const count = literalCount === "No" ? 0 : +literalCount; + if (date && literalLevel && literalX && literalCount) + return [ + { + x: +literalX, + y, - if (lastYAttribute > yAttribute) x++; + date, + count: +literalCount, + level: +literalLevel, + }, + ]; - lastYAttribute = yAttribute; - - return { date, count, level, x, yAttribute }; - } + return []; + }) ); - const yAttributes = Array.from( - new Set(rects.map((c) => c.yAttribute)).keys() - ).sort(); - - const cells = rects.map(({ yAttribute, ...c }) => ({ - y: yAttributes.indexOf(yAttribute), - ...c, - })); - return cells; };