feat: Initial Commit

Signed-off-by: Avior <github@avior.me>
This commit is contained in:
Florian Bouillon 2022-12-07 12:58:31 +01:00
commit abfd6d8445
Signed by: Florian Bouillon
GPG Key ID: BEEAF3722D0EBF64
14 changed files with 2995 additions and 0 deletions

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
* text=auto eol=lf
*.png binary
*.jpg binary
*.jpeg binary

BIN
.github/codestats.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 171 KiB

BIN
.github/prusaslicer.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 103 KiB

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

87
3d.scad Normal file
View File

@ -0,0 +1,87 @@
// -- TO CHANGE START -- //
// do not chang it yourself
jan = [6433, 12574, 12013, 14896, 6242, 12306, 11732, 22842, 25780, 7375, 1030, 2676, 8857, 7948, 11429, 21389, 13025, 5335, 8942, 4457, 14171, 9951, 10766, 7780, 29865, 24567, 7702, 24382, 17697, 12100, 16698];
feb = [21093, 2887, 10773, 10345, 5897, 6255, 20849, 17071, 17168, 8504, 27323, 33772, 16624, 16915, 14535, 10766, 24339, 14177, 24986, 13760, 47418, 59739, 19918, 12892, 20671, 13143, 9618, 9389, 0, 0];
mar = [23643, 29897, 23874, 26867, 20092, 8363, 6073, 10425, 26663, 111087, 30647, 19681, 19344, 15507, 17636, 12541, 7369, 17568, 17077, 15472, 9506, 6338, 8851, 21544, 24243, 7023, 5401, 7157, 20584, 23584, 16505];
apr = [18005, 20973, 15606, 31164, 19681, 6483, 9336, 19675, 16235, 20264, 24966, 5373, 15940, 11196, 31502, 19592, 12128, 5567, 4127, 16426, 12003, 9349, 30876, 16646, 17918, 10468, 29463, 17615, 33916, 26946];
may = [7170, 23252, 9804, 31352, 15586, 14805, 11428, 1109, 17412, 10792, 15063, 26795, 21759, 16454, 6553, 4298, 18480, 12895, 9416, 15555, 11009, 8964, 12185, 14078, 13741, 12137, 16978, 9225, 5745, 11983, 16882];
jun = [18025, 9358, 14984, 9903, 8786, 9800, 4640, 10981, 243, 9525, 23704, 6319, 5829, 15594, 27113, 21561, 12550, 7705, 5961, 17777, 5608, 23655, 15029, 6724, 17000, 10188, 656, 2250, 15440, 14002];
jul = [28390, 11561, 7373, 6742, 17094, 29206, 11565, 8060, 14140, 30700, 24018, 20970, 12356, 23407, 15595, 18958, 27610, 22698, 14382, 18506, 11033, 26381, 15258, 7264, 16294, 20051, 7199, 3656, 9122, 12367, 7399];
aug = [37781, 16170, 5423, 7281, 625, 17514, 14275, 11376, 4164, 6922, 12616, 14079, 5546, 10987, 0, 2947, 16041, 9591, 8883, 8308, 10195, 12063, 13868, 8181, 11185, 19938, 9788, 6687, 12555, 22733, 5933];
sep = [8156, 14355, 7410, 7958, 5638, 1974, 7015, 3924, 559, 8996, 19943, 28816, 26180, 18516, 9069, 14086, 7128, 11096, 16909, 10968, 15450, 7919, 10128, 7433, 3533, 12644, 25574, 30150, 21302, 10383];
oct = [7109, 11067, 13580, 24341, 10774, 11742, 19973, 23626, 30664, 9417, 16783, 9260, 14108, 9489, 4830, 28002, 21885, 16954, 15868, 16902, 19634, 22134, 24960, 11625, 10090, 13084, 20795, 8160, 11223, 5578, 1741];
nov = [0, 13537, 30169, 11276, 6862, 13438, 10021, 14033, 11938, 10941, 3634, 23317, 33661, 22821, 30040, 13944, 13980, 10466, 8872, 9020, 10166, 37281, 31896, 15089, 15184, 14277, 8070, 13321, 11396, 9223];
dec = [8826, 11600, 5676, 8647, 8214, 26350, 12609, 9070, 21900, 17233, 11803, 17161, 12652, 2534, 5066, 26487, 27623, 21798, 17840, 11337, 3662, 9396, 16589, 242, 0, 0, 2674, 1892, 98, 10846, 9384];
max = 111087;
min = 0;
text = "Aviortheking";
// -- TO CHANGE END -- //
maxHeight = 100;
spacing = 0;
socleHeight = 20;
textHeight = 10;
minValue = 0;
module generateMonth(month, offset = 0) {
for (index = [0 : len(month) -1 ]) {
it = month[index];
translate([10 + (10 + spacing) * index,10 + (10 + spacing) * offset, socleHeight - 1]) {
cube([10,10,it < minValue ? 0 : it * maxHeight / max + 1]);
}
}
}
union() {
difference() {
cube([330, 140, socleHeight]);
translate([5,1.9,(socleHeight - textHeight) / 2]) {
rotate([90,0,0]) {
linear_extrude(2)
text(text, size=textHeight);
}
}
}
for (monthIndex = [ 0 : 11 ]) {
if (monthIndex == 0) {
generateMonth(jan, monthIndex);
}
if (monthIndex == 1) {
generateMonth(feb, monthIndex);
}
if (monthIndex == 2) {
generateMonth(mar, monthIndex);
}
if (monthIndex == 3) {
generateMonth(apr, monthIndex);
}
if (monthIndex == 4) {
generateMonth(may, monthIndex);
}
if (monthIndex == 5) {
generateMonth(jun, monthIndex);
}
if (monthIndex == 6) {
generateMonth(jul, monthIndex);
}
if (monthIndex == 7) {
generateMonth(aug, monthIndex);
}
if (monthIndex == 8) {
generateMonth(sep, monthIndex);
}
if (monthIndex == 9) {
generateMonth(oct, monthIndex);
}
if (monthIndex == 10) {
generateMonth(nov, monthIndex);
}
if (monthIndex == 11) {
generateMonth(dec, monthIndex);
}
}
}

