diff --git a/getTrafic.js b/getTrafic.js index 99841a9..476b02a 100644 --- a/getTrafic.js +++ b/getTrafic.js @@ -3,34 +3,67 @@ const path = require('path'); const moment = require('moment-timezone'); const _ = require('lodash'); const schedule = require('node-schedule'); +const { MongoExe } = require('./models/common/data'); +const config = require('./config/config'); -let traficLog = fs.readFileSync(path.join(__dirname , "default.log") , {encoding : 'utf-8'}); -traficLog = traficLog.replace(/ UTC -> IsLoggedIn:true /gim , ""); -let lines = traficLog.split("\r\n"); -let items = []; -for (line of lines) { - let [date , time , account] = line.split(' '); - date = date.replace("," , ""); - let obj = { - datetime : `${date} ${time}` , - account : account +function getTrafic () { + let traficLog = fs.readFileSync(path.join(__dirname , "default.log") , {encoding : 'utf-8'}); + traficLog = traficLog.replace(/ UTC -> IsLoggedIn:true /gim , ""); + let lines = traficLog.split("\r\n"); + + let items = []; + for (line of lines) { + let [date , time , account] = line.split(' '); + date = date.replace("," , ""); + let obj = { + datetime : `${date} ${time}` , + account : account + } + items.push(obj); } - items.push(obj); + let hitItem = items.filter(v => { + if (v.account == "UTC") return false; + try { + let taipeiTime = moment.utc(v.datetime , "YYYY.MM.DD HH:mm:ss.SSSS").tz("Asia/Taipei"); + let isoFormatTime = moment(taipeiTime , "YYYY.MM.DD").format('YYYY-MM-DD'); + let dateNow = moment(new Date()).format('YYYY-MM-DD'); + return moment(isoFormatTime , 'YYYY-MM-DD').isSameOrAfter(dateNow); + } catch (e) { + console.log(v.datetime); + return false; + } + }) + + hitItem = _.uniqBy(hitItem , 'account'); + (async ()=> { + try { + const mongoConn = await MongoExe(); + await mongoConn.db(config.MONGODB.db).collection('loginLog').findOneAndUpdate( + { id : moment(new Date()).format('YYYY-MM-DD')} , + { + $set : { + num : hitItem.length, + refreshTime: new Date() + } + } , + { + upsert : true + } + ); + console.log("today trafic log finished"); + process.exit(0); + } catch (e) { + console.error(e); + process.exit(1); + } + })(); + console.log("login stu: " , hitItem); + console.log(moment(new Date()).format('YYYY-MM-DD') , ":" , hitItem.length); } -let hitItem = items.filter(v => { - if (v.account == "UTC") return false; - try { - let taipeiTime = moment.utc(v.datetime , "YYYY.MM.DD HH:mm:ss.SSSS").tz("Asia/Taipei"); - let isoFormatTime = moment(taipeiTime , "YYYY.MM.DD").format('YYYY-MM-DD'); - let dateNow = moment(new Date()).format('YYYY-MM-DD'); - return moment(isoFormatTime , 'YYYY-MM-DD').isSameOrAfter(dateNow); - } catch (e) { - console.log(v.datetime); - return false; - } -}) -hitItem = _.uniqBy(hitItem , 'account'); -console.log(hitItem.length); +schedule.scheduleJob("0 58 23 * * *" , function () { + getTrafic(); +}); +getTrafic(); diff --git a/models/users/passport.js b/models/users/passport.js index de03663..9735398 100644 --- a/models/users/passport.js +++ b/models/users/passport.js @@ -1,8 +1,5 @@ var LocalStrategy = require('passport-local').Strategy; -const fs = require('fs'); -const DBdata = require('../common/data.js') const School_Auth = require('./School_Auth.js'); -const request = require('request'); const myFunc = require('../../routes/My_Func'); const cheerio = require('cheerio'); const nodeFetch = require('node-fetch'); @@ -90,7 +87,6 @@ module.exports = async function(passport) //req.session.myJar = myReqObj.jar; let logingResultCode = loginresult.split('_'); let j = myFunc.getJar(req); - let signOffJar = myFunc.getSignOffJar(req); if (!logingResultCode.includes("true")) { console.log("error pwd"); @@ -110,7 +106,6 @@ module.exports = async function(passport) let $ = cheerio.load(homeBody); let Profile = $("#ctl00_tableProfile tr"); - let stuInfo = []; for (let i = 0 ; i < Profile.length ; i++) { @@ -173,6 +168,7 @@ module.exports = async function(passport) if (!STNO) { return done(null , false , req.flash('error',"無法取得資訊,請重新登入,若還是無法登入請聯繫作者。")); } + await School_Auth.ilmsAuth(username , password , req); return done(null ,username); }));