hubspy
Version:
A package to extract github metadata and total contributions for a github user.
62 lines (61 loc) • 2.83 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import * as cheerio from 'cheerio';
export default function GetYearWiseContributions(name) {
return __awaiter(this, void 0, void 0, function* () {
const url = `https://github.com/${name}?action=show&controller=profiles&tab=contributions&user_id=${name}`;
const data = yield fetch(url, {
headers: {
connection: "keep-alive",
"X-Requested-With": "XMLHttpRequest",
},
});
if (!data.ok) {
throw new Error(`Error in getting year data for ${name} ${data.status} ${url} ${data.statusText}`);
}
let content = yield data.text();
let $ = cheerio.load(content);
const activeYears = [];
$("a[id*='year-link']").each((i, year) => {
let value = $(year).contents();
activeYears.push(Number(value));
});
const yearWiseContributions = [];
const yearWisePromise = [];
for (let year of activeYears) {
const params = new URLSearchParams({
from: `${year}-01-01`,
to: `${year}-12-31`
});
const data = yearWisePromise.push(fetch(`https://github.com/users/${name}/contributions?${params.toString()}`, {
headers: {
connection: "keep-alive",
"X-Requested-With": "XMLHttpRequest",
},
}));
}
const allData = yield Promise.all(yearWisePromise);
let i = 0;
for (let data of allData) {
if (!data.ok) {
throw new Error(`Error in getting contribution data for ${name} ${data.status}: ${data.statusText}`);
}
content = yield data.text();
$ = cheerio.load(content);
let value = $("h2[class='f4 text-normal mb-2']").text();
let contrib = value.trim().replace("/n", "").split(" ");
yearWiseContributions.push({
year: activeYears[i++],
contributions: parseInt(contrib[0].replace(/,/g, ''), 10)
});
}
return yearWiseContributions;
});
}