var request = require('request');
var cheerio = require('cheerio');
var Table = require('cli-table');

module.exports = function() {
    // 今日推荐
    var todayRecoTable = new Table({
        head: ['今日推荐', '收益信息']
    });

    // 高收益推荐
    var highRateTable = new Table({
        head: ['高收益推荐', '收益信息']
    });

    var options = {
        url: 'http://liangchao.licai.360.cn/'
    };

    request(options, function (err, res, body) {
        if (!err && res.statusCode == 200) {
            var parseInfo = parse(body);
            var todayRecoInfo = parseInfo.todayRecoInfo;
            renderTodyRecoTable(todayRecoInfo);
            var otherRecoInfo = parseInfo.otherRecoInfo;
            renderOtherRecoTable(otherRecoInfo);
        }
    });

    // 渲染今日推荐表格
    function renderTodyRecoTable(info) {
        for (var i = 0, l = info.length; i < l; i++) {
            todayRecoTable.push([info[i].title, info[i].desc]);
        }
        console.log(todayRecoTable.toString());        
    }

    // 渲染其它类别的表格
    function renderOtherRecoTable(info) {
        for (var i = 0, l = info.length; i < l; i++) {
            var table = new Table({
                head: [info[i].name.trim(), '收益信息']
            });

            for (var j = 0, data = info[i]['data'], len = data.length; j < len; j++) {
                table.push([data[j].title, data[j].desc]);
            }

            console.log(table.toString());
        }
    }

    // 提取有用信息
    function parse(body) {
        var $ = cheerio.load(body);
        // 今日推荐
        var todayRecoArr = [];
        var dayReco = $("div.slide-content .slide-section");
        dayReco.each(function (index, section) {
            var title = $(section).find('.slide-name').text();
            var descs = $(section).find('.slide-txt');
            var descArr = [];
            descs.each(function (index, desc) {
                descArr.push($(desc).text().replace('%', '% '));
            });
            todayRecoArr.push({
                title : title,
                desc: descArr.join('  ')
            });
        });
        // 其它推荐
        var otherRecoArr = [];
        var modules = $('#doc-main-bd > .f-l .module');
        modules.each(function (index, module) {
            var moduleMap = {};
            var moduleName = $(module).find('.module-title').text();
            var productItem = $(module).find('.product-item');
            moduleMap['name'] = moduleName;
            moduleMap['data'] = [];
            productItem.each(function (index, item) {
                var descs = $(item).find('.product-yield');
                var descArr = [];
                descs.each(function (index, desc) {
                    descArr.push($(desc).find('.yield').text() + ' ' + $(desc).find('span.txt').text());
                });
                moduleMap['data'].push({
                    title : $(item).find('.product-name').text(),
                    desc : descArr.join('  ')
                });
            });
            otherRecoArr.push(moduleMap);
        });

        return {
            todayRecoInfo : todayRecoArr,
            otherRecoInfo : otherRecoArr
        };
    }


};
@@ -350,18 +350,8 @@
 p://
-liangchao.
 lica
@@ -361,16 +361,16 @@
 60.cn/'%0A
+
     %7D;%0A%0A
@@ -511,24 +511,27 @@
 %0A           
+ //
  var todayRe
@@ -571,24 +571,27 @@
 %0A           
+ //
  renderTodyR
@@ -1525,24 +1525,27 @@
 %E4%BB%8A%E6%97%A5%E6%8E%A8%E8%8D%90%0A       
+ //
  var todayRe
@@ -1559,24 +1559,27 @@
  %5B%5D;%0A       
+ //
  var dayReco
@@ -1627,16 +1627,19 @@
 %0A       
+ //
  dayReco
@@ -1676,28 +1676,31 @@
 ) %7B%0A        
+//
     
+ 
 var title = 
@@ -1738,36 +1738,39 @@
 text();%0A        
+//
     
+ 
 var descs = $(se
@@ -1795,32 +1795,35 @@
 e-txt');%0A       
+ //
      var descArr
@@ -1832,24 +1832,27 @@
  %5B%5D;%0A       
+ //
      descs.e
@@ -1879,32 +1879,35 @@
  desc) %7B%0A       
+ //
          descArr
@@ -1947,32 +1947,35 @@
  '%25 '));%0A       
+ //
      %7D);%0A       
@@ -1966,32 +1966,35 @@
      %7D);%0A       
+ //
      todayRecoAr
@@ -2006,32 +2006,34 @@
 h(%7B%0A        
+//
         
 title : titl
@@ -2012,32 +2012,33 @@
       //        
+ 
 title : title,%0A 
@@ -2035,32 +2035,35 @@
 : title,%0A       
+ //
          desc: d
@@ -2079,32 +2079,35 @@
 in('  ')%0A       
+ //
      %7D);%0A       
@@ -2102,24 +2102,27 @@
  %7D);%0A       
+ //
  %7D);%0A       
@@ -3095,16 +3095,16 @@
 eturn %7B%0A
-
         
@@ -3098,32 +3098,35 @@
 rn %7B%0A           
+ //
  todayRecoInfo :

remove recommand