sync_publish_time_hy_mf.js 4.3 KB

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