From a2af3be807279b334ff981124d28f579ee590954 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=B0=8F=E5=A2=A8?= <2291200076@qq.com> Date: Sun, 24 Jul 2022 01:01:23 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AF=8F=E5=B0=8F=E6=97=B6=E5=B0=86data?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=A4=B9=E4=B8=AD=E7=9A=84=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E6=8E=A8=E9=80=81=E5=88=B0git=E4=BB=93=E5=BA=93=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 41 ++++++++++++++++++++++++++++++++++++++--- src/execute_command.js | 18 +++++++----------- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 211d3c6..c64729d 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ const dotenv = require('dotenv'); const schedule = require('node-schedule'); +const path = require('path'); +const os = require('os'); /** * 环境变量 @@ -10,10 +12,15 @@ process.env = {}; // 清除系统自带的环境变量 dotenv.config('./.env'); // 导入 .env 文件中的环境变量 // console.log(process.env); -// 调试模式 const DEBUG_MODE = process.env.DEBUG_MODE == true; const EXECUTE_AT_STARTUP = process.env.EXECUTE_AT_STARTUP == true; +const ROOT_PATH = path.join(__dirname, process.env.DATA_FOLDER ?? 'data'); + + +/** + * 调试模式 + */ if (DEBUG_MODE) { console.log('DEBUG_MODE is on'); console.log('Environment variables: ', process.env); @@ -23,6 +30,7 @@ if (DEBUG_MODE) { * 引入模块 */ const get_hotband = require('./src/get_hotband'); +const execute_command = require('./src/execute_command'); /** @@ -31,9 +39,9 @@ const get_hotband = require('./src/get_hotband'); console.log("Start running ..."); // 程序主函数 -function start() { +async function start() { // 爬取热搜数据 - get_hotband.main(); + await get_hotband.main(); } // 调试模式下,程序一启动就首先运行一次 @@ -46,3 +54,30 @@ if (EXECUTE_AT_STARTUP) { // 这里指定第 5 秒是为了稍微与微博服务器热榜更新时间错开,避免因为微秒级误差造成拉取两次相同的热榜数据 // refer: https://www.npmjs.com/package/node-schedule const scheduleJob = schedule.scheduleJob('05 * * * * *', start); + +// 定时推送 +async function pushToGitRepo() { + let commands = [ + 'git status', + 'git add .', + `git commit -m "${new Date(Date.now() + 8 * 3600 * 1000).toISOString().substring(0, 19).replace('T', ' ')} update"`, + `git push origin master`, + 'git status', + ]; + switch (os.type()) { + case 'Windows_NT': // Windows + commands.unshift('dir'); + break; + + case 'Darwin': // Mac OS X + case 'Linux': // Linux + default: + commands.unshift('pwd'); + break; + } + let outputs = await execute_command.execute(ROOT_PATH, commands); + console.log(commands, outputs); +} + +// 每个小时同步一次 +schedule.scheduleJob('0 0 * * * *', pushToGitRepo); diff --git a/src/execute_command.js b/src/execute_command.js index 23fec0e..df21067 100644 --- a/src/execute_command.js +++ b/src/execute_command.js @@ -1,24 +1,18 @@ 'use strict'; -const path = require('path'); const child_process = require('child_process'); const iconv = require("iconv-lite"); const encoding = "cp936"; const bufferEncoding = "binary"; -const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data'); -var cmds = [ - 'dir', - 'git status', -]; - -async function main() { +async function execute(rootPath, cmds) { + let outputs = []; for (let cmd of cmds) { let result = await new Promise(function (resolve) { // refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/ child_process.exec(cmd, { - cwd: DATA_FOLDER, // 脚本执行目录 + cwd: rootPath, // 脚本执行目录 encoding: bufferEncoding }, function (err, stdout, stderr) { if (err) { @@ -38,7 +32,9 @@ async function main() { } }); }); - console.log(result); + outputs.push(result); } + return outputs; } -main(); \ No newline at end of file + +exports.execute = execute;