diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..eea845d --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +GITHUB_TOKEN = "" # Github Personal Access Token +USERS = "" # Usernames separated by comma diff --git a/.gitignore b/.gitignore index 6704566..dd0893e 100644 --- a/.gitignore +++ b/.gitignore @@ -102,3 +102,14 @@ dist # TernJS port file .tern-port + +# Environment +.env + +# General +.DS_Store +.AppleDouble +.LSOverride +.idea +.vscode +package-lock.json diff --git a/index.js b/index.js new file mode 100644 index 0000000..2399a97 --- /dev/null +++ b/index.js @@ -0,0 +1,71 @@ +const fetch = require('node-fetch'); +const parallel = require('run-parallel') +require('dotenv').config() + + +var curr = new Date; +var first = curr.getDate() - curr.getDay(); +var last = first + 6; + +var firstday = new Date(curr.setDate(first)).toISOString(); +var lastday = new Date(curr.setDate(last)).toISOString(); + +async function getContributions(username) { + const headers = { + 'Authorization': `bearer ${process.env.GITHUB_TOKEN}`, + } + const body = { + "query": `query { + user(login: "${username}") { + name + contributionsCollection(from: "${firstday}", to: "${lastday}") { + contributionCalendar { + totalContributions + } + } + } + }` + } + const response = await fetch('https://api.github.com/graphql', { + method: 'POST', + body: JSON.stringify(body), + headers: headers + }) + const data = await response.json() + return data +} + + +const users = process.env.USERS.split(","); + +const callbacks = users.map(user => (callback) => { + getContributions(user).then(data => { + callback(null, data); + }) +}) + +function getData() { + parallel(callbacks, (err, results) => { + var data = []; + results.forEach(response => { + data.push({ + Name: response.data.user.name, + Contributions: response.data.user.contributionsCollection.contributionCalendar.totalContributions, + }); + }); + + data = data.sort(function (a, b) { + return b.Contributions - a.Contributions; + }) + console.clear(); + console.table(data); + console.log("Updated: " + (new Date())) + + }); +} + +getData(); +setInterval(() => { + getData(); +}, 10 * 1000) + diff --git a/package.json b/package.json new file mode 100644 index 0000000..225cb2e --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "github-ranking", + "version": "0.0.1", + "description": "GitHub Ranking", + "main": "index.js", + "dependencies": { + "dotenv": "^10.0.0", + "node-fetch": "^2.6.1", + "run-parallel": "^1.2.0" + }, + "devDependencies": {}, + "author": "gussrw", + "license": "MIT" +}