PULL_DATA.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. const CMD = {}
  2. const time_count = 5000;
  3. const redis_help = require('../src/use_redis');
  4. const origin_data_controllers = require('../src/data_manager/Controllers/origin_data_controllers');
  5. const pull_log_controllers = require('../src/data_manager/Controllers/pull_log_controllers');
  6. const helper = require('../src/helper');
  7. const axios = require('axios')
  8. const HttpClient = require('../src/HttpClient')
  9. const config = require('../etc/config.json');
  10. const DailyRecordManager = require('./daily_records');
  11. const schedule = require('node-schedule');
  12. const manager = new DailyRecordManager();
  13. var session = null
  14. var cur_page = 0
  15. var task_list = []
  16. var _24HourRanges = []
  17. var jobs = []
  18. var cur_day_data = []
  19. async function processTask(timeRange){
  20. try{
  21. if(cur_page==-1){
  22. throw "没有数据了"
  23. }
  24. let FilterConfig = await redis_help.getKeyValue("FilterConfig")
  25. FilterConfig = JSON.parse(FilterConfig)
  26. let response = await CMD.get_novel_material_list(cur_page)
  27. // console.log("get_novel_material_list:",response)
  28. let materialId_list = []
  29. let materialId_data_list = []
  30. if(response.result.list == undefined){
  31. cur_page = -1
  32. throw "没有数据了"
  33. }
  34. if(response.result.list.length<=0){
  35. cur_page = -1
  36. throw "没有数据了"
  37. }
  38. if(timeRange!=null){
  39. const today = new Date().toISOString().split('T')[0];
  40. cur_day_data[today][timeRange.start].count = response.result.count
  41. const updateResult = await manager.updateRecord(today,cur_day_data[timeRange.today]);
  42. }
  43. for (let index = 0; index < response.result.list.length; index++) {
  44. const origin_element = response.result.list[index];
  45. const result = await origin_data_controllers.getOriginData({
  46. materialId:origin_element.materialId})
  47. if(result.success){
  48. await origin_data_controllers.updateOriginData({id:result.data.id},
  49. {
  50. kepp_num:origin_element.favoriteCount.count,
  51. comment_num:origin_element.commentCount.count,
  52. like_num:origin_element.likeCount.count,
  53. shared_num:origin_element.shareCount.count
  54. })
  55. }else{
  56. if(CMD.isRight(FilterConfig,origin_element)){
  57. materialId_list.push(origin_element.materialId)
  58. materialId_data_list[origin_element.materialId] = {
  59. kepp_num:origin_element.favoriteCount.count,
  60. comment_num:origin_element.commentCount.count,
  61. like_num:origin_element.likeCount.count,
  62. shared_num:origin_element.shareCount.count
  63. }
  64. }
  65. }
  66. }
  67. response = await CMD.get_detail(materialId_list)
  68. for (let index = 0; index < response.result.list.length; index++) {
  69. const element = response.result.list[index];
  70. const info = materialId_data_list[element.materialId]
  71. if(element.hasAnchorInfo){
  72. await origin_data_controllers.createOriginData(
  73. {
  74. video_id:element.awemeId,
  75. materialId:element.materialId,
  76. video_link:element.awemeUrl,
  77. title:element.title,
  78. publish_time:new Date(element.publishTime),
  79. kepp_num:info.kepp_num,
  80. comment_num:info.comment_num,
  81. like_num:info.like_num,
  82. shared_num:info.shared_num,
  83. is_guajian:1,
  84. guajian_link:element.landingUrl,
  85. status:0
  86. }
  87. )
  88. }
  89. }
  90. // console.log("get_detail:",response)
  91. // await pull_log_controllers.createData()
  92. }catch(e){
  93. console.log("PULL_DATA: error:",e)
  94. }finally{
  95. if(cur_page!=-1){
  96. cur_page++;
  97. global.setTimeout(processTask,time_count)
  98. }
  99. }
  100. }
  101. CMD.isRight = function(FilterConfig,origin_element){
  102. if(origin_element.likeCount.count < FilterConfig.like_num){// console.log("点赞数正确")
  103. return false
  104. }
  105. if(origin_element.favoriteCount.count < FilterConfig.kepp_num){ // console.log("收藏数正确")
  106. return false
  107. }
  108. if(origin_element.shareCount.count < FilterConfig.shared_num){ // console.log("分享数正确")
  109. return false
  110. }
  111. if(origin_element.commentCount.count < FilterConfig.comment_num){ // console.log("评论")
  112. return false
  113. }
  114. return true
  115. }
  116. CMD.getToken = async function(){
  117. const response = await session.post(config.pull_data_config.get_token,{
  118. userName:config.pull_data_config.userName,
  119. verCode:"1234",
  120. password:config.pull_data_config.password,
  121. loginType:"OPENAPI"
  122. });
  123. return response.data
  124. }
  125. CMD.get_novel_material_list = async function(page,size=500) {
  126. const now = new Date(); // 当前时间
  127. // const fiveMinutesAgo = new Date(now - 5 * 60 * 1000); // 5分钟前的时间
  128. const fiveMinutesAgo = new Date(now - 60 * 60 * 1000); // 60分钟前的时间
  129. let data = {
  130. startTime:fiveMinutesAgo.getTime(),
  131. endTime:now.getTime()
  132. }
  133. let params = `page=${page}&size=${size}&sort=update_time,desc`
  134. let url = config.pull_data_config.get_novel_material_list+params
  135. console.log(url,data)
  136. const response = await session.post(url,data, {
  137. headers: {
  138. 'token':await redis_help.getKeyValue("pull_data_token")
  139. }
  140. });
  141. return response.data
  142. }
  143. CMD.get_detail = async function(materialId_list) {
  144. let data = {
  145. list:materialId_list
  146. }
  147. const response = await session.post(config.pull_data_config.get_detail,data, {
  148. headers: {
  149. 'token':await redis_help.getKeyValue("pull_data_token")
  150. }
  151. });
  152. return response.data
  153. }
  154. CMD.init = async function(){
  155. session = axios.create({
  156. baseURL: config.isDebug?config.pull_data_config.debug_host:config.pull_data_config.release_host,
  157. headers: {
  158. 'Accept': 'application/json, text/plain, */*',
  159. }
  160. });
  161. redis_help.connect(async ()=>{
  162. let response = await CMD.getToken()
  163. console.log("getToken:",response.result.token)
  164. await redis_help.setKeyValue("pull_data_token",response.result.token)
  165. // processTask()
  166. _24HourRanges = CMD.generate24HourRanges()
  167. // 定义任务回调函数
  168. const taskCallback = async (context) => {
  169. var { timeRange, executionTime } = context;
  170. console.log('执行任务:', {
  171. 时间区间: timeRange.name,
  172. 开始时间: timeRange.start,
  173. 执行时间: executionTime
  174. });
  175. const today = new Date().toISOString().split('T')[0];
  176. for (const key in cur_day_data) {
  177. if (Object.prototype.hasOwnProperty.call(cur_day_data, key)) {
  178. if(key==today){
  179. }else{
  180. cur_day_data[key] = []
  181. }
  182. }
  183. }
  184. if(!cur_day_data[today]){
  185. cur_day_data[today] = {}
  186. }
  187. if(!cur_day_data[today][timeRange.start]){
  188. cur_day_data[today][timeRange.start] = {name:timeRange.name}
  189. }
  190. console.log("cur_day_data[today]:",cur_day_data[today])
  191. const result = await manager.createRecord(today,cur_day_data[today]);
  192. if(result.success){
  193. }else{
  194. cur_day_data[today] = await manager.getRecord(today);
  195. cur_day_data[today][timeRange.start] = {}
  196. }
  197. cur_page = 0
  198. // 示例:实际任务逻辑
  199. await processTask(timeRange);
  200. };
  201. // 启动调度器
  202. CMD.startScheduler(taskCallback);
  203. })
  204. }
  205. // 生成24小时的时间区间
  206. CMD.generate24HourRanges = function() {
  207. // const ranges = [];
  208. // const today = new Date();
  209. // today.setMinutes(0);
  210. // today.setSeconds(0);
  211. // today.setMilliseconds(0);
  212. // for (let hour = 0; hour < 24; hour++) {
  213. // const timeString = `${hour.toString().padStart(2, '0')}:00`;
  214. // let periodName = '';
  215. // // 设置当天的小时
  216. // today.setHours(hour);
  217. // const timestamp = today.getTime();
  218. // // 根据时间划分时段
  219. // if (hour >= 0 && hour < 6) {
  220. // periodName = '凌晨区间';
  221. // } else if (hour >= 6 && hour < 9) {
  222. // periodName = '早晨区间';
  223. // } else if (hour >= 9 && hour < 12) {
  224. // periodName = '上午区间';
  225. // } else if (hour >= 12 && hour < 14) {
  226. // periodName = '中午区间';
  227. // } else if (hour >= 14 && hour < 18) {
  228. // periodName = '下午区间';
  229. // } else if (hour >= 18 && hour < 22) {
  230. // periodName = '晚上区间';
  231. // } else {
  232. // periodName = '深夜区间';
  233. // }
  234. // ranges.push({
  235. // name: periodName,
  236. // start: timeString,
  237. // timestamp: timestamp,
  238. // hour:hour,
  239. // timeFormat: new Date(timestamp).toLocaleString() // 可读的时间格式
  240. // });
  241. // }
  242. let ranges = [
  243. {
  244. name: '凌晨区间',
  245. start: '00:00',
  246. timestamp: 1733760000000,
  247. hour: 0,
  248. timeFormat: '12/10/2024, 12:00:00 AM'
  249. },
  250. {
  251. name: '凌晨区间',
  252. start: '01:00',
  253. timestamp: 1733763600000,
  254. hour: 1,
  255. timeFormat: '12/10/2024, 1:00:00 AM'
  256. },
  257. {
  258. name: '凌晨区间',
  259. start: '02:00',
  260. timestamp: 1733767200000,
  261. hour: 2,
  262. timeFormat: '12/10/2024, 2:00:00 AM'
  263. },
  264. {
  265. name: '凌晨区间',
  266. start: '03:00',
  267. timestamp: 1733770800000,
  268. hour: 3,
  269. timeFormat: '12/10/2024, 3:00:00 AM'
  270. },
  271. {
  272. name: '凌晨区间',
  273. start: '04:00',
  274. timestamp: 1733774400000,
  275. hour: 4,
  276. timeFormat: '12/10/2024, 4:00:00 AM'
  277. },
  278. {
  279. name: '凌晨区间',
  280. start: '05:00',
  281. timestamp: 1733778000000,
  282. hour: 5,
  283. timeFormat: '12/10/2024, 5:00:00 AM'
  284. },
  285. {
  286. name: '早晨区间',
  287. start: '06:00',
  288. timestamp: 1733781600000,
  289. hour: 6,
  290. timeFormat: '12/10/2024, 6:00:00 AM'
  291. },
  292. {
  293. name: '早晨区间',
  294. start: '07:00',
  295. timestamp: 1733785200000,
  296. hour: 7,
  297. timeFormat: '12/10/2024, 7:00:00 AM'
  298. },
  299. {
  300. name: '早晨区间',
  301. start: '08:00',
  302. timestamp: 1733788800000,
  303. hour: 8,
  304. timeFormat: '12/10/2024, 8:00:00 AM'
  305. },
  306. {
  307. name: '上午区间',
  308. start: '09:00',
  309. timestamp: 1733792400000,
  310. hour: 9,
  311. timeFormat: '12/10/2024, 9:00:00 AM'
  312. },
  313. {
  314. name: '上午区间',
  315. start: '10:00',
  316. timestamp: 1733796000000,
  317. hour: 10,
  318. timeFormat: '12/10/2024, 10:00:00 AM'
  319. },
  320. {
  321. name: '上午区间',
  322. start: '11:00',
  323. timestamp: 1733799600000,
  324. hour: 11,
  325. timeFormat: '12/10/2024, 11:00:00 AM'
  326. },
  327. {
  328. name: '中午区间',
  329. start: '12:00',
  330. timestamp: 1733803200000,
  331. hour: 12,
  332. timeFormat: '12/10/2024, 12:00:00 PM'
  333. },
  334. {
  335. name: '中午区间',
  336. start: '13:00',
  337. timestamp: 1733806800000,
  338. hour: 13,
  339. timeFormat: '12/10/2024, 1:00:00 PM'
  340. },
  341. {
  342. name: '下午区间',
  343. start: '14:00',
  344. timestamp: 1733810400000,
  345. hour: 14,
  346. timeFormat: '12/10/2024, 2:00:00 PM'
  347. },
  348. {
  349. name: '下午区间',
  350. start: '15:00',
  351. timestamp: 1733814000000,
  352. hour: 15,
  353. timeFormat: '12/10/2024, 3:00:00 PM'
  354. },
  355. {
  356. name: '下午区间',
  357. start: '16:35',
  358. timestamp: 1733817600000,
  359. hour: 16,
  360. timeFormat: '12/10/2024, 4:00:00 PM'
  361. },
  362. {
  363. name: '下午区间',
  364. start: '17:00',
  365. timestamp: 1733821200000,
  366. hour: 17,
  367. timeFormat: '12/10/2024, 5:00:00 PM'
  368. },
  369. {
  370. name: '晚上区间',
  371. start: '18:00',
  372. timestamp: 1733824800000,
  373. hour: 18,
  374. timeFormat: '12/10/2024, 6:00:00 PM'
  375. },
  376. {
  377. name: '晚上区间',
  378. start: '19:00',
  379. timestamp: 1733828400000,
  380. hour: 19,
  381. timeFormat: '12/10/2024, 7:00:00 PM'
  382. },
  383. {
  384. name: '晚上区间',
  385. start: '20:00',
  386. timestamp: 1733832000000,
  387. hour: 20,
  388. timeFormat: '12/10/2024, 8:00:00 PM'
  389. },
  390. {
  391. name: '晚上区间',
  392. start: '21:00',
  393. timestamp: 1733835600000,
  394. hour: 21,
  395. timeFormat: '12/10/2024, 9:00:00 PM'
  396. },
  397. {
  398. name: '深夜区间',
  399. start: '22:00',
  400. timestamp: 1733839200000,
  401. hour: 22,
  402. timeFormat: '12/10/2024, 10:00:00 PM'
  403. },
  404. {
  405. name: '深夜区间',
  406. start: '23:00',
  407. timestamp: 1733842800000,
  408. hour: 23,
  409. timeFormat: '12/10/2024, 11:00:00 PM'
  410. }
  411. ]
  412. return ranges;
  413. }
  414. CMD.startScheduler = function(taskCallback) {
  415. // 为每个时间点创建定时任务
  416. _24HourRanges.forEach(timeRange => {
  417. // 解析小时
  418. const hour = parseInt(timeRange.start.split(':')[0]);
  419. const minute = parseInt(timeRange.start.split(':')[1]);
  420. // 创建定时任务 - 在每天的指定小时整点执行
  421. const job = schedule.scheduleJob(`${minute} ${hour} * * *`, async () => {
  422. try {
  423. console.log(`开始执行任务: ${timeRange.name} ${timeRange.start}`);
  424. // 执行回调函数
  425. await taskCallback({
  426. timeRange,
  427. executionTime: new Date()
  428. });
  429. } catch (error) {
  430. // 记录失败
  431. console.error('任务执行错误:', error);
  432. }
  433. });
  434. jobs.push({
  435. job,
  436. timeRange
  437. });
  438. console.log(`已安排任务: ${timeRange.name} ${timeRange.start}`);
  439. });
  440. console.log(`调度器已启动,共设置 ${jobs.length} 个定时任务`);
  441. }
  442. CMD.init()