badge.js (1956B)
1 // api/badge.js 2 export default async function handler(req, res) { 3 res.setHeader('Content-Type', 'image/svg+xml'); 4 res.setHeader('Cache-Control', 'no-cache'); 5 6 try { 7 const token = process.env.GITHUB_TOKEN; 8 const repo = 'NodeX-AR/Pytml'; 9 const path = 'count.json'; 10 11 // Get the current count from GitHub 12 const getRes = await fetch( 13 `https://api.github.com/repos/${repo}/contents/${path}`, 14 { 15 headers: { 16 'Authorization': `token ${token}`, 17 'Accept': 'application/vnd.github.v3+json' 18 } 19 } 20 ); 21 22 let count = 0; 23 if (getRes.ok) { 24 const data = await getRes.json(); 25 const content = Buffer.from(data.content, 'base64').toString('utf-8'); 26 count = parseInt(content) || 0; 27 } 28 29 const svg = ` 30 <svg xmlns="http://www.w3.org/2000/svg" width="160" height="20"> 31 <linearGradient id="b" x2="0" y2="100%"> 32 <stop offset="0" stop-color="#bbb" stop-opacity=".1"/> 33 <stop offset="1" stop-opacity=".1"/> 34 </linearGradient> 35 <mask id="a"> 36 <rect width="160" height="20" rx="3" fill="#fff"/> 37 </mask> 38 <g mask="url(#a)"> 39 <rect width="80" height="20" fill="#555"/> 40 <rect x="80" width="80" height="20" fill="#007ec6"/> 41 <rect width="160" height="20" fill="url(#b)"/> 42 </g> 43 <g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"> 44 <text x="40" y="14">pytml loads</text> 45 <text x="120" y="14">${count}</text> 46 </g> 47 </svg> 48 `; 49 50 res.status(200).send(svg); 51 52 } catch (error) { 53 res.status(200).send(` 54 <svg xmlns="http://www.w3.org/2000/svg" width="160" height="20"> 55 <rect width="160" height="20" fill="#555"/> 56 <text x="10" y="14" fill="white" font-family="monospace" font-size="11"> 57 pytml loads: error 58 </text> 59 </svg> 60 `); 61 } 62 }