mirror of
				https://github.com/KevinMidboe/waka-box.git
				synced 2025-10-29 18:00:21 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			72 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| require("dotenv").config();
 | |
| const { WakaTimeClient, RANGE } = require("wakatime-client");
 | |
| const Octokit = require("@octokit/rest");
 | |
| 
 | |
| const {
 | |
|   GIST_ID: gistId,
 | |
|   GITHUB_TOKEN: githubToken,
 | |
|   WAKATIME_API_KEY: wakatimeApiKey
 | |
| } = process.env;
 | |
| 
 | |
| const wakatime = new WakaTimeClient(wakatimeApiKey);
 | |
| 
 | |
| const octokit = new Octokit({
 | |
|   auth: `token ${githubToken}`
 | |
| });
 | |
| 
 | |
| async function main() {
 | |
|   const stats = await wakatime.getMyStats({ range: RANGE.LAST_7_DAYS });
 | |
|   await updateGist(stats);
 | |
| }
 | |
| 
 | |
| async function updateGist(stats) {
 | |
|   let gist;
 | |
|   try {
 | |
|     gist = await octokit.gists.get({ gist_id: gistId });
 | |
|   } catch (error) {
 | |
|     console.error(`Unable to get gist\n${error}`);
 | |
|   }
 | |
| 
 | |
|   const lines = [];
 | |
|   for (let i = 0; i < 5; i++) {
 | |
|     const data = stats.data.languages[i];
 | |
|     const { name, percent, text: time } = data;
 | |
| 
 | |
|     const line = [
 | |
|       name.padEnd(11),
 | |
|       time.padEnd(14),
 | |
|       generateBarChart(percent, 12),
 | |
|       String(percent.toFixed(1)).padStart(5) + "%"
 | |
|     ];
 | |
| 
 | |
|     lines.push(line.join(" "));
 | |
|   }
 | |
| 
 | |
|   try {
 | |
|     // Get original filename to update that same file
 | |
|     const filename = Object.keys(gist.data.files)[0];
 | |
|     await octokit.gists.update({
 | |
|       gist_id: gistId,
 | |
|       files: {
 | |
|         [filename]: {
 | |
|           filename: `📊 Weekly development breakdown`,
 | |
|           content: lines.join("\n")
 | |
|         }
 | |
|       }
 | |
|     });
 | |
|   } catch (error) {
 | |
|     console.error(`Unable to update gist\n${error}`);
 | |
|   }
 | |
| }
 | |
| 
 | |
| function generateBarChart(percent, size) {
 | |
|   const empty = "░";
 | |
|   const full = "█";
 | |
|   const barsFull = Math.round(size * (percent / 100));
 | |
|   return full.repeat(barsFull).padEnd(size, empty);
 | |
| }
 | |
| 
 | |
| (async () => {
 | |
|   await main();
 | |
| })();
 |