MCPcopy
hub / github.com/sallar/github-contributions-chart / fetchDataForYear

Function fetchDataForYear

src/utils/api/fetch.js:36–95  ·  view source on GitHub ↗
(url, year, format)

Source from the content-addressed store, hash-verified

34}
35
36async function fetchDataForYear(url, year, format) {
37 const data = await fetch(`https://github.com${url}`, {
38 headers: {
39 "x-requested-with": "XMLHttpRequest"
40 }
41 });
42 const $ = cheerio.load(await data.text());
43 const $days = $(
44 "table.ContributionCalendar-grid td.ContributionCalendar-day"
45 );
46
47 const contribText = $(".js-yearly-contributions h2")
48 .text()
49 .trim()
50 .match(/^([0-9,]+)\s/);
51 let contribCount;
52 if (contribText) {
53 [contribCount] = contribText;
54 contribCount = parseInt(contribCount.replace(/,/g, ""), 10);
55 }
56
57 return {
58 year,
59 total: contribCount || 0,
60 range: {
61 start: $($days.get(0)).attr("data-date"),
62 end: $($days.get($days.length - 1)).attr("data-date")
63 },
64 contributions: (() => {
65 const parseDay = (day, index) => {
66 const $day = $(day);
67 const date = $day
68 .attr("data-date")
69 .split("-")
70 .map((d) => parseInt(d, 10));
71 const color = COLOR_MAP[$day.attr("data-level")];
72 const value = {
73 date: $day.attr("data-date"),
74 count: index === 0 ? contribCount : 0,
75 color,
76 intensity: $day.attr("data-level") || 0
77 };
78 return { date, value };
79 };
80
81 if (format !== "nested") {
82 return $days.get().map((day, index) => parseDay(day, index).value);
83 }
84
85 return $days.get().reduce((o, day, index) => {
86 const { date, value } = parseDay(day, index);
87 const [y, m, d] = date;
88 if (!o[y]) o[y] = {};
89 if (!o[y][m]) o[y][m] = {};
90 o[y][m][d] = value;
91 return o;
92 }, {});
93 })()

Callers 1

fetchDataForAllYearsFunction · 0.85

Calls 1

parseDayFunction · 0.85

Tested by

no test coverage detected