-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
243 lines (224 loc) · 8.51 KB
/
run.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const weather = require('./lib/weather');
const windSpeedTrans = require('./lib/windSpeedTrans');
const ptt = require('./lib/ptt');
const covid = require('./lib/covid');
const goodSentence = require('./lib/goodSentence');
const news = require('./lib/news');
const dedupPost = require('./lib/dedupPost');
//const util = require('util');
let plurk = require('./lib/plurk');
if (process.argv.length === 4 && process.argv[3] === 'debug') {
plurk = {
callAPI: (url, param, cb) => {
cb(null, {
plurk_id: 'DEBUG',
qualifier_translated: param.qualifier,
content: param.content
});
}
};
}
function postPlurk(content, qualifier) {
return new Promise((resolve, reject) => {
plurk.callAPI('/APP/Timeline/plurkAdd', {
content,
qualifier: qualifier || 'says',
lang: 'tr_ch',
}, (err, data) => {
if (err) {
console.log('ERROR', err);
reject(err);
} else {
console.log('======== OK', data.plurk_id, data.qualifier_translated, data.content);
resolve(data);
}
});
});
}
function getWeatherContents(weather) {
const todayWeather = weather.daily[0];
const todayWeatherDescription = todayWeather.weather.map((w) => {
return w.description;
}).join('、');
const getLocalTime = function (timestamp) {
const dataObj = new Date((timestamp + weather.timezone_offset) * 1000);
const minute = dataObj.getUTCMinutes();
return dataObj.getUTCHours() + ':' + (minute < 10 ? '0' + minute : minute);
};
const getWindDirectionName = function (deg) {
const mapping = ['北', '東北', '東', '東南', '南', '西南', '西', '西北'];
let base = 22.5;
for (let i = 0 ; i < 9; i++) {
if (deg < base) {
return mapping[i % mapping.length];
}
base += 45;
}
};
const sunsetTime = getLocalTime(todayWeather.sunset);
const sunriseTime = getLocalTime(todayWeather.sunrise);
const currentWeather = weather.current;
const contents = [];
const displayInt = {
todayMax: todayWeather.temp.max,
todayMin: todayWeather.temp.min,
feelDay: todayWeather.feels_like.day,
feelNight: todayWeather.feels_like.night,
currentTemp: currentWeather.temp,
currentFeel: currentWeather.feels_like,
};
Object.keys(displayInt).forEach((key) => {
displayInt[key] = Math.round(displayInt[key]);
});
const wind = windSpeedTrans(todayWeather.wind_speed);
const typhoon = wind.typhoon ? '(' + wind.typhoon.name + '颱風)' : '';
const windDir = getWindDirectionName(todayWeather.wind_deg) + '風';
let weatherStr = `阿姆斯特丹今天天氣${todayWeatherDescription}
高溫 ${displayInt.todayMax}°C 低溫 ${displayInt.todayMin}°C 濕度 ${todayWeather.humidity}%
體感溫度白天 ${displayInt.feelDay}°C 晚上 ${displayInt.feelNight}°C
${wind.name}風${wind.rank}級 ${windDir} ${todayWeather.wind_speed} 公尺/秒 ${typhoon}
日出時間 ${sunriseTime} 日落時間 ${sunsetTime}
現在氣溫 ${displayInt.currentTemp}°C 體感溫度 ${displayInt.currentFeel}°C
`;
const localHour = (new Date((new Date).getTime() + weather.timezone_offset * 1000)).getUTCHours();
let helloText = '早安';
if (localHour >= 12 && localHour < 18) {
helloText = '午安';
} else if (localHour >= 18 || localHour < 4) {
helloText = '晚安';
}
helloText += '!';
contents.push(helloText + weatherStr.trim());
if (weather.alerts && weather.alerts.length > 0) {
let wa = weather.alerts[weather.alerts.length > 1 ? 1 : 0];
let warning = `天氣警報 [${wa.event}]
${wa.description}`;
contents.push(warning.slice(0, 360));
}
return contents;
}
function plurkWeather() {
weather.getWeatherAmsterdam((weatherData) => {
//console.log(util.inspect(weatherData, {showHidden: false, depth: null}))
const contents = getWeatherContents(weatherData);
postPlurk(contents[0]);
if (contents.length > 1) {
postPlurk(contents[1]);
}
});
}
function plurkPTT() {
ptt.getHotArticles(10, (data) => {
dedupPost.init();
function getPTTQualifier(type, title) {
if (type === '問卦') {
if (title.match(/?|\?/)) {
return 'asks';
} else {
return 'wonders';
}
} else {
return 'shares';
}
}
for (let i = 0; i < data.length; i++) {
let item = data[i];
if (!dedupPost.wasPosted(item.title)) {
postPlurk('https://www.ptt.cc' + item.url + ' (' + item.title + ')', getPTTQualifier(item.type, item.title));
dedupPost.add(item.title);
break;
}
}
dedupPost.finish();
});
}
const taskRouter = {
all: function() {
Object.keys(this).filter(task => task !== 'all').forEach((task) => {
console.log('Invoke task', task);
taskRouter[task]();
});
},
weather: function() {
plurkWeather();
},
ptt: function() {
plurkPTT();
},
covid: function() {
covid('netherlands').then((covidData) => {
const sentences = ['荷蘭昨天新增中國肺炎 ' + covidData.NewConfirmed + ' 例'];
covid('taiwan').then((covidDataTw) => {
let sentenceTaiwan;
if (covidDataTw.TWNewConfirmedLocal || covidDataTw.TWNewConfirmedExternal) {
sentenceTaiwan = '台灣昨天新增中國肺炎 ' + covidDataTw.NewConfirmed + ' 例';
const twDetail = [];
if (covidDataTw.TWNewConfirmedLocal) {
twDetail.push('本土' + covidDataTw.TWNewConfirmedLocal);
}
if (covidDataTw.TWNewConfirmedExternal) {
twDetail.push('境外' + covidDataTw.TWNewConfirmedExternal);
}
sentenceTaiwan += '(' + twDetail.join('、') + ')';
} else if (covidDataTw.FallbackRawTitle) {
sentenceTaiwan = '台灣:' + covidDataTw.FallbackRawTitle;
}
sentences.push(sentenceTaiwan);
postPlurk(sentences.join("\n"), 'has');
}).catch((err) => {
console.log('Unable to fetch covid info from Taiwan', err);
postPlurk(sentences.join("\n"), 'has');
});
}).catch((err) => {
console.log('Unable to fetch covid info from Netherlands', err);
});
},
sentence: function() {
goodSentence((data) => {
if (data) {
const from = data.from ? `\n~*${data.from}*` : '';
postPlurk(`${data.text}${from}`, 'thinks');
}
});
},
news: function() {
news.getNews((data) => {
const now = new Date();
const nowts = now.getTime();
data = data.filter((item) => {
if (item.title && item.title.match(/荷蘭|歐盟/)
&& nowts - item.time.getTime() < 86400000) {
return true;
} else {
return false;
}
}).map((item) => {
item.title = item.title.replace('[國際]', '').trim();
return item;
});
if (data.length > 0) {
for (let i = 0; i < data.length; i++) {
let j = Math.floor(data.length * Math.random());
let temp = data[i];
data[i] = data[j];
data[j] = temp;
}
dedupPost.init();
for (let i = 0; i < data.length; i++) {
let item = data[i];
if (!dedupPost.wasPosted(item.title)) {
dedupPost.add(item.title);
postPlurk(item.url + ' (' + item.title + ')', 'shares');
break;
}
}
dedupPost.finish();
}
});
},
};
if (process.argv.length < 3 || (!taskRouter[process.argv[2]])) {
console.error('Usage:', process.argv[0], process.argv[1], Object.keys(taskRouter).join('|'), '[debug]');
return -1;
}
taskRouter[process.argv[2]]();