-
Notifications
You must be signed in to change notification settings - Fork 1
/
contentGenerator.js
123 lines (104 loc) · 3.85 KB
/
contentGenerator.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
const dataCache = require('./dataCache')
const {redColor,yellowColor,greenColor} = require('./colorSetting.js')
const fs = require('fs');
const { JSDOM } = require('jsdom');
const svgConvert = require('./svgConvertion.js')
//get Information of pollen
var pollenData = undefined;
var treeValue = undefined;
var grassValue = undefined;
var weedsValue = undefined;
var time = undefined;
var twitterMessage = undefined;
var treeTextValue = undefined;
var grassTextValue = undefined;
var weedsTextValue = undefined;
const getInfo = (() => {
pollenData = dataCache.fetch();
time = new Date(pollenData.Time).toISOString().slice(0, 10);
treeValue = pollenData.values.treeIndex;
grassValue = pollenData.values.grassIndex;
weedsValue = pollenData.values.weedIndex;
})
const generate = ((resolve, reject) => {
getInfo();
console.log("Started editing image");
const svgFile = fs.readFileSync('./Post.svg', 'utf-8');
const dom = new JSDOM(svgFile, { contentType: 'image/svg+xml' });
const document = dom.window.document;
//Get style ID
const treeElement1 = document.getElementById('linearGradient8213');
const treeElement2 = document.getElementById('stop8209');
const grassElement1 = document.getElementById('linearGradient8316');
const grassElement2 = document.getElementById('stop8312');
const weedsElement1 = document.getElementById('linearGradient8308');
const weedsElement2 = document.getElementById('stop8304');
//Get Text ID
const treeText = document.getElementById('tspan8344');
const grassText = document.getElementById('tspan8348');
const weedsText = document.getElementById('tspan8352');
//Tree
if(treeValue >= 0 && treeValue <= 1){
treeElement1.style = greenColor;
treeElement2.style = greenColor;
treeText.textContent = "Low";
treeTextValue = "Low";
}else if(treeValue >= 2 && treeValue <= 3){
treeElement1.style = yellowColor;
treeElement2.style = yellowColor;
treeText.textContent = "Medium";
treeTextValue = "Medium";
}else{
treeElement1.style = redColor;
treeElement2.style = redColor;
treeText.textContent = "High";
treeTextValue = "High";
}
// grass
if(grassValue >= 0 && grassValue <= 1){
grassElement1.style = greenColor;
grassElement2.style = greenColor;
grassText.textContent = "Low";
grassTextValue = "Low";
}else if(grassValue >= 2 && grassValue <= 3){
grassElement1.style = yellowColor;
grassElement2.style = yellowColor;
grassText.textContent = "Medium";
grassTextValue = "Medium";
}else{
grassElement1.style = redColor;
grassElement2.style = redColor;
grassText.textContent = "High";
grassTextValue = "High";
}
//weeds
if(weedsValue >= 0 && weedsValue <= 1){
weedsElement1.style = greenColor;
weedsElement2.style = greenColor;
weedsText.textContent = "Low";
weedsTextValue = "Low";
}else if(weedsValue >= 2 && weedsValue <= 3){
weedsElement1.style = yellowColor;
weedsElement2.style = yellowColor;
weedsText.textContent = "Medium";
weedsTextValue = "Medium";
}else{
weedsElement1.style = redColor;
weedsElement2.style = redColor;
weedsText.textContent = "High";
weedsTextValue = "High";
}
let updatedSvgFile = dom.serialize();
fs.writeFileSync('Post.svg', updatedSvgFile);
console.log("Image editing finished!");
//convert Post.svg to Post.jpg
new Promise(svgConvert.svgConvert).then(() => {
twitterMessage = `Pollen levels in Toronto ${time}\nTree: ${treeTextValue}\nGrass: ${grassTextValue}\nWeeds: ${weedsTextValue}`;
resolve(twitterMessage);
}).catch((error) => {
reject(error);
});
})
module.exports = {
generate
}