'use strict'; const fs = require('fs'); const path = require('path'); const fileUtils = require('./utils/fileUtils'); const requestUtils = require('./utils/requestUtils'); const API_URL = "https://app.bilibili.com/x/v2/search/trending/ranking"; const SUB_FOLDER = "bilibili-hotband"; const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data', SUB_FOLDER); console.log("DATA_FOLDER", DATA_FOLDER); fileUtils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在 async function main() { let requestTimestamp = Date.now(); let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString(); let result = await requestUtils.getApiResult(API_URL); if (result.code != 0) { console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), SUB_FOLDER, "请求成功,但服务器处理失败,等待3s后重试。"); await new Promise((resolve) => { setTimeout(resolve, 3000); // 等待3秒 }); result = await requestUtils.getApiResult(API_URL); if (result.ok != 1) { console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), SUB_FOLDER, "请求成功,但服务器处理失败,保存失败信息。"); // ok 不为 1,那么久直接保存便于后续分析,不进行后续处理 fileUtils.saveJSON({ saveFolder: DATA_FOLDER, now: now, fileNameSuffix: `origin-error`, object: result, compress: true, uncompress: false }); return; } } console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), SUB_FOLDER, "请求成功"); // console.log("result", result); let data = result.data; // 去除 trackid delete data["trackid"]; // console.log(data); /** * 保存原始数据 */ fileUtils.saveJSON({ saveFolder: DATA_FOLDER, now: now, fileNameSuffix: `origin`, object: result, compress: true, uncompress: false }); /** * 获取需要的数据,进行转换 */ let convert = []; data.list.forEach(item => { // { // "position": 1, // "keyword": "关键词", // "show_name": "热搜名称", // "word_type": 8, // "icon": "热搜的图标,也可能没有", // "hot_id": 7399 // 热搜id // } convert.push(item); }); fileUtils.saveJSON({ saveFolder: DATA_FOLDER, now: now, fileNameSuffix: `final`, object: convert, compress: true, uncompress: false, }); /** * 更新最新的 */ fs.writeFileSync(`${DATA_FOLDER}/latest.json`, JSON.stringify({ update_time: requestTimestamp, update_time_friendly: now.substring(0, 19).replace(/T/g, " "), data: data.list, exp_str: data.exp_str, })); } exports.main = main;