From c744d24393b7fdb169654c499da8bd7e7d77f609 Mon Sep 17 00:00:00 2001
From: 沈斌 <bluelazysb@hotmail.com>
Date: Tue, 27 Dec 2016 09:06:46 +0800
Subject: [PATCH] 环境数月报自动生成生成并发送邮件功能的完善

---
 reporter.js  |  117 ++++++++++++++++++++++++---------------
 package.json |    4 +
 config.js    |    3 +
 3 files changed, 78 insertions(+), 46 deletions(-)

diff --git a/config.js b/config.js
index ebe530f..6f1f55e 100644
--- a/config.js
+++ b/config.js
@@ -3,6 +3,9 @@
  */
 
 module.exports = {
+    mongo: {
+        uri: 'mongodb://moral_user:m2o0r1a6l_passw0rd@121.40.92.176:27017/moral_db'
+    },
     smtpOption: {
         host: 'smtp.qq.com',
         port: 465,
diff --git a/package.json b/package.json
index 6fd4966..b58b020 100644
--- a/package.json
+++ b/package.json
@@ -6,6 +6,8 @@
     "nodemailer-smtp-transport": "~2.7.2",
     "retry": "~0.10.0",
     "request": "~2.79.0",
-    "webshot": "~0.18.0"
+    "webshot": "~0.18.0",
+    "mongodb": "~2.2.16",
+    "moment": "~2.17.1"
   }
 }
diff --git a/reporter.js b/reporter.js
index 5b60a3e..0fe4df4 100644
--- a/reporter.js
+++ b/reporter.js
@@ -9,50 +9,77 @@
 var webshot = require('webshot');
 var fs = require("fs");
 var config = require('./config');
+var mongoClient = require('mongodb').MongoClient;
+var moment = require('moment');
 
-request('http://env.7drlb.com/report/user/5766a035f08504e7cd3fb33e/year/2016/month/11/view', function (error, response, body) {
-    if (!error && response.statusCode == 200) {
-        var filename = '5766a035f08504e7cd3fb33e_2016_11.png';
-        webshot(body, filename, {
-            renderDelay: 3000,
-            siteType:'html',
-            screenSize: {
-                width: 1000,
-                height: 2000
-            },
-            shotSize: {
-                width: 1000,
-                height: 'all'
-            }
-        }, function(err) {
-            var filepath = __dirname + '/' + filename;
-            var imageFile = fs.readFileSync(filepath);
-            var send_email = function(email, subject, content, level) {
-                var operation = retry.operation(config.retryOption);
-                operation.attempt(function(currentAttempt) {
-                    var to = email;
-                    var html = '<img src="data:image/png;base64,' + imageFile.toString("base64") + '" width=1000 height=2000>';
-                    var transport = nodemailer.createTransport(smtpTransport(config.smtpOption));
-                    var mailOptions = {
-                        from: config.smtpOption.auth.user,
-                        to: to,
-                        subject:subject,
-                        html:html,
-                        attachments: [{
-                            path: filepath,
-                            filename: filename,
-                            content: content
-                        }]
-                    };
-                    transport.sendMail(mailOptions, function(err, doc){
-                        if (operation.retry(err)) {
-                            return;
-                        }
-                    });
-                });
-            };
-
-            send_email("it01@moral.org.cn", '���������������������������������������', '2016-11������������');
-        });
+mongoClient.connect(config.mongo.uri, function(err, db) {
+    if (err) {
+        console.log(err.message);
+        return;
     }
-});
\ No newline at end of file
+    console.log('Connecting to Mongo DB at ' + config.mongo.uri);
+
+    var current = moment();
+    var year = current.format("YYYY");
+    var month = current.format("MM");
+
+    db.collection("users").find().toArray(function(err, docs) {
+        docs.forEach(function(doc) {
+            var userID = doc._id;
+            var email = doc.email;
+            console.log(year + ' | ' + month + ' | ' + userID + ' | ' + email);
+            sendReport(userID, year, month, email);
+        });
+    });
+
+    db.close();
+});
+
+var sendReport = function(userID, year, month, email) {
+    var url = 'http://env.7drlb.com/report/user/' + userID + '/year/' + year + '/month/' + month + '/view';
+    request(url, function (error, response, body) {
+        if (!error && response.statusCode == 200) {
+            var filename = userID + '_' + year + '_' + month + '.png';
+            webshot(body, filename, {
+                renderDelay: 3000,
+                siteType:'html',
+                screenSize: {
+                    width: 1000,
+                    height: 2000
+                },
+                shotSize: {
+                    width: 1000,
+                    height: 'all'
+                }
+            }, function(err) {
+                var filepath = __dirname + '/' + filename;
+                var imageFile = fs.readFileSync(filepath);
+                var send_email = function(email, subject, content, level) {
+                    var operation = retry.operation(config.retryOption);
+                    operation.attempt(function(currentAttempt) {
+                        var to = email;
+                        var html = '<img src="data:image/png;base64,' + imageFile.toString("base64") + '" width=1000 height=2000>';
+                        var transport = nodemailer.createTransport(smtpTransport(config.smtpOption));
+                        var mailOptions = {
+                            from: config.smtpOption.auth.user,
+                            to: to,
+                            subject:subject,
+                            html:html,
+                            attachments: [{
+                                path: filepath,
+                                filename: filename,
+                                content: content
+                            }]
+                        };
+                        transport.sendMail(mailOptions, function(err, doc){
+                            if (operation.retry(err)) {
+                                return;
+                            }
+                        });
+                    });
+                };
+                send_email(email, '���������������������������������������', '2016-11������������');
+            });
+        }
+    });
+};
\ No newline at end of file

--
Gitblit v1.8.0