diff --git a/README.md b/README.md index 0e570aa..c50ab09 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,11 @@ B站热搜接口:https://app.bilibili.com/x/v2/search/trending/ranking > 该接口来自页面:https://www.bilibili.com/blackboard/activity-trending-topic.html +B站排行榜接口:https://api.bilibili.com/x/web-interface/ranking/v2?type=all + +> 该接口来自页面:https://www.bilibili.com/v/popular/rank/all +> (切换到其他榜单再切换回来会调用此接口) + ## 运行环境 原理上来说 Windows 下和 Linux 都可运行,目前仅在 Windows 下测试过,暂未在 Linux 系统下测试。 diff --git a/index.js b/index.js index f786500..3ba44d1 100644 --- a/index.js +++ b/index.js @@ -9,7 +9,7 @@ const fs = require('fs'); /** * 环境变量 */ - if (!fs.existsSync('.env')) { +if (!fs.existsSync('.env')) { // 如果没有 .env 文件,则报错并退出 console.error('[ERROR] .env file not found!'); return; @@ -38,6 +38,8 @@ if (DEBUG_MODE) { */ 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'); @@ -54,6 +56,7 @@ async function start() { // 爬取热搜数据 await get_weibo_hotband.main(); await get_bilibili_hotband.main(); + await get_bilibili_rank.main(); // 调试模式下 if (DEBUG_MODE) { diff --git a/src/get_bilibili_rank.js b/src/get_bilibili_rank.js new file mode 100644 index 0000000..dec154b --- /dev/null +++ b/src/get_bilibili_rank.js @@ -0,0 +1,87 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const fileUtils = require('./utils/fileUtils'); +const requestUtils = require('./utils/requestUtils'); + +const API_URL = "https://api.bilibili.com/x/web-interface/ranking/v2?type=all"; +const SUB_FOLDER = "bilibili-rank"; + +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, "请求成功,但服务器处理失败,正在重试。"); + 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 => { + // 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, " "), + note: data.note, + data: data.list, + })); +} + +exports.main = main;