-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (78 loc) · 3.29 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
#!/usr/bin/env node
const Promise = require('bluebird');
const fs = require('fs');
const path = require('path');
const request = require('request-promise');
const moment = require('moment');
const _ = require('lodash');
const errors = require('request-promise/errors');
const args = require('./args.js');
const config = _.assign({
filteredPokemonIds: null,
trustedUserId: null,
minLatitude: 24.783617562869416,
maxLatitude: 24.82740393838965,
minLongitude: 120.93629837036131,
maxLongitude: 121.0129451751709,
queryInterval: 10000,
telegramChatId: null,
telegramBotToken: null,
telegramBotEnable: false,
source: 'goradar'
}, require(path.resolve(args.config)));
const PokeRadar = require('./providers/pokeradar.js');
const telegramBot = require('./telegram_bot.js')(config);
const pokemonNames = require('./pokemon_names.js');
const pokemonStickers = require('./stickers.js');
const getReverseGeocode = require('./get_reverse_geocode.js');
let sentPokemons = [];
const pushNotifications = function(pokemons) {
let promise = Promise.resolve();
sentPokemons = _.filter(sentPokemons, (o) => o.until.isAfter(moment()));
pokemons.forEach(function(v) {
if (!_.find(sentPokemons, (o) => o.uniqueId == v.uniqueId) && v.remainingTime.diff(moment.utc(0)) > 0) {
let message = '';
promise = promise.then(() => getReverseGeocode(v.latitude, v.longitude)).then(function(reverseGeocode) {
message = `#${v.pokemonName.zh} (${reverseGeocode.map((x) => '#' + x).join(' ')} #${v.pokemonName.en} #${v.pokemonId})\n`
+ `導航: ${v.direction}\n`
+ `剩餘時間: ${v.remainingTime.format('mm:ss')}\n`
+ `結束於: ${v.until.format('YYYY-MM-DD HH:mm:ss')}`;
console.log(moment().format(), 'message:', message);
});
if (config.telegramBotEnable && telegramBot && config.telegramChatId) {
promise = promise
.then(() => telegramBot.sendSticker(config.telegramChatId, pokemonStickers[v.pokemonId]))
.then(() => telegramBot.sendMessage(config.telegramChatId, message))
.then(() => telegramBot.sendLocation(config.telegramChatId, v.latitude, v.longitude))
.catch(function(err) {
console.error(moment().format(), err.message);
})
}
sentPokemons.push(v);
}
});
return promise;
}
let Provider = require('./providers/' + config.source);
let provider = new Provider(config);
provider
.init()
.then(function requestLoop() {
return provider
.getPokemons()
.then(pushNotifications)
.catch(errors.StatusCodeError, function (reason) {
console.error(moment().format(), reason.message);
})
.catch(errors.RequestError, function (reason) {
console.error(moment().format(), reason.message);
})
.delay(config.queryInterval)
.then(requestLoop);
})
.catch(function(reason) {
console.error(moment().format(), reason.message);
console.log('Program Stopped')
// TODO: use TelegramBot#stopPolling instead
telegramBot._polling.abort = true;
});