请求API抽离出独立方法 requestUtils
This commit is contained in:
parent
68812bc7ca
commit
05ea84f47e
@ -1,49 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const request = require('request');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const utils = require('./utils/utils');
|
||||
const fileUtils = require('./utils/fileUtils');
|
||||
const requestUtils = require('./utils/requestUtils');
|
||||
|
||||
const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data', 'bilibili');
|
||||
console.log("DATA_FOLDER", DATA_FOLDER);
|
||||
utils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在
|
||||
|
||||
// 请求微博热搜 APi 接口
|
||||
async function getApiResult(url) {
|
||||
var return_data = await new Promise((resolve) => {
|
||||
request({
|
||||
method: 'GET',
|
||||
url: url,
|
||||
json: true,
|
||||
}, (error, response, result) => {
|
||||
if (!error && (response.statusCode == 200)) {
|
||||
// 请求成功
|
||||
resolve(result);
|
||||
} else {
|
||||
// 请求失败
|
||||
console.log(`error is ${error}`);
|
||||
resolve("error");
|
||||
}
|
||||
});
|
||||
});
|
||||
// console.log(`return_data is ${JSON.stringify(return_data)}`);
|
||||
return return_data;
|
||||
}
|
||||
fileUtils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在
|
||||
|
||||
async function main() {
|
||||
let requestTimestamp = Date.now();
|
||||
let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString();
|
||||
|
||||
let result = await getApiResult("https://app.bilibili.com/x/v2/search/trending/ranking");
|
||||
let result = await requestUtils.getApiResult("https://app.bilibili.com/x/v2/search/trending/ranking");
|
||||
if (result.code != 0) {
|
||||
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "bilibili", "请求成功,但服务器处理失败,正在重试。");
|
||||
result = await getApiResult("https://app.bilibili.com/x/v2/search/trending/ranking");
|
||||
result = await requestUtils.getApiResult("https://app.bilibili.com/x/v2/search/trending/ranking");
|
||||
if (result.ok != 1) {
|
||||
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "bilibili", "请求成功,但服务器处理失败,保存失败信息。");
|
||||
// ok 不为 1,那么久直接保存便于后续分析,不进行后续处理
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `origin-error`,
|
||||
@ -67,7 +45,7 @@ async function main() {
|
||||
/**
|
||||
* 保存原始数据
|
||||
*/
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `origin`,
|
||||
@ -92,7 +70,7 @@ async function main() {
|
||||
// }
|
||||
convert.push(item);
|
||||
});
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `final`,
|
||||
|
@ -1,49 +1,27 @@
|
||||
'use strict';
|
||||
|
||||
const request = require('request');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
const utils = require('./utils/utils');
|
||||
const fileUtils = require('./utils/fileUtils');
|
||||
const requestUtils = require('./utils/requestUtils');
|
||||
|
||||
const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data', 'weibo');
|
||||
console.log("DATA_FOLDER", DATA_FOLDER);
|
||||
utils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在
|
||||
|
||||
// 请求微博热搜 APi 接口
|
||||
async function getApiResult(url) {
|
||||
var return_data = await new Promise((resolve) => {
|
||||
request({
|
||||
method: 'GET',
|
||||
url: url,
|
||||
json: true,
|
||||
}, (error, response, result) => {
|
||||
if (!error && (response.statusCode == 200)) {
|
||||
// 请求成功
|
||||
resolve(result);
|
||||
} else {
|
||||
// 请求失败
|
||||
console.log(`error is ${error}`);
|
||||
resolve("error");
|
||||
}
|
||||
});
|
||||
});
|
||||
// console.log(`return_data is ${JSON.stringify(return_data)}`);
|
||||
return return_data;
|
||||
}
|
||||
fileUtils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在
|
||||
|
||||
async function main() {
|
||||
let requestTimestamp = Date.now();
|
||||
let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString();
|
||||
|
||||
let result = await getApiResult("https://weibo.com/ajax/statuses/hot_band");
|
||||
let result = await requestUtils.getApiResult("https://weibo.com/ajax/statuses/hot_band");
|
||||
if (result.ok != 1) {
|
||||
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "weibo", "请求成功,但服务器处理失败,正在重试。");
|
||||
result = await getApiResult("https://weibo.com/ajax/statuses/hot_band");
|
||||
result = await requestUtils.getApiResult("https://weibo.com/ajax/statuses/hot_band");
|
||||
if (result.ok != 1) {
|
||||
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "weibo", "请求成功,但服务器处理失败,保存失败信息。");
|
||||
// ok 不为 1,那么久直接保存便于后续分析,不进行后续处理
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `origin-error`,
|
||||
@ -61,7 +39,7 @@ async function main() {
|
||||
/**
|
||||
* 保存原始数据
|
||||
*/
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `origin`,
|
||||
@ -159,7 +137,7 @@ async function main() {
|
||||
},
|
||||
});
|
||||
});
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `final`,
|
||||
@ -184,7 +162,7 @@ async function main() {
|
||||
`原始:${item.raw_hot} 显示:${item.num} 调控: ${item.num - item.raw_hot}`
|
||||
]);
|
||||
});
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `regulation`,
|
||||
@ -205,7 +183,7 @@ async function main() {
|
||||
data.band_list.forEach(function (item) {
|
||||
delete item["mblog"];
|
||||
});
|
||||
utils.saveJSON({
|
||||
fileUtils.saveJSON({
|
||||
saveFolder: DATA_FOLDER,
|
||||
now: now,
|
||||
fileNameSuffix: `simplify`,
|
||||
|
27
src/utils/requestUtils.js
Normal file
27
src/utils/requestUtils.js
Normal file
@ -0,0 +1,27 @@
|
||||
const request = require('request');
|
||||
|
||||
// 请求 APi 接口
|
||||
async function getApiResult(url) {
|
||||
var return_data = await new Promise((resolve) => {
|
||||
request({
|
||||
method: 'GET',
|
||||
url: url,
|
||||
json: true,
|
||||
}, (error, response, result) => {
|
||||
if (!error && (response.statusCode == 200)) {
|
||||
// 请求成功
|
||||
resolve(result);
|
||||
} else {
|
||||
// 请求失败
|
||||
console.log(`error is ${error}`);
|
||||
resolve("error");
|
||||
}
|
||||
});
|
||||
});
|
||||
// console.log(`return_data is ${JSON.stringify(return_data)}`);
|
||||
return return_data;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getApiResult,
|
||||
}
|
Loading…
Reference in New Issue
Block a user