const axios = require('axios'); const config = require('../etc/config.json'); const helper = require('./helper'); class PullDataService { constructor(redis_help) { this.mRedis = redis_help this.session = axios.create({ baseURL: config.isDebug?config.pull_data_config.debug_host:config.pull_data_config.release_host, headers: { 'Accept': 'application/json, text/plain, */*', } }); this.token = "" } async getToken() { try { let maxRetries = 3 let delay = 1000; for (let i = 0; i < maxRetries; i++) { const response = await this.session.post(config.pull_data_config.get_token,{ userName:config.pull_data_config.userName, verCode:config.pull_data_config.verCode, password:config.pull_data_config.release_password, loginType:config.pull_data_config.loginType }); if(!response.data.success){ if (i === maxRetries - 1) throw response; console.log(`Retry getToken ${i + 1} of ${maxRetries}`); await new Promise(resolve => setTimeout(resolve, delay * (i + 1))); }else{ this.token = response.data.data.token await this.mRedis.setKeyValue("pull_data_token",this.token) return response.data; } } } catch (error) { return {data:null,success:false,msg:error} } } async get_novel_material_list(cur_timeRange,page,size=500) { let maxRetries = 3 let delay = 1000; let otherStr = null if(cur_timeRange.pull_day!=null&&cur_timeRange.pull_day!=""&&cur_timeRange.pull_day!=undefined){ otherStr = cur_timeRange.pull_day } for (let i = 0; i < maxRetries; i++) { try { var now = new Date(); // 当前时间 if(cur_timeRange!=null){ if(cur_timeRange.start_time==null||cur_timeRange.start_time==""||cur_timeRange.start_time==undefined){ now = helper.getTimeStampByHourMinute(cur_timeRange.start,otherStr) }else{ now = helper.getTimeStampByHourMinute(cur_timeRange.start_time,otherStr) } } let interval_minute = 60; if(cur_timeRange!=null){ interval_minute = cur_timeRange.interval_minute } // const fiveMinutesAgo = new Date(now - 5 * 60 * 1000); // 5分钟前的时间 const fiveMinutesAgo = new Date(now - interval_minute * 60 * 1000); // 60分钟前的时间 let data = { startTime:fiveMinutesAgo.getTime(), endTime:now.getTime(), count:false } if(cur_timeRange!=null){ cur_timeRange.pull_time = data } let params = `page=${page}&size=${size}&sort=update_time,desc` let url = config.pull_data_config.get_novel_material_list+params console.log(url,data) var response = await this.session.post(url,data, { headers: { 'token':await this.mRedis.getKeyValue("pull_data_token") } }); if(!response.data.success){ throw response.data }else{ return response.data; } } catch (error) { if (i === maxRetries - 1) return {data:null,success:false,msg:error}; console.log(`Retry get_novel_material_list ${i + 1} of ${maxRetries}`); await new Promise(resolve => setTimeout(resolve, delay * (i + 1))); } } } async get_detail(materialId_list) { let maxRetries = 3 let delay = 1000; for (let i = 0; i < maxRetries; i++) { try { let data = { list:materialId_list, count:false } // console.log("config.pull_data_config.get_detail:",data) const response = await this.session.post(config.pull_data_config.get_detail,data, { headers: { 'token':await this.mRedis.getKeyValue("pull_data_token") } }); if(!response.data.success){ throw response.data }else{ return response.data; } } catch (error) { if (i === maxRetries - 1) return {data:null,success:false,msg:error}; console.log(`Retry get_detail ${i + 1} of ${maxRetries}`); await new Promise(resolve => setTimeout(resolve, delay * (i + 1))); } } } } module.exports = PullDataService;