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

添加B站排行榜接口拉取

This commit is contained in:
程序员小墨 2022-07-29 16:21:55 +08:00
parent 5bb9ac6235
commit f2dcfe274e
3 changed files with 96 additions and 1 deletions

View File

@ -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 系统下测试。

View File

@ -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) {

87
src/get_bilibili_rank.js Normal file
View File

@ -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;