-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·143 lines (127 loc) · 3.79 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env node
const express = require('express')
const fs = require('fs')
const request = require('request')
const Jimp = require('jimp')
const printer = require('@thiagoelg/node-printer')
const cors = require('cors')
let rawdata = fs.readFileSync('creatures.json')
let cards = JSON.parse(rawdata);
let cardlist = []
cards.forEach(c => {
if(!cardlist[c.cmc]) {
cardlist[c.cmc] = []
}
if(c.scryfallId) {
cardlist[c.cmc].push(c)
}
})
if(!fs.existsSync(__dirname + "/pictures")) {
fs.mkdirSync(__dirname + "/pictures/")
}
if(!fs.existsSync(__dirname + "/dithered")) {
fs.mkdirSync(__dirname + "/dithered/")
}
const app = express()
app.use(cors())
app.get('/card/:cmc', (req, res) => {
let card = {}
if(cardlist[req.params.cmc]) {
card = cardlist[req.params.cmc][Math.floor(Math.random()*cardlist[req.params.cmc].length)]
}
res.send(card)
})
app.get('/dithered/:scryFallId', (req, res) => {
getCard(req.params.scryFallId, (path) => path ? dither(req.params.scryFallId, path, (path) => res.sendFile(path)) : res.send({error: "card not found", id: req.params.scryFallId}))
})
app.get('/picture/:scryFallId', (req, res) => {
getCard(req.params.scryFallId, (path) => path ? res.sendFile(path): res.send({error: "card not found", id: req.params.scryFallId}))
})
app.get('/printers', (req, res) => {
res.send(printer.getPrinters())
})
app.get('/print/:scryFallId', (req, res) => {
if(printer.getPrinters().map(p => p.name).includes(req.query.printer)) {
getCard(req.params.scryFallId, (path) => printer.printFile({filename: path , type:'JPEG'}))
res.send({"printed":true})
}
else {
res.send({"printed":false})
}
})
const getCard = (scryFallId, cb) => {
const filePath = __dirname + "/pictures/"+scryFallId +".jpg"
if(!fs.existsSync(filePath)) {
let SFurl = "https://api.scryfall.com/cards/" + scryFallId;
request.get(SFurl, (err, response, body) => {
if(err) {
console.log("ERROR: (" + SFurl + ") " + err)
cb(null);
}
else {
let card = JSON.parse(body)
let uri = undefined
if(card.image_uris && card.image_uris.normal) {
uri = card.image_uris.normal
}
else if(card.card_faces
&& card.card_faces.length > 0
&& card.card_faces[0].image_uris
&& card.card_faces[0].image_uris.normal) {
uri = card.card_faces[0].image_uris.normal
}
if(uri) {
Jimp.read(uri)
.then(image => image.resize(230,320))
.then(image => image.write(filePath,
() => cb(filePath)
))
}
else {
console.log("Card not found from URL:" + SFurl);
cb(null);
}
}
})
}
else {
cb(filePath)
}
}
const server = app.listen(8081, () => {
console.log("Server started")
})
const dither = (scryFallId, cardPath, cb) => {
const filePath = __dirname + "/dithered/"+scryFallId +".jpg";
if(!fs.existsSync(filePath)) {
Jimp.read(cardPath).then(image =>
image.greyscale()).then(image => {
bits = image.bitmap.data.filter((value, index) => index % 4 == 0)
floydSteinberg(bits, image.bitmap.width, image.bitmap.height)
for(let i = 0; i<bits.length;i++) {
image.bitmap.data[i*4] = image.bitmap.data[i*4+1] = image.bitmap.data[i*4+2] = bits[i]
image.bitmap.data[i*4+3] = 255
}
image.write(filePath, () => cb(filePath))
})
}
else {
cb(filePath)
}
}
const floydSteinberg = (sb, w, h) => {
for(let i=0; i<h; i++) {
for(let j=0; j<w; j++) {
let ci = i*w+j;
let cc = sb[ci];
let rc = (cc<128?0:255);
let err = (cc-rc)>>3;
sb[ci] = rc;
if(j+1<w) sb[ci +1] += (err*7)>>4;
if(i+1==h) continue;
if(j >0) sb[ci+w-1] += (err*3)>>4;
sb[ci+w ] += (err*5)>>4;
if(j+1<w) sb[ci+w+1] += (err*1)>>4;
}
}
}