commit a00d0135972c4f25a6b585a585e0d4a1a43f8655 Author: yutent Date: Wed Jun 7 19:01:53 2023 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f468967 --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode +*.env + +node_modules/ +data/logs/ +data/ssl/ +public/upload/ +package-lock.json \ No newline at end of file diff --git a/app.dev.yaml b/app.dev.yaml new file mode 100644 index 0000000..ce69c99 --- /dev/null +++ b/app.dev.yaml @@ -0,0 +1,16 @@ + +script: node ./app.js +cwd: ./ +watch: true +name: stats:23333 +ignore_watch: [data, public, package.json, package-lock.json, node_modules, .git, .gitignore, app.dev.yaml, app.yaml] +exec_mode: fork + +error_file: ./data/logs/error.log +out_file: ./data/logs/out.log +merge_logs: true +min_uptime: 60s +max_restarts: 1 +max_memory_restart: 300M +env: + NODE_ENV: development diff --git a/app.js b/app.js new file mode 100644 index 0000000..bb13df5 --- /dev/null +++ b/app.js @@ -0,0 +1,32 @@ +/** + * {} + * @author yutent + * @date 2023/06/07 18:01:50 + */ + +import Five from '@gm5/core' +import fs from 'iofs' + +const app = new Five() + +if (fs.isfile('./config.env')) { + // + fs.cat('./config.env') + .toString() + .split('\n') + .forEach(it => { + let tmp = it.trim() + if (tmp) { + tmp = tmp.split(': ') + process.env[tmp[0]] = tmp[1] + } + }) +} + +app.set({ + token: process.env.GITHUB_TOKEN +}) + +app.preload('./apps/') + +app.listen(23333) diff --git a/app.yaml b/app.yaml new file mode 100644 index 0000000..03b4591 --- /dev/null +++ b/app.yaml @@ -0,0 +1,16 @@ + +script: node ./app.js +cwd: ./ +watch: true +name: stats:23333 +ignore_watch: [data, public, package.json, package-lock.json, node_modules, .git, .gitignore, app.dev.yaml, app.yaml] +exec_mode: fork + +error_file: ./data/logs/error.log +out_file: ./data/logs/out.log +merge_logs: true +min_uptime: 60s +max_restarts: 1 +max_memory_restart: 300M +env: + NODE_ENV: production diff --git a/apps/api.js b/apps/api.js new file mode 100644 index 0000000..524b4c0 --- /dev/null +++ b/apps/api.js @@ -0,0 +1,74 @@ +/** + * {} + * @author yutent + * @date 2023/06/07 18:06:57 + */ + +import Controller from '@gm5/controller' +import { render } from '../lib/svg.js' + +export default class Github extends Controller { + __main__() { + // + } + + async toplangsAction() { + let { username, count = 6 } = this.request.get() + let token = this.context.get('token') + + let { data } = await fetch('https://api.github.com/graphql', { + headers: { + Authorization: 'Bearer ' + token, + 'Content-Type': 'application/json' + }, + method: 'post', + body: JSON.stringify({ + query: ` +query { + repositoryOwner(login: "${username}") { + repositories(ownerAffiliations: [OWNER, ORGANIZATION_MEMBER], isFork: false, first: 100){ + nodes { + name + languages(first: 10) { + edges { + size + node { + color + name + } + } + } + } + } + } +} + ` + }) + }).then(r => r.json()) + + let { nodes } = data.repositoryOwner.repositories + let dict = {} + let langs = [] + + nodes.forEach(it => { + for (let lang of it.languages.edges) { + if (dict[lang.node.name]) { + dict[lang.node.name].size += lang.size + } else { + dict[lang.node.name] = { size: lang.size, color: lang.node.color } + } + } + }) + + for (let k in dict) { + langs.push({ name: k, size: dict[k].size, color: dict[k].color }) + } + + langs.sort((a, b) => b.size - a.size) + + this.response.set('Content-Type', 'image/svg+xml') + this.response.end(render({})) + + // this.response.send(200, { langs: langs.slice(0, count) }) + } +} diff --git a/lib/svg.js b/lib/svg.js new file mode 100644 index 0000000..e02e35b --- /dev/null +++ b/lib/svg.js @@ -0,0 +1,35 @@ +/** + * {} + * @author yutent + * @date 2023/06/07 18:41:20 + */ + +function html(strs, ...vals) { + let output = '' + for (let it of strs) { + output += it + (vals.shift() || '') + } + return output +} + +export function render({ title = 'Most Used Languages' }) { + return html` + + + + ${title} + + + ` +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..7b12e5d --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "name": "github-stats", + "type": "module", + "dependencies": { + "@gm5/core": "^1.1.5" + } +}