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

请求API抽离出独立方法 requestUtils

This commit is contained in:
程序员小墨 2022-07-29 15:47:38 +08:00
parent 68812bc7ca
commit 05ea84f47e
4 changed files with 45 additions and 62 deletions

View File

@ -1,49 +1,27 @@
'use strict'; 'use strict';
const request = require('request');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); 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'); const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data', 'bilibili');
console.log("DATA_FOLDER", DATA_FOLDER); console.log("DATA_FOLDER", DATA_FOLDER);
utils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在 fileUtils.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;
}
async function main() { async function main() {
let requestTimestamp = Date.now(); let requestTimestamp = Date.now();
let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString(); 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) { if (result.code != 0) {
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "bilibili", "请求成功,但服务器处理失败,正在重试。"); 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) { if (result.ok != 1) {
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "bilibili", "请求成功,但服务器处理失败,保存失败信息。"); console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "bilibili", "请求成功,但服务器处理失败,保存失败信息。");
// ok 不为 1那么久直接保存便于后续分析不进行后续处理 // ok 不为 1那么久直接保存便于后续分析不进行后续处理
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `origin-error`, fileNameSuffix: `origin-error`,
@ -67,7 +45,7 @@ async function main() {
/** /**
* 保存原始数据 * 保存原始数据
*/ */
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `origin`, fileNameSuffix: `origin`,
@ -92,7 +70,7 @@ async function main() {
// } // }
convert.push(item); convert.push(item);
}); });
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `final`, fileNameSuffix: `final`,

View File

@ -1,49 +1,27 @@
'use strict'; 'use strict';
const request = require('request');
const fs = require('fs'); const fs = require('fs');
const path = require('path'); 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'); const DATA_FOLDER = path.join(path.dirname(__dirname), process.env.DATA_FOLDER ?? 'data', 'weibo');
console.log("DATA_FOLDER", DATA_FOLDER); console.log("DATA_FOLDER", DATA_FOLDER);
utils.createFolder(DATA_FOLDER); // 程序运行就保证 data 目录存在 fileUtils.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;
}
async function main() { async function main() {
let requestTimestamp = Date.now(); let requestTimestamp = Date.now();
let now = new Date(requestTimestamp + 8 * 3600 * 1000).toISOString(); 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) { if (result.ok != 1) {
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "weibo", "请求成功,但服务器处理失败,正在重试。"); 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) { if (result.ok != 1) {
console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "weibo", "请求成功,但服务器处理失败,保存失败信息。"); console.log(new Date(Date.now() + 8 * 60 * 60 * 1000).toISOString(), "weibo", "请求成功,但服务器处理失败,保存失败信息。");
// ok 不为 1那么久直接保存便于后续分析不进行后续处理 // ok 不为 1那么久直接保存便于后续分析不进行后续处理
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `origin-error`, fileNameSuffix: `origin-error`,
@ -61,7 +39,7 @@ async function main() {
/** /**
* 保存原始数据 * 保存原始数据
*/ */
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `origin`, fileNameSuffix: `origin`,
@ -159,7 +137,7 @@ async function main() {
}, },
}); });
}); });
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `final`, fileNameSuffix: `final`,
@ -184,7 +162,7 @@ async function main() {
`原始:${item.raw_hot} 显示:${item.num} 调控: ${item.num - item.raw_hot}` `原始:${item.raw_hot} 显示:${item.num} 调控: ${item.num - item.raw_hot}`
]); ]);
}); });
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `regulation`, fileNameSuffix: `regulation`,
@ -205,7 +183,7 @@ async function main() {
data.band_list.forEach(function (item) { data.band_list.forEach(function (item) {
delete item["mblog"]; delete item["mblog"];
}); });
utils.saveJSON({ fileUtils.saveJSON({
saveFolder: DATA_FOLDER, saveFolder: DATA_FOLDER,
now: now, now: now,
fileNameSuffix: `simplify`, fileNameSuffix: `simplify`,

27
src/utils/requestUtils.js Normal file
View 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,
}