sync_publish_time_hy_mf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 = ${publish_time} WHERE id = ${video_product_info.id} `
  36. );
  37. }else{
  38. let hy_book_data = await CMD.add_parent_book(video_product_info.product_parent_id)
  39. if(hy_book_data!=null){
  40. let publish_time = hy_book_data.createTime
  41. let words = hy_book_data.words
  42. let book_name = hy_book_data.name;
  43. let genre = 1
  44. if(words>100000){
  45. genre = 1;
  46. }else{
  47. genre = 3;
  48. }
  49. await CMD.insert_product({
  50. tg_platform_id:config.platform_heiyan,
  51. book_name:book_name,
  52. genre:genre,
  53. book_id:video_product_info.product_parent_id,
  54. words:words,
  55. publish_time:publish_time
  56. })
  57. }else{
  58. await connection.execute(
  59. ` UPDATE video_product SET publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  60. );
  61. console.error("查无此书:",video_product_info,hy_book_data)
  62. }
  63. }
  64. }
  65. }catch(e){
  66. if(e==0){
  67. right_status = false
  68. }
  69. console.error("processTask error:",e)
  70. } finally{
  71. if(connection!=null){
  72. connection.end()
  73. }
  74. global.setTimeout(processTask, 2000);
  75. }
  76. }
  77. CMD.add_parent_book = async function(product_id){
  78. let data = await hy_search_book.search_id(product_id)
  79. if(data==undefined||data==null){
  80. data = null
  81. }
  82. if(data!=null){
  83. if(data.rows==null||data.rows==undefined){
  84. data = null
  85. }
  86. }
  87. if(data!=null){
  88. if(data.rows.length<=0){
  89. data = null
  90. }
  91. }
  92. if(data!=null){
  93. return data.rows[0]
  94. }
  95. return data
  96. }
  97. CMD.search_parent_id = async function(connection,product_parent_id) {
  98. let sql = `SELECT * FROM video_product WHERE product_id = '${product_parent_id}' LIMIT 1`
  99. const [rows] = await connection.execute(
  100. sql
  101. );
  102. if(rows.length<=0){
  103. return null
  104. }
  105. return rows[0]
  106. }
  107. CMD.insert_product = async function(data){
  108. await video_product_controllers.createProductData({
  109. book_platform:data.tg_platform_id,
  110. product_name:data.book_name,
  111. genre:data.genre,
  112. product_id:data.book_id,
  113. words:data.words,
  114. publish_time:data.publish_time
  115. })
  116. }
  117. CMD.init = async function(){
  118. redis_help.connect((results)=>{
  119. if(results){
  120. processTask();
  121. }
  122. })
  123. }
  124. CMD.init()