sync_publish_time_hy_mf.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //同步黑岩免费的发布时间
  2. const config = require("../etc/config.json");
  3. const mysql = require('mysql2/promise');
  4. const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  5. const redis_help = require('../src/use_redis');
  6. const hy_search_book = require('../src/api/hy/hy_search_book');
  7. const helper = require("../src/helper");
  8. const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
  9. const filter_data_controllers = require('../src/data_manager/Controllers/filter_data_controllers');
  10. const tools = require("../tools");
  11. const CMD = {}
  12. async function processTask(){
  13. let right_status = true
  14. let connection = null
  15. try{
  16. connection = await mysql.createConnection({
  17. ...taskdbConfig,
  18. multipleStatements: true
  19. });
  20. //先获取100本没有发布时间的黑岩书籍
  21. let sql = `SELECT * FROM video_product WHERE book_platform = ${config.platform_heiyanmf} AND publish_time IS NULL LIMIT 500`
  22. console.log("sql:",sql)
  23. const [rows] = await connection.execute(
  24. sql
  25. );
  26. if(rows.length<=0){
  27. throw 0
  28. }
  29. for (let index = 0; index < rows.length; index++) {
  30. let video_product_info = rows[index]
  31. // let data =
  32. // console.log("data:",data)
  33. let data = await CMD.search_parent_id(connection,video_product_info.product_parent_id)
  34. if(data!=null){
  35. let publish_time = helper.getDate7DaysBefore(data.publish_time,null,'YYYY-MM-DD')
  36. await connection.execute(
  37. `UPDATE video_product SET publish_time = ? WHERE id = ${video_product_info.id} `,
  38. [publish_time]
  39. );
  40. }else{
  41. let hy_book_data = await CMD.add_parent_book(video_product_info.product_parent_id)
  42. if(hy_book_data!=null){
  43. let publish_time = hy_book_data.openTime
  44. if(publish_time==undefined){
  45. publish_time = hy_book_data.createTime
  46. }
  47. let words = hy_book_data.words
  48. let book_name = hy_book_data.name;
  49. let genre = 1
  50. if(words>100000){
  51. genre = 1;
  52. }else{
  53. genre = 3;
  54. }
  55. await CMD.insert_product({
  56. tg_platform_id:config.platform_heiyan,
  57. book_name:book_name,
  58. genre:genre,
  59. book_id:video_product_info.product_parent_id,
  60. words:words,
  61. publish_time:publish_time
  62. })
  63. }else{
  64. await connection.execute(
  65. ` UPDATE video_product SET publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  66. );
  67. console.error("查无此书:",video_product_info,hy_book_data)
  68. }
  69. }
  70. }
  71. }catch(e){
  72. if(e==0){
  73. right_status = false
  74. }
  75. console.error("processTask error:",e)
  76. } finally{
  77. if(connection!=null){
  78. connection.end()
  79. }
  80. global.setTimeout(processTask, 2000);
  81. }
  82. }
  83. CMD.add_parent_book = async function(product_id){
  84. let data = await hy_search_book.search_id(product_id)
  85. if(data==undefined||data==null){
  86. data = null
  87. }
  88. if(data!=null){
  89. if(data.rows==null||data.rows==undefined){
  90. data = null
  91. }
  92. }
  93. if(data!=null){
  94. if(data.rows.length<=0){
  95. data = null
  96. }
  97. }
  98. if(data!=null){
  99. return data.rows[0]
  100. }
  101. return data
  102. }
  103. CMD.search_parent_id = async function(connection,product_parent_id) {
  104. let sql = `SELECT * FROM video_product WHERE product_id = '${product_parent_id}' LIMIT 1`
  105. const [rows] = await connection.execute(
  106. sql
  107. );
  108. if(rows.length<=0){
  109. return null
  110. }
  111. return rows[0]
  112. }
  113. CMD.insert_product = async function(data){
  114. await video_product_controllers.createProductData({
  115. book_platform:data.tg_platform_id,
  116. product_name:data.book_name,
  117. genre:data.genre,
  118. product_id:data.book_id,
  119. words:data.words,
  120. publish_time:data.publish_time
  121. })
  122. }
  123. CMD.init = async function(){
  124. redis_help.connect((results)=>{
  125. if(results){
  126. processTask();
  127. }
  128. })
  129. }
  130. CMD.init()