From bbe9753ad37ca40327a4f81fa1bc3ad9560f3480 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: Sat, 23 Jul 2022 23:48:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5=E7=8E=AF=E5=A2=83=E5=8F=98?= =?UTF-8?q?=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 8 ++++++++ index.js | 28 ++++++++++++++++++++++++++++ src/execute_command.js | 1 + src/get_hotband.js | 11 +++++++---- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..f6d7059 --- /dev/null +++ b/.env @@ -0,0 +1,8 @@ +# 调试模式 +# 1为开启调试 +DEBUG_MODE=1 + +# 爬取数据保存的文件夹 +# 目录开头与结尾的 [./] [/] [\] [\\] 均可带可不带 +# 默认为 data 文件夹 +DATA_FOLDER=data diff --git a/index.js b/index.js index 4dd6cf8..a486d45 100644 --- a/index.js +++ b/index.js @@ -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'); + +/** + * 开始运行 + */ +console.log("Start running ..."); + +// 爬取热搜数据 get_hotband.main(); \ No newline at end of file diff --git a/src/execute_command.js b/src/execute_command.js index 76372e3..557016f 100644 --- a/src/execute_command.js +++ b/src/execute_command.js @@ -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 cmds = [ 'git status', diff --git a/src/get_hotband.js b/src/get_hotband.js index 5159a32..0346237 100644 --- a/src/get_hotband.js +++ b/src/get_hotband.js @@ -2,8 +2,10 @@ const request = require('request'); 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 接口 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); // 创建当前文件夹 - let folder = `./data/${year}/${month}/${day}`; + let folder = `${DATA_FOLDER}/${year}/${month}/${day}`; createFolder(folder); 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() { + console.log("DATA_FOLDER", DATA_FOLDER); + let requestTimestamp = Date.now(); let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString(); @@ -158,7 +162,6 @@ async function main() { * 两者差值通过观测似乎最大是 1250000 * 例如 【爆】唐山打架事件8名违法嫌疑人已到案 这条热搜一开始 delta 首先不断增大,最大达到 1250000 * 然后热搜数量增加到 12600000 左右的时候,delta 逐渐减小到 1040000 左右 - * 所有热搜的 detla(带正负) 加起来就是基本上在100000-230000之间 */ num: item.num, 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_friendly: now.substring(0, 19).replace(/T/g, " "), regulation: convert2,