-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgatsby-node.mjs
164 lines (141 loc) · 4.51 KB
/
gatsby-node.mjs
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import axios from 'axios';
import Color from 'color';
import dayjs from 'dayjs';
import timezone from 'dayjs/plugin/timezone.js';
import utc from 'dayjs/plugin/utc.js';
import path from 'path';
import sharp from 'sharp';
import manifest from './package.json' with { type: 'json' };
import getForecast from './src/utilities/getForecast.mjs';
import { getTemperatureFriendly } from './src/utilities/getRoomTemperatureComfortFromCelsius.mjs';
axios.defaults.timeout === 30000;
dayjs.extend(utc);
dayjs.extend(timezone);
dayjs.tz.setDefault('Europe/London');
const CLOUDY_IMAGE_SRC =
'https://www.metoffice.gov.uk/webfiles/latest/images/icons/weather/12.svg';
const PROBABLY_RAINING = 'Probably Raining';
const { title, description, author, version } = manifest;
const meta = {
siteTitle: title,
siteDescription: description,
siteUrl: process.env.GATSBY_SITE_URL,
monetization: `$ilp.gatehub.net/484331722`,
author,
version,
timeStamp: null,
todaysWeather: 'probably raining',
location: 'South London',
ogImage: `og-image-${dayjs().tz().format('YYYY-MM-DD-HH:mm:ss')}.png`,
};
function getIsTooHotForRoomTemperatureFromCelsius(celsius) {
// the maximum should be below 24 °C (75 °F) – and to avoid sick building syndrome, below 22 °C (72 °F).[3]
// From https://en.wikipedia.org/wiki/Room_temperature Accessed 2019-12-28
if (celsius > 24) {
return true;
}
return false;
}
function getIsTooColdForRoomTemperatureFromCelsius(celsius) {
// The World Health Organization's standard ...
// For those with respiratory problems or allergies, they recommend no less than 16 °C */
// From https://en.wikipedia.org/wiki/Room_temperature Accessed 2019-12-28
if (celsius < 16) {
return true;
}
return false;
}
function getIsFrostyFromCelsius(celsius) {
// Frost is likely below 4 degrees celsius
// https://www.metoffice.gov.uk/weather/learn-about/weather/types-of-weather/frost-and-ice/forecasting-frost
if (celsius <= 4) {
return true;
}
}
function getTemperatureColor(celsius) {
if (!isFinite(celsius)) return null;
if (getIsTooHotForRoomTemperatureFromCelsius(celsius)) {
return '#cc0605';
}
if (getIsFrostyFromCelsius(celsius)) {
return '#004a93';
}
if (getIsTooColdForRoomTemperatureFromCelsius(celsius)) {
return '#0075c4';
}
return '#f1d220';
}
export const createPages = async ({ actions: { createPage } }) => {
try {
const items = await getForecast();
// log('getForecast items: ', items);
meta.timeStamp = `${dayjs(new Date()).tz().format('YYYY-MM-DD HHmm')}`;
if (!items || items.length < 1) {
throw new Error(`No items in retrieved weather forecast`);
}
const today = items[0];
const backgroundColor =
Color(getTemperatureColor(today.averageTemperature))
.lighten(0.75)
.hex() || '#FFFFFF';
const input = (
await axios({
method: 'get',
url: today.icon,
responseType: 'arraybuffer',
headers: { 'Accept-Encoding': 'gzip,deflate,compress' },
})
).data;
await sharp(input, { density: 450 })
.flatten({ background: backgroundColor })
.resize(1200, 630, {
fit: 'contain',
background: backgroundColor,
})
.png()
.toFile(`public/${meta.ogImage}`);
await sharp(input, { density: 450 })
.flatten({ background: backgroundColor })
.png()
.resize(48)
.toFile(`public/favicon.ico`);
await sharp(input, { density: 450 })
.flatten({ background: backgroundColor })
.resize(180)
.toFile(`public/apple-touch-icon.png`);
meta.todaysWeather = `It's ${getTemperatureFriendly(
today.averageTemperature
).toLowerCase()} and ${today.description.toLowerCase()}`;
createPage({
path: `/`,
component: path.resolve('./src/templates/WeatherContainer.mjs'),
context: { items, meta },
});
} catch (error) {
console.error('Error creating pages');
console.error(error);
const now = dayjs();
const today = now.toISOString();
const mock = [
{
friendlyDate: 'Today',
time: today,
description: PROBABLY_RAINING,
icon: CLOUDY_IMAGE_SRC,
temperature: null,
},
{
friendlyDate: 'Sorry, problem getting forecast.',
time: today,
description: `${error}`,
icon: CLOUDY_IMAGE_SRC,
temperature: null,
},
];
createPage({
path: `/`,
component: path.resolve('./src/templates/WeatherContainer.mjs'),
context: { items: mock, meta },
});
}
};