update contribution parser

This commit is contained in:
platane
2023-07-17 22:10:14 +02:00
committed by Platane
parent 54dbbbf73d
commit 7233ec9e15

View File

@@ -39,40 +39,36 @@ export const getGithubUserContribution = async (
}; };
const parseUserPage = (content: string) => { const parseUserPage = (content: string) => {
// take roughly the svg block // take roughly the table block
const block = content const block = content
.split(`class="js-calendar-graph-svg"`)[1] .split(`aria-describedby="contribution-graph-description"`)[1]
.split("</svg>")[0]; .split("<tbody>")[1]
.split("</tbody>")[0];
let x = 0; const cells = block.split("</tr>").flatMap((inside, y) =>
let lastYAttribute = 0; inside.split("</td>").flatMap((m) => {
const date = m.match(/data-date="([^"]+)"/)?.[1];
const rects = Array.from(block.matchAll(/<rect[^>]*>[^<]*<\/rect>/g)).map( const literalLevel = m.match(/data-level="([^"]+)"/)?.[1];
([m]) => { const literalX = m.match(/data-ix="([^"]+)"/)?.[1];
const date = m.match(/data-date="([^"]+)"/)![1]; const literalCount = m.match(/(No|\d+) contributions? on/)?.[1];
const level = +m.match(/data-level="([^"]+)"/)![1];
const yAttribute = +m.match(/y="([^"]+)"/)![1];
const literalCount = m.match(/(No|\d+) contributions? on/)![1]; if (date && literalLevel && literalX && literalCount)
const count = literalCount === "No" ? 0 : +literalCount; return [
{
x: +literalX,
y,
if (lastYAttribute > yAttribute) x++; date,
count: +literalCount,
level: +literalLevel,
},
];
lastYAttribute = yAttribute; return [];
})
return { date, count, level, x, yAttribute };
}
); );
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; return cells;
}; };