-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
135 lines (117 loc) · 3.45 KB
/
index.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
const Twitter = require("twitter");
const http = require("http");
const https = require("https");
const { Image, createCanvas } = require("canvas");
const { getColor, findNearest, hex } = require("./tools");
const fs = require("fs");
if (process.env.NODE_ENV !== "production") {
require("dotenv").config();
}
const client = new Twitter({
consumer_key: process.env.TWITTER_API_CONSUMER_KEY || "",
consumer_secret: process.env.TWITTER_API_CONSUMER_SECRET || "",
access_token_key: process.env.TWITTER_API_TOKEN || "",
access_token_secret: process.env.TWITTER_API_TOKEN_SECRET || "",
});
const LOCATION = process.env.LOCATION || "Berlin";
const SOURCE_IMAGE = process.env.SOURCE_IMAGE ||
"http://www.met.fu-berlin.de/wetter/webcam/picam2_prod.jpg";
const MIN_SLEEP_TIME = 0.25 * 60 * 60 * 1000;
const MAX_SLEEP_TIME = 0.5 * 60 * 60 * 1000;
let lastColor;
const loop = () => {
getImage((src) => {
const img = new Image();
img.src = src;
const canvas = createCanvas();
const color = getColor(img, canvas);
const hexValue = hex(color);
const name = findNearest(color);
if (lastColor != name) {
lastColor = name;
updateWithImage(name, hexValue);
} else {
console.error("Error tweeting color: ", name);
}
});
const sleep = Math.round(
MIN_SLEEP_TIME + Math.random() * (MAX_SLEEP_TIME - MIN_SLEEP_TIME)
);
console.log(
"Bot is sleeping for " +
sleep / 60 / 1000 +
" minutes, will return at " +
new Date(sleep + new Date().valueOf()).toString() +
"."
);
setTimeout(loop, sleep);
};
const getImage = (callback) => {
const sourceUrl = new URL(SOURCE_IMAGE);
const get = sourceUrl.protocol === 'https:' ? https.get : http.get;
const req = get(SOURCE_IMAGE, (res) => {
if (res.statusCode == 200) {
const chunks = [];
res.on("data", (chunk) => {
chunks.push(chunk);
});
res.on("end", () => {
const src = Buffer.concat(chunks);
callback(src);
});
} else {
console.error("Error fetching image from source: " + res.statusCode);
}
});
req.on("error", (e) => {
console.error("Request Error: " + e.message);
});
};
const updateWithImage = (name, hex) => {
const canvas = createCanvas();
const ctx = canvas.getContext("2d");
canvas.width = 400;
canvas.height = 300;
ctx.fillStyle = `#${hex}`;
ctx.fillRect(0, 0, 400, 300);
const dataURL = canvas.toDataURL().replace(/^data:image\/png;base64,/, "");
return fs.writeFile("output.png", dataURL, "base64", (err) => {
if (err) throw err;
sendUpdate(name, hex);
});
};
const sendUpdate = (name, hex) => {
const image = fs.readFileSync("output.png", "base64");
client.post(
"media/upload",
{ media_data: image },
(error, data, response) => {
if (error) {
console.error(error);
}
const status = {
status: `The color of the sky in ${LOCATION} is ${name}. #${hex}`,
media_ids: data.media_id_string,
};
client.post("statuses/update", status, (error, status, response) => {
if (error) {
console.error(error);
} else {
console.log("Status updated.");
}
});
client.post(
"account/update_profile_banner",
{ banner: image },
(error, data, response) => {
if (error) {
console.error(error);
} else {
console.log("Profile banner updated.");
}
}
);
}
);
};
loop();