✨ update contribution parser
This commit is contained in:
@@ -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("</svg>")[0];
|
||||
.split(`aria-describedby="contribution-graph-description"`)[1]
|
||||
.split("<tbody>")[1]
|
||||
.split("</tbody>")[0];
|
||||
|
||||
let x = 0;
|
||||
let lastYAttribute = 0;
|
||||
const cells = block.split("</tr>").flatMap((inside, y) =>
|
||||
inside.split("</td>").flatMap((m) => {
|
||||
const date = m.match(/data-date="([^"]+)"/)?.[1];
|
||||
|
||||
const rects = Array.from(block.matchAll(/<rect[^>]*>[^<]*<\/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;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user