-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
299 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const cheerio = require("cheerio"); | ||
const _ = require("lodash"); | ||
|
||
const COLOR_MAP = { | ||
0: "#ebedf0", | ||
1: "#9be9a8", | ||
2: "#40c463", | ||
3: "#30a14e", | ||
4: "#216e39", | ||
}; | ||
|
||
async function fetchYears(username) { | ||
const data = await fetch( | ||
`https://github.com/${username}?tab=contributions`, | ||
{ | ||
headers: { | ||
"x-requested-with": "XMLHttpRequest", | ||
}, | ||
}, | ||
); | ||
const body = await data.text(); | ||
const $ = cheerio.load(body); | ||
return $(".js-year-link") | ||
.get() | ||
.map((a) => { | ||
const $a = $(a); | ||
const href = $a.attr("href"); | ||
const githubUrl = new URL(`https://github.com${href}`); | ||
githubUrl.searchParams.set("tab", "contributions"); | ||
const formattedHref = `${githubUrl.pathname}${githubUrl.search}`; | ||
|
||
return { | ||
href: formattedHref, | ||
text: $a.text().trim(), | ||
}; | ||
}); | ||
} | ||
|
||
async function fetchDataForYear(url, year, format) { | ||
const data = await fetch(`https://github.com${url}`, { | ||
headers: { | ||
"x-requested-with": "XMLHttpRequest", | ||
}, | ||
}); | ||
const $ = cheerio.load(await data.text()); | ||
const $days = $( | ||
"table.ContributionCalendar-grid td.ContributionCalendar-day", | ||
); | ||
|
||
const contribText = $(".js-yearly-contributions h2") | ||
.text() | ||
.trim() | ||
.match(/^([0-9,]+)\s/); | ||
let contribCount; | ||
if (contribText) { | ||
[contribCount] = contribText; | ||
contribCount = parseInt(contribCount.replace(/,/g, ""), 10); | ||
} | ||
|
||
return { | ||
year, | ||
total: contribCount || 0, | ||
range: { | ||
start: $($days.get(0)).attr("data-date"), | ||
end: $($days.get($days.length - 1)).attr("data-date"), | ||
}, | ||
contributions: (() => { | ||
const parseDay = (day, index) => { | ||
const $day = $(day); | ||
const date = $day | ||
.attr("data-date") | ||
.split("-") | ||
.map((d) => parseInt(d, 10)); | ||
const color = COLOR_MAP[$day.attr("data-level")]; | ||
const value = { | ||
date: $day.attr("data-date"), | ||
count: index === 0 ? contribCount : 0, | ||
color, | ||
intensity: $day.attr("data-level") || 0, | ||
}; | ||
return { date, value }; | ||
}; | ||
|
||
if (format !== "nested") { | ||
return $days.get().map((day, index) => parseDay(day, index).value); | ||
} | ||
|
||
return $days.get().reduce((o, day, index) => { | ||
const { date, value } = parseDay(day, index); | ||
const [y, m, d] = date; | ||
if (!o[y]) o[y] = {}; | ||
if (!o[y][m]) o[y][m] = {}; | ||
o[y][m][d] = value; | ||
return o; | ||
}, {}); | ||
})(), | ||
}; | ||
} | ||
|
||
async function fetchDataForAllYears(username, format) { | ||
const years = await fetchYears(username); | ||
return Promise.all( | ||
years.map((year) => fetchDataForYear(year.href, year.text, format)), | ||
).then((resp) => { | ||
return { | ||
years: (() => { | ||
const obj = {}; | ||
const arr = resp.map((year) => { | ||
const { contributions, ...rest } = year; | ||
_.setWith(obj, [rest.year], rest, Object); | ||
return rest; | ||
}); | ||
return format === "nested" ? obj : arr; | ||
})(), | ||
contributions: | ||
format === "nested" | ||
? resp.reduce((acc, curr) => _.merge(acc, curr.contributions)) | ||
: resp | ||
.reduce((list, curr) => [...list, ...curr.contributions], []) | ||
.sort((a, b) => { | ||
if (a.date < b.date) return 1; | ||
else if (a.date > b.date) return -1; | ||
return 0; | ||
}), | ||
}; | ||
}); | ||
} | ||
|
||
module.exports = { fetchDataForAllYears }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.