diff --git a/packages/github-user-contribution/__tests__/getGithubUserContribution.spec.ts b/packages/github-user-contribution/__tests__/getGithubUserContribution.spec.ts new file mode 100644 index 0000000..d8eb2ae --- /dev/null +++ b/packages/github-user-contribution/__tests__/getGithubUserContribution.spec.ts @@ -0,0 +1,14 @@ +import { getGithubUserContribution } from ".."; + +it("should get user contribution", async () => { + const { cells, colorScheme } = await getGithubUserContribution("platane"); + + expect(cells).toBeDefined(); + expect(colorScheme).toEqual([ + "#ebedf0", + "#9be9a8", + "#40c463", + "#30a14e", + "#216e39", + ]); +}); diff --git a/packages/github-user-contribution/index.ts b/packages/github-user-contribution/index.ts new file mode 100644 index 0000000..5341ac5 --- /dev/null +++ b/packages/github-user-contribution/index.ts @@ -0,0 +1,61 @@ +// import * as https from "https"; + +// @ts-ignore +// import * as cheerio from "cheerio"; + +import { JSDOM } from "jsdom"; + +export const getGithubUserContribution = async (userName: string) => { + // const content: string = await new Promise((resolve, reject) => { + // const req = https.request(`https://github.com/${userName}`, (res) => { + // let data = ""; + + // res.on("error", reject); + // res.on("data", (chunk) => (data += chunk)); + // res.on("end", () => resolve(data)); + // }); + + // req.on("error", reject); + // req.end(); + // }); + + // const dom = new JSDOM(content); + + const dom = await JSDOM.fromURL(`https://github.com/${userName}`); + + const colorScheme = Array.from( + dom.window.document.querySelectorAll(".legend > li") + ).map( + (element) => + element.getAttribute("style")?.match(/background\-color: +(#\w+)/)?.[1]! + ); + + const cells = Array.from( + dom.window.document.querySelectorAll(".js-calendar-graph-svg > g > g") + ) + .map((column, x) => + Array.from(column.querySelectorAll("rect")).map((element, y) => ({ + x, + y, + count: element.getAttribute("data-count"), + date: element.getAttribute("data-date"), + color: element.getAttribute("fill"), + k: colorScheme.indexOf(element.getAttribute("fill")!), + })) + ) + .flat(); + + return { colorScheme, cells }; +}; + +type ThenArg = T extends PromiseLike ? U : T; + +export type Cell = ThenArg< + ReturnType +>["cells"][number]; + +// "#ebedf0"; +// "#9be9a8"; +// "#40c463"; +// "#30a14e"; +// "#216e39"; diff --git a/packages/github-user-contribution/package.json b/packages/github-user-contribution/package.json new file mode 100644 index 0000000..18e684f --- /dev/null +++ b/packages/github-user-contribution/package.json @@ -0,0 +1,10 @@ +{ + "name": "@snk/github-user-contribution", + "version": "1.0.0", + "dependencies": { + "jsdom": "16.3.0" + }, + "devDependencies": { + "@types/jsdom": "16.2.3" + } +}