Skip to content

Instantly share code, notes, and snippets.

@pnlybubbles
Last active November 28, 2015 15:09
Show Gist options
  • Save pnlybubbles/bb0c86b308507394d6c4 to your computer and use it in GitHub Desktop.
Save pnlybubbles/bb0c86b308507394d6c4 to your computer and use it in GitHub Desktop.
ぷよクエのマルチの部屋をTwitterでトラックして、pushbulletを使ってプッシュ通知する
import 'source-map-support/register';
import PushBullet from 'pushbullet';
import Twit from 'twit';
import winston from 'winston';
winston.add(winston.transports.File, { filename: 'winston.log', json: false });
const pusher = new PushBullet('*****');
const T = new Twit({
consumer_key: '*****',
consumer_secret: '*****',
access_token: '*****',
access_token_secret: '*****',
});
let hist = [];
const trackStream = T.stream('statuses/filter', {track: 'ぷよクエ'});
trackStream.on('tweet', (res) => {
const roomNumber = res.text.match(/[0-9]{6}/);
if (roomNumber) {
const msg = res.text
.replace(/ぷよクエ|マルチ|プレイ|甘口|中辛|辛口|激辛|部屋|番号|【|】|:|(http[^s]+)|([0-9]{6})|#|#|\n/g, '').trim();
const level = (res.text.match(/甘口|中辛|辛口|激辛/) || '未指定').toString();
const title = `${res.user.screen_name} ${roomNumber} ${level} ${msg}`;
const link = `com.sega.puyoquest:\/\/multiroom?room_no=${roomNumber}`;
const weekend = [0, 6].indexOf(new Date().getDay()) !== -1;
let priority = 5;
if (weekend) {
if (res.text.match(/黄|きいろ/)) {
priority = 0;
} else {
priority = ['甘口', '激辛', '辛口', '中辛', '未指定'].indexOf(level) + 1;
}
} else {
priority = ['甘口', '激辛', '辛口', '中辛', '未指定'].indexOf(level);
}
if (hist.length >= 5) { console.log((new Date() - hist[0]) / 1000 / 5 / 30, priority); }
if (hist.length <= 4 || priority === 0 || (new Date() - hist[0]) / 1000 / 5 / 30 >= priority) {
hist.push(new Date());
if (hist.length >= 6) { hist.shift(); }
pusher.link('*****@gmail.com', title, link, (err, response) => {
if (err) {
winston.error(`[pushbullet failed] ${err}`);
} else {
winston.info(`[pushbullet] room_number:${roomNumber.toString()} title:${title}`);
setTimeout(() => {
pusher.deletePush(response.iden, (err_) => {
if (err_) {
winston.error(`[pushbullet delete failed] ${err_}`);
}
});
}, 60 * 1000);
}
});
}
}
});
trackStream.on('error', (res) => {
winston.error(JSON.stringify(res));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment