Skip to content

Instantly share code, notes, and snippets.

@axross
Last active August 29, 2015 14:17
Show Gist options
  • Save axross/d4c4d387f0314b0a553b to your computer and use it in GitHub Desktop.
Save axross/d4c4d387f0314b0a553b to your computer and use it in GitHub Desktop.
リトルノアの部屋番号自動で見に行くやつ
import moment from 'moment';
import cheerio from 'cheerio';
import request from 'superagent';
const LEVEL_NUM = 8;
const MAP_NUMS = [4, 5];
var _url = 'http://wiki.famitsu.com/littlenoah' +
'/COOP%E5%8B%9F%E9%9B%86%E6%8E%B2%E7%A4%BA%E6%9D%BF' +
'/%E3%82%A8%E3%83%AA%E3%82%A2' + LEVEL_NUM;
var _regexps = MAP_NUMS.map(v => {
return new RegExp(`(${LEVEL_NUM})?[\\-\\.\\*]*(${v})[\\S\\s]*(\\d{5})`, 'm');
});
var _caches = [];
var fetchHTML = () => {
return new Promise((resolve, reject) => {
request.get(_url).end((err, res) => {
if (err) return reject(err);
resolve({ html: res.text });
});
});
};
var getCommentsByHTML = html => {
var $ = cheerio.load(html);
var $commentBody = $('.comment-body')
var texts = [];
$commentBody.each((i, el) => {
texts.push($(el).text());
});
return texts;
};
var parseCoop = text => {
for (let i = 0; i < _regexps.length; ++i) {
let result = _regexps[i].exec(text);
if (!result || !result[3]) continue;
let [, level, map, id] = result;
level = level || LEVEL_NUM;
return { stage: `${level}-${map}`, id: id };
}
return null;
};
var cacheCoop = coop => {
if (_caches.indexOf(coop.id) > -1) return false;
if (_caches.length === 100) _caches.shift();
_caches.push(coop.id);
return true;
};
var output = (coops) => {
var now = moment().format('HH:mm:ss');
coops.forEach(coop => {
console.log(`[${now}] ${coop.stage} : ${coop.id}`);
});
console.log(`[${now}] ----`);
};
(function main() {
fetchHTML().then(function(data) {
var comments = getCommentsByHTML(data.html);
var coops = comments.map(parseCoop).filter(v => !!v);
var newCoops = coops.filter(cacheCoop);
output(newCoops);
}).catch(function(err) {
console.error(err);
});
setTimeout(main, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment