添加B站排行榜接口拉取
This commit is contained in:
		@@ -22,6 +22,11 @@ B站热搜接口:https://app.bilibili.com/x/v2/search/trending/ranking
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
> 该接口来自页面:https://www.bilibili.com/blackboard/activity-trending-topic.html
 | 
					> 该接口来自页面: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 系统下测试。
 | 
					原理上来说 Windows 下和 Linux 都可运行,目前仅在 Windows 下测试过,暂未在 Linux 系统下测试。
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								index.js
									
									
									
									
									
								
							@@ -9,7 +9,7 @@ const fs = require('fs');
 | 
				
			|||||||
/**
 | 
					/**
 | 
				
			||||||
 * 环境变量
 | 
					 * 环境变量
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
 if (!fs.existsSync('.env')) {
 | 
					if (!fs.existsSync('.env')) {
 | 
				
			||||||
    // 如果没有 .env 文件,则报错并退出
 | 
					    // 如果没有 .env 文件,则报错并退出
 | 
				
			||||||
    console.error('[ERROR] .env file not found!');
 | 
					    console.error('[ERROR] .env file not found!');
 | 
				
			||||||
    return;
 | 
					    return;
 | 
				
			||||||
@@ -38,6 +38,8 @@ if (DEBUG_MODE) {
 | 
				
			|||||||
 */
 | 
					 */
 | 
				
			||||||
const get_weibo_hotband = require('./src/get_weibo_hotband');
 | 
					const get_weibo_hotband = require('./src/get_weibo_hotband');
 | 
				
			||||||
const get_bilibili_hotband = require('./src/get_bilibili_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');
 | 
					const execute_command = require('./src/execute_command');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -54,6 +56,7 @@ async function start() {
 | 
				
			|||||||
    // 爬取热搜数据
 | 
					    // 爬取热搜数据
 | 
				
			||||||
    await get_weibo_hotband.main();
 | 
					    await get_weibo_hotband.main();
 | 
				
			||||||
    await get_bilibili_hotband.main();
 | 
					    await get_bilibili_hotband.main();
 | 
				
			||||||
 | 
					    await get_bilibili_rank.main();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // 调试模式下
 | 
					    // 调试模式下
 | 
				
			||||||
    if (DEBUG_MODE) {
 | 
					    if (DEBUG_MODE) {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										87
									
								
								src/get_bilibili_rank.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										87
									
								
								src/get_bilibili_rank.js
									
									
									
									
									
										Normal 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;
 | 
				
			||||||
		Reference in New Issue
	
	Block a user