37
Dockerfile Normal file
View File

@ -0,0 +1,37 @@
#########
# Build #
#########
# openscad doesn't seems to work on Alpine Linux :(
# FROM docker.io/node:alpine as IMAGE
FROM docker.io/node:latest as IMAGE
# External deps (for node-gyp add: "python3 make g++")
# git is used to install the npm packages with git deps
RUN apt-get update &&\
apt-get install git openscad -y
# run as non root user
USER node
# go to user repository
WORKDIR /home/node
# Add package json
ADD --chown=node:node package.json package-lock.json ./
# install dependencies from package lock
RUN npm ci
# Add project files
ADD --chown=node:node . .
# build
RUN npm run build
RUN npm ci --omit=dev
# Expose port
EXPOSE 3000
# run it !
CMD ["npm", "run", "start"]

9
LICENSE Normal file
View File

@ -0,0 +1,9 @@
MIT License
Copyright (c) 2022 Florian BOUILLON
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

27
README.md Normal file
View File

@ -0,0 +1,27 @@
# Code::Stats Skyline
Tool to generate an STL using your Codestats Total XPs
[Based on Github's Skyline](skyline.github.com)
[Idea by @ColinM#7415](https://discord.com/channels/510060567298310144/510060567298310146/1049790442964783114) on the [Code::Stats Discord](https://discord.gg/gyzRfjc)
| Source | Result |
|------------------------------|--------------------------------|
| ![](./.github/codestats.png) | ![](./.github/prusaslicer.png) |
## How to generate
Go to the following URL and appends your username like below
```
https://codestats-skyline.avior.me/?username=Aviortheking
```
It will generate and allows you to download the generated STL!
## Deploy the tool yourself!
You can use the Dockerfile to deploy the tool yourself!
_Code will be refactored as it has been done quickly :p_

26
index.ts Normal file
View File

@ -0,0 +1,26 @@
import express from 'express'
import http from 'http'
import { generateThingy } from './main'
const app = express()
app.get('/', async (req, res) => {
const { username } = req.query
if (!username) {
res.status(400).send('Error: missing username parameter in query')
return
}
const bfr = await generateThingy(username as string)
res.setHeader('Content-Disposition', `attachment; filename=${username}.stl`)
res.status(200).send(bfr)
})
const server = http
.createServer(app)
const port = process.env.PORT ? parseInt(process.env.PORT) : 3000
server.listen(port, '0.0.0.0')
console.log(`Server started on port ${port}!`)

13
interfaces.d.ts vendored Normal file
View File

@ -0,0 +1,13 @@
export interface XP {
new_xps: number
xps: number
}
export interface CodeStatsJSON {
dates: Record<`${number}-${number}-${number}`, number>
languages: Record<string, XP>
machines: Record<string, XP>
new_xp: number
total_xp: number
user: string
}

106
main.ts Normal file
View File

@ -0,0 +1,106 @@
import { objectLoop, objectValues } from '@dzeio/object-util'
import fetch from 'node-fetch'
import { promises as fs } from 'fs'
import { CodeStatsJSON } from './interfaces'
// @ts-expect-error no typing for this lib
import nodescad from 'nodescad'
const months = [
'',
'jan',
'feb',
'mar',
'apr',
'may',
'jun',
'jul',
'aug',
'sep',
'oct',
'nov',
'dec',
]
async function exists(path: string) {
try {
await fs.access(path)
return true
} catch {
return false
}
}
export const generateThingy = async (username: string) => {
const res = await fetch(`https://codestats.net/api/users/${username}`)
const data = await res.json() as CodeStatsJSON
const correctedData: Record<string, Array<number>> = {}
for (const month of months) {
if (month === '') {
continue
}
correctedData[month] = Array(31).fill(0)
}
objectLoop(data.dates, (value, key) => {
const [yearTxt, monthTxt, dayTxt] = key.split('-')
// if we want to add a way to filter by year we can by using the `_` var
const [_, month, day] = [parseInt(yearTxt), parseInt(monthTxt), parseInt(dayTxt)]
if (!(months[month] in correctedData)) {
correctedData[months[month]] = []
}
if (!(day in correctedData[months[month]])) {
correctedData[months[month]][day] = 0
}
correctedData[months[month]][day] += value
})
const max = objectValues(correctedData).reduce((p, c) => {
const monthMax = Math.max(...c)
return monthMax > p ? monthMax : p
}, 0)
const min = objectValues(correctedData).reduce((p, c) => {
const monthMax = Math.min(...c)
return monthMax < p ? monthMax : p
}, 0)
let final = ''
objectLoop(correctedData, (value, key) => {
final += `${key} = [${value.slice(1).join(', ')}];\n`
})
final += `max = ${max};\n`
final += `min = ${min};\n`
final += `text = "${username}";`
console.log('Output:')
console.log(final)
// handle prod file
let cadPath = './3d.scad'
if (!await exists(cadPath)) {
cadPath = '../3d.scad'
}
const file = await fs.readFile(cadPath, 'utf-8')
const modified = file.replace(/\/\/.+\/\//gs, final)
// await fs.writeFile('output.scad', file.replace(/\/\/.+\/\//gs, final))
const stl = await new Promise<any>((res, rej) => {
nodescad.render({
input: modified
}, (err: Error, result: any) => {
if (err) {
rej(err)
return
}
res(result)
})
})
return stl.buffer as Buffer
}

2652
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

23
package.json Normal file
View File

@ -0,0 +1,23 @@
{
"name": "codestats-city",
"main": "dist/index.js",
"scripts": {
"dev": "ts-node-dev --respawn --transpile-only --clear --quiet index.ts",
"build": "tsc --project tsconfig.json",
"start": "node dist/index.js"
},
"dependencies": {
"@dzeio/config": "^1.1.8",
"@dzeio/object-util": "^1.4.2",
"express": "^4.18.2",
"node-fetch": "^2",
"nodescad": "^1.1.0",
"typescript": "^4.9.3"
},
"devDependencies": {
"@types/express": "^4.17.14",
"@types/node-fetch": "^2",
"ts-node": "^10.9.1",
"ts-node-dev": "^2.0.0"
}
}

10
tsconfig.json Normal file
View File

@ -0,0 +1,10 @@
{
"extends": "@dzeio/config/tsconfig.base.json",
"compilerOptions": {
"outDir": "dist",
"baseUrl": "./"
},
"exclude": [
"dist"
]
}