-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitHub.js
39 lines (31 loc) · 1.27 KB
/
GitHub.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const puppeteer = require("puppeteer");
class GitHub {
async fetchActivity(username, days) {
const browser = await puppeteer.launch({
executablePath: '/usr/bin/chromium-browser',
args: ['--disable-gpu', '--disable-setuid-sandbox', '--no-sandbox', '--no-zygote']
});
const page = await browser.newPage();
await page.goto(`https://github.com/${username}`);
// Wait for the elements to load
await page.waitForSelector('.ContributionCalendar-day');
// Extract the data-date attribute from each element
const data = await page.evaluate(() => {
const elements = Array.from(document.querySelectorAll('.ContributionCalendar-day'));
const map = {};
elements.forEach(element => {
const date = element.getAttribute('data-date');
const level = element.getAttribute('data-level');
if (date) map[date] = level;
});
return map;
});
// Convert the object into an array of key-value pairs and sort it by the date
const sortedData = Object.entries(data).sort((a, b) => new Date(a[0]) - new Date(b[0]));
// Keep only the most recent x days
const recentData = sortedData.slice(-Math.abs(days));
await browser.close();
return recentData.map(entry => entry[1]);
}
}
module.exports = GitHub