1
0
Code Issues Pull Requests Projects Releases Wiki Activity GitHub Gitee

引入环境变量

This commit is contained in:
程序员小墨 2022-07-23 23:48:55 +08:00
parent 8067df5ae2
commit bbe9753ad3
4 changed files with 44 additions and 4 deletions

8
.env Normal file
View File

@ -0,0 +1,8 @@
# 调试模式
# 1为开启调试
DEBUG_MODE=1
# 爬取数据保存的文件夹
# 目录开头与结尾的 [./] [/] [\] [\\] 均可带可不带
# 默认为 data 文件夹
DATA_FOLDER=data

View File

@ -1,3 +1,31 @@
'use strict';
/**
* 环境变量
*/
const dotenv = require('dotenv');
process.env = {}; // 清除系统自带的环境变量
dotenv.config('./.env'); // 导入 .env 文件中的环境变量
// console.log(process.env);
// 调试模式
const DEBUG_MODE = !!process.env.DEBUG_MODE;
if (DEBUG_MODE) {
console.log('DEBUG_MODE is on');
console.log('Environment variables: ', process.env);
}
/**
* 引入模块
*/
const get_hotband = require('./src/get_hotband'); const get_hotband = require('./src/get_hotband');
/**
* 开始运行
*/
console.log("Start running ...");
// 爬取热搜数据
get_hotband.main(); get_hotband.main();

View File

@ -1,3 +1,4 @@
// refer: https://www.webhek.com/post/execute-a-command-line-binary-with-node-js/
var child_process = require('child_process'); var child_process = require('child_process');
var cmds = [ var cmds = [
'git status', 'git status',

View File

@ -2,8 +2,10 @@
const request = require('request'); const request = require('request');
const fs = require('fs'); const fs = require('fs');
const { dirname } = require('path');
const path = require('path');
console.log("Start running ..."); const DATA_FOLDER = path.join(dirname(__dirname), process.env.DATA_FOLDER ?? 'data');
// 请求微博热搜 APi 接口 // 请求微博热搜 APi 接口
async function getApiResult(url) { async function getApiResult(url) {
@ -52,7 +54,7 @@ function saveJSON({ now, fileNameSuffix, object, compress = true, uncompress = t
// console.log( "year, month, day, hour, minute: " + year + ", " + month + ", " + day + ", " + hour + ", " + minute); // console.log( "year, month, day, hour, minute: " + year + ", " + month + ", " + day + ", " + hour + ", " + minute);
// 创建当前文件夹 // 创建当前文件夹
let folder = `./data/${year}/${month}/${day}`; let folder = `${DATA_FOLDER}/${year}/${month}/${day}`;
createFolder(folder); createFolder(folder);
let fileName = `${folder}/${year}${month}${day}_${hour}${minute}_${fileNameSuffix}`; let fileName = `${folder}/${year}${month}${day}_${hour}${minute}_${fileNameSuffix}`;
@ -68,6 +70,8 @@ function saveJSON({ now, fileNameSuffix, object, compress = true, uncompress = t
} }
async function main() { async function main() {
console.log("DATA_FOLDER", DATA_FOLDER);
let requestTimestamp = Date.now(); let requestTimestamp = Date.now();
let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString(); let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString();
@ -158,7 +162,6 @@ async function main() {
* 两者差值通过观测似乎最大是 1250000 * 两者差值通过观测似乎最大是 1250000
* 例如 唐山打架事件8名违法嫌疑人已到案 这条热搜一开始 delta 首先不断增大最大达到 1250000 * 例如 唐山打架事件8名违法嫌疑人已到案 这条热搜一开始 delta 首先不断增大最大达到 1250000
* 然后热搜数量增加到 12600000 左右的时候delta 逐渐减小到 1040000 左右 * 然后热搜数量增加到 12600000 左右的时候delta 逐渐减小到 1040000 左右
* 所有热搜的 detla带正负 加起来就是基本上在100000-230000之间
*/ */
num: item.num, num: item.num,
raw_hot: item.raw_hot, raw_hot: item.raw_hot,
@ -235,7 +238,7 @@ async function main() {
/** /**
* 更新最新的 * 更新最新的
*/ */
fs.writeFileSync(`./data/latest.json`, JSON.stringify({ fs.writeFileSync(`${DATA_FOLDER}/latest.json`, JSON.stringify({
update_time: requestTimestamp, update_time: requestTimestamp,
update_time_friendly: now.substring(0, 19).replace(/T/g, " "), update_time_friendly: now.substring(0, 19).replace(/T/g, " "),
regulation: convert2, regulation: convert2,