合并仓库前的准备
This commit is contained in:
110
hotband/index.js
Normal file
110
hotband/index.js
Normal file
@@ -0,0 +1,110 @@
|
||||
'use strict';
|
||||
|
||||
const dotenv = require('dotenv');
|
||||
const schedule = require('node-schedule');
|
||||
const path = require('path');
|
||||
const os = require('os');
|
||||
const fs = require('fs');
|
||||
|
||||
/**
|
||||
* 环境变量
|
||||
*/
|
||||
if (!fs.existsSync('.env')) {
|
||||
// 如果没有 .env 文件,则报错并退出
|
||||
console.error('[ERROR] .env file not found!');
|
||||
return;
|
||||
}
|
||||
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 PUSH_TO_GIT = process.env.PUSH_TO_GIT == 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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 引入模块
|
||||
*/
|
||||
const get_weibo_hotband = require('./src/get_weibo_hotband');
|
||||
const get_bilibili_hotband = require('./src/get_bilibili_hotband');
|
||||
const get_bilibili_rank = require('./src/get_bilibili_rank');
|
||||
|
||||
const execute_command = require('./src/execute_command');
|
||||
|
||||
|
||||
/**
|
||||
* 开始运行
|
||||
*/
|
||||
console.log("Start running ...");
|
||||
|
||||
|
||||
/**
|
||||
* 程序主函数
|
||||
*/
|
||||
async function start() {
|
||||
// 爬取热搜数据
|
||||
await get_weibo_hotband.main();
|
||||
await get_bilibili_hotband.main();
|
||||
await get_bilibili_rank.main();
|
||||
|
||||
// 调试模式下
|
||||
if (DEBUG_MODE) {
|
||||
// 推送到 Git 仓库
|
||||
await pushToGitRepo();
|
||||
}
|
||||
}
|
||||
|
||||
// 调试模式下,程序一启动就首先运行一次
|
||||
if (EXECUTE_AT_STARTUP) {
|
||||
process.stdout.write("程序启动时,立即运行一次\t");
|
||||
start();
|
||||
}
|
||||
|
||||
// 每分钟的第 5 秒执行一次
|
||||
// 这里指定第 5 秒是为了稍微与微博服务器热榜更新时间错开,避免因为微秒级误差造成拉取两次相同的热榜数据
|
||||
// refer: https://www.npmjs.com/package/node-schedule
|
||||
const scheduleJob = schedule.scheduleJob('05 * * * * *', start);
|
||||
|
||||
|
||||
/**
|
||||
* 定时将热搜数据推送到 Git 仓库
|
||||
*/
|
||||
async function pushToGitRepo() {
|
||||
if (!PUSH_TO_GIT) return;
|
||||
|
||||
let commands = [
|
||||
'git status',
|
||||
'git pull',
|
||||
'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);
|
Reference in New Issue
Block a user