var co = require('co');
var request = require('co-request');
var cheerio = require('cheerio');
var CronJob = require('cron').CronJob;
var moment = require('moment');
var mongoose = require('mongoose');
var OneModel = mongoose.model('One');
var logger = require('winston');

module.exports = function() {
	grab(); //启动时抓取一次
	new CronJob('0 0 1 * * *', grab, null, true, 'Asia/Shanghai');
};


var grab = function() {
	co(function*() {
		var localVol = yield getLocalVol();
		var vol = getTodayVol();
		logger.info("local's vol = " + localVol);
		logger.info("today's vol = " + vol);
		var one = {};
		var path , result, $;
		for (var i = localVol + 1; i <= vol; i++) {
			path = 'http://wufazhuce.com/one/vol.' + i;
			result = yield request(path);
			if (result.statusCode !== 200) {
				throw Error('can not get ' + path);
			}
			//console.log(result.body);
			$ = cheerio.load(result.body, {
				decodeEntities: false
			});
			//var one = {};
			one.vol = i;
			one.tilte = $('h2.articulo-titulo').text().trim();
			one.author = $('p.articulo-autor').text().trim().replace('作者/', '');
			one.abstract = $('div.comilla-cerrar').text().trim();
			one.article = $('div.articulo-contenido').html(); 
			one.one = $('div.one-cita').text().trim();
			one.date = parseDate($('p.dom').text(), $('p.may').text()).unix();
			one.imgUrl = $('div.one-imagen img').attr('src');
			one.imgDetail = $('div.one-imagen-leyenda').text().trim();
			yield new OneModel(one).save();
			logger.info("save vol." + one.vol + " success");
		}
	}).then(function() {
		logger.info("grab done");
	}).catch(function(err) {
		logger.error(err.stack);
	});
}

var parseDate = function(day, monthAndYear) {
	var dateStr = day + " " + monthAndYear + " +0800";
	return moment(dateStr, 'D MMM YYYY Z');
}

var getTodayVol = function() {
	var day1 = moment("8 Oct 2012 +0800", 'D MMM YYYY Z');
	// var sub = moment().subtract(day1);
	return (moment().subtract(day1.unix(), "seconds").unix() / (24 * 60 * 60) | 0) + 1; // today - firstday +1
};

var getLocalVol = function*() {
	var res = yield OneModel.find({}, {
		vol: 1
	}).sort({
		vol: -1
	}).limit(1);
	return res.length > 0 ? res[0].vol : 0;
};

@@ -2034,24 +2034,31 @@
 nction*() %7B%0A
+%09try%7B%0A%09
 %09var res = y
@@ -2082,16 +2082,17 @@
 d(%7B%7D, %7B%0A
+%09
 %09%09vol: 1
@@ -2092,16 +2092,17 @@
 %09vol: 1%0A
+%09
 %09%7D).sort
@@ -2104,16 +2104,17 @@
 .sort(%7B%0A
+%09
 %09%09vol: -
@@ -2115,16 +2115,17 @@
 vol: -1%0A
+%09
 %09%7D).limi
@@ -2126,24 +2126,25 @@
 ).limit(1);%0A
+%09
 %09return res.
@@ -2172,11 +2172,124 @@
 ol : 0;%0A
+%09%7Dcatch(e)%7B%0A%09%09logger.error('getLocalVol error res :'+res);%0A%09%09logger.error(e);%0A%09%09return yield getLocalVol();%0A%09%7D%0A%09%0A
 %7D;%0A

fix bug