DATA_VIDEO_ID.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. const config = require("../etc/config.json");
  2. const helper = require("../src/helper");
  3. const redis_help = require('../src/use_redis');
  4. const mysql = require('mysql2/promise');
  5. const PullDataService = require('../src/PullDataService');
  6. var pullDataService = new PullDataService(redis_help)
  7. const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  8. const CMD = {}
  9. async function processTask(){
  10. let connection = null
  11. try{
  12. const today = helper.getLocalDate();
  13. let date = today.replace(/-/g, '');
  14. let table_name = `pull_data_${date}`;
  15. // 创建数据库连接
  16. connection = await mysql.createConnection({
  17. ...dbConfig,
  18. multipleStatements: true
  19. });
  20. // 检查表是否存在
  21. const [tableCheck] = await connection.execute(
  22. `SELECT TABLE_NAME
  23. FROM information_schema.tables
  24. WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`,
  25. [dbConfig.database, table_name]
  26. );
  27. if (tableCheck.length === 0) {
  28. // 表不存在
  29. console.error(`表 ${table_name} 不存在`);
  30. throw new Error(`表 ${table_name} 不存在`);
  31. } else {
  32. // 表存在,执行查询
  33. const [rows] = await connection.execute(
  34. `SELECT * FROM ${table_name} WHERE status = 0 LIMIT 500`
  35. );
  36. if(rows.length<=0){
  37. throw "没有数据处理!"
  38. }
  39. let materialId_list = []
  40. rows.forEach((row, index) => {
  41. materialId_list.push(row.materialId)
  42. });
  43. let response = await pullDataService.get_detail(materialId_list)
  44. console.log("response:", response.data.list.length)
  45. if(response.success){
  46. for (let index = 0; index < parseInt(response.data.list.length); index++) {
  47. const element = response.data.list[index];
  48. console.log("element:",element)
  49. if(element.videoId){
  50. let landingUrl = element.landingUrl == undefined ?"":decodeURIComponent(element.landingUrl)
  51. await connection.execute(
  52. `UPDATE ${table_name}
  53. SET videoId = ?,
  54. status = 1 ,
  55. landingUrl = ? ,
  56. publishTime = ?
  57. WHERE uniqueId = ?`,
  58. [element.videoId,landingUrl,new Date(element.publishTime),element.uniqueId]
  59. );
  60. }
  61. }
  62. }
  63. }
  64. }catch(e){
  65. console.error("processTask error:",e)
  66. } finally{
  67. global.setTimeout(processTask, 1000);
  68. if(connection!=null){
  69. await connection.end();
  70. }
  71. }
  72. }
  73. CMD.init = async function(){
  74. redis_help.connect((results)=>{
  75. if(results){
  76. processTask();
  77. }
  78. })
  79. }
  80. CMD.init()