每小时将data文件夹中的数据推送到git仓库中
This commit is contained in:
		
							
								
								
									
										41
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								index.js
									
									
									
									
									
								
							@@ -2,6 +2,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const dotenv = require('dotenv');
 | 
					const dotenv = require('dotenv');
 | 
				
			||||||
const schedule = require('node-schedule');
 | 
					const schedule = require('node-schedule');
 | 
				
			||||||
 | 
					const path = require('path');
 | 
				
			||||||
 | 
					const os = require('os');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 环境变量
 | 
					 * 环境变量
 | 
				
			||||||
@@ -10,10 +12,15 @@ process.env = {};           // 清除系统自带的环境变量
 | 
				
			|||||||
dotenv.config('./.env');    // 导入 .env 文件中的环境变量
 | 
					dotenv.config('./.env');    // 导入 .env 文件中的环境变量
 | 
				
			||||||
// console.log(process.env);
 | 
					// console.log(process.env);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// 调试模式
 | 
					 | 
				
			||||||
const DEBUG_MODE = process.env.DEBUG_MODE == true;
 | 
					const DEBUG_MODE = process.env.DEBUG_MODE == true;
 | 
				
			||||||
const EXECUTE_AT_STARTUP = process.env.EXECUTE_AT_STARTUP == 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) {
 | 
					if (DEBUG_MODE) {
 | 
				
			||||||
    console.log('DEBUG_MODE is on');
 | 
					    console.log('DEBUG_MODE is on');
 | 
				
			||||||
    console.log('Environment variables: ', process.env);
 | 
					    console.log('Environment variables: ', process.env);
 | 
				
			||||||
@@ -23,6 +30,7 @@ if (DEBUG_MODE) {
 | 
				
			|||||||
 * 引入模块
 | 
					 * 引入模块
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
const get_hotband = require('./src/get_hotband');
 | 
					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 ...");
 | 
					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 秒是为了稍微与微博服务器热榜更新时间错开,避免因为微秒级误差造成拉取两次相同的热榜数据
 | 
					// 这里指定第 5 秒是为了稍微与微博服务器热榜更新时间错开,避免因为微秒级误差造成拉取两次相同的热榜数据
 | 
				
			||||||
// refer: https://www.npmjs.com/package/node-schedule
 | 
					// refer: https://www.npmjs.com/package/node-schedule
 | 
				
			||||||
const scheduleJob = schedule.scheduleJob('05 * * * * *', start);
 | 
					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);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,24 +1,18 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const path = require('path');
 | 
					 | 
				
			||||||
const child_process = require('child_process');
 | 
					const child_process = require('child_process');
 | 
				
			||||||
const iconv = require("iconv-lite");
 | 
					const iconv = require("iconv-lite");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const encoding = "cp936";
 | 
					const encoding = "cp936";
 | 
				
			||||||
const bufferEncoding = "binary";
 | 
					const bufferEncoding = "binary";
 | 
				
			||||||
const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data');
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
var cmds = [
 | 
					async function execute(rootPath, cmds) {
 | 
				
			||||||
    'dir',
 | 
					    let outputs = [];
 | 
				
			||||||
    'git status',
 | 
					 | 
				
			||||||
];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function main() {
 | 
					 | 
				
			||||||
    for (let cmd of cmds) {
 | 
					    for (let cmd of cmds) {
 | 
				
			||||||
        let result = await new Promise(function (resolve) {
 | 
					        let result = await new Promise(function (resolve) {
 | 
				
			||||||
            // refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
 | 
					            // refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
 | 
				
			||||||
            child_process.exec(cmd, {
 | 
					            child_process.exec(cmd, {
 | 
				
			||||||
                cwd: DATA_FOLDER, // 脚本执行目录
 | 
					                cwd: rootPath, // 脚本执行目录
 | 
				
			||||||
                encoding: bufferEncoding
 | 
					                encoding: bufferEncoding
 | 
				
			||||||
            }, function (err, stdout, stderr) {
 | 
					            }, function (err, stdout, stderr) {
 | 
				
			||||||
                if (err) {
 | 
					                if (err) {
 | 
				
			||||||
@@ -38,7 +32,9 @@ async function main() {
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            });
 | 
					            });
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
        console.log(result);
 | 
					        outputs.push(result);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					    return outputs;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
main();
 | 
					
 | 
				
			||||||
 | 
					exports.execute = execute;
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user