♻️ drop cheerio

This commit is contained in:
platane
2022-04-21 19:23:29 +02:00
committed by Platane
parent 40b26d0110
commit a81c1bcc97

View File

@@ -1,5 +1,4 @@
import fetch from "node-fetch"; import fetch from "node-fetch";
import * as cheerio from "cheerio";
import { formatParams, Options } from "./formatParams"; import { formatParams, Options } from "./formatParams";
/** /**
@@ -40,74 +39,39 @@ export const getGithubUserContribution = async (
}; };
const parseUserPage = (content: string) => { const parseUserPage = (content: string) => {
const $ = cheerio.load(content); // take roughly the svg block
const block = content
.split(`class="js-calendar-graph-svg"`)[1]
.split("</svg>")[0];
// let x = 0;
// parse cells let lastYAttribute = 0;
const rawCells = $(".js-calendar-graph rect[data-count]")
.toArray()
.map((x) => {
const level = +x.attribs["data-level"];
const count = +x.attribs["data-count"];
const date = x.attribs["data-date"];
return { const rects = Array.from(block.matchAll(/<rect[^>]*>/g)).map(([m]) => {
svgPosition: getSvgPosition(x), const date = m.match(/data-date="([^"]+)"/)![1];
level, const count = +m.match(/data-count="([^"]+)"/)![1];
count, const level = +m.match(/data-level="([^"]+)"/)![1];
date, const yAttribute = +m.match(/y="([^"]+)"/)![1];
};
});
const xMap: Record<number, true> = {}; if (lastYAttribute > yAttribute) x++;
const yMap: Record<number, true> = {};
rawCells.forEach(({ svgPosition: { x, y } }) => { lastYAttribute = yAttribute;
xMap[x] = true;
yMap[y] = true; return { date, count, level, x, yAttribute };
}); });
const xRange = Object.keys(xMap) const yAttributes = Array.from(
.map((x) => +x) new Set(rects.map((c) => c.yAttribute)).keys()
.sort((a, b) => +a - +b); ).sort();
const yRange = Object.keys(yMap)
.map((x) => +x)
.sort((a, b) => +a - +b);
const cells = rawCells.map(({ svgPosition, ...c }) => ({ const cells = rects.map(({ yAttribute, ...c }) => ({
y: yAttributes.indexOf(yAttribute),
...c, ...c,
x: xRange.indexOf(svgPosition.x),
y: yRange.indexOf(svgPosition.y),
})); }));
return cells; return cells;
}; };
// returns the position of the svg elements, accounting for it's transform and it's parent transform
// ( only accounts for translate transform )
const getSvgPosition = (
e: cheerio.Element | null
): { x: number; y: number } => {
if (!e || e.tagName === "svg") return { x: 0, y: 0 };
const p = getSvgPosition(e.parent as cheerio.Element);
if (e.attribs.x) p.x += +e.attribs.x;
if (e.attribs.y) p.y += +e.attribs.y;
if (e.attribs.transform) {
const m = e.attribs.transform.match(
/translate\( *([\.\d]+) *, *([\.\d]+) *\)/
);
if (m) {
p.x += +m[1];
p.y += +m[2];
}
}
return p;
};
export type Res = Awaited<ReturnType<typeof getGithubUserContribution>>; export type Res = Awaited<ReturnType<typeof getGithubUserContribution>>;
export type Cell = Res[number]; export type Cell = Res[number];