sync_publish_time_fq_mf.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
  7. const helper = require("../src/helper");
  8. const CMD = {}
  9. async function processTask(){
  10. let right_status = true
  11. let connection = null
  12. try{
  13. connection = await mysql.createConnection({
  14. ...taskdbConfig,
  15. multipleStatements: true
  16. });
  17. //先获取100本没有发布时间的番茄免费书籍
  18. let sql = `SELECT * FROM video_product WHERE book_platform = ${config.platform_fanqiemf} AND product_parent_id !="" AND publish_time IS NULL LIMIT 5`
  19. console.log("sql:",sql)
  20. const [rows] = await connection.execute(
  21. sql
  22. );
  23. if(rows.length<=0){
  24. throw 0
  25. }
  26. for (let index = 0; index < rows.length; index++) {
  27. let video_product_info = rows[index]
  28. if(video_product_info.product_parent_id==video_product_info.product_id){
  29. await connection.execute(
  30. ` UPDATE video_product SET publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  31. );
  32. }else{
  33. let book_info = await video_product_controllers.getProductData({book_id:video_product_info.product_parent_id})
  34. if(book_info.success){
  35. if(book_info.data.publish_time!=null){
  36. let publish_time = helper.getDate7DaysBefore(book_info.data.publish_time,null,'YYYY-MM-DD')
  37. await connection.execute(
  38. `UPDATE video_product SET publish_time = ? WHERE id = ${video_product_info.id} `,[publish_time]
  39. );
  40. }
  41. }else{
  42. //添加到番茄付费里
  43. let data = await require('../src/api/fq/fq_search_book').search_new_id(video_product_info.product_parent_id)
  44. if(data!=null){
  45. let publish_time = data.publish_time
  46. let words = data.words
  47. let book_name = data.book_name;
  48. let genre = 1
  49. if(words>100000){
  50. genre = 1;
  51. }else{
  52. genre = 3;
  53. }
  54. await CMD.insert_product({
  55. tg_platform_id:config.platform_fanqie,
  56. book_name:book_name,
  57. genre:genre,
  58. book_id:video_product_info.product_parent_id,
  59. words:words,
  60. publish_time:publish_time
  61. })
  62. }else{
  63. console.error("没找父书:",video_product_info.product_parent_id)
  64. await connection.execute(
  65. ` UPDATE video_product SET status = 0 , publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  66. );
  67. console.error("查无此书:",video_product_info)
  68. }
  69. }
  70. }
  71. }
  72. }catch(e){
  73. if(e==0){
  74. right_status = false
  75. }
  76. console.error("processTask error:",e)
  77. } finally{
  78. if(connection!=null){
  79. connection.end()
  80. }
  81. global.setTimeout(processTask, 2000);
  82. }
  83. }
  84. CMD.insert_product = async function(data){
  85. await video_product_controllers.createProductData({
  86. book_platform:data.tg_platform_id,
  87. product_name:data.book_name,
  88. genre:data.genre,
  89. product_id:data.book_id,
  90. words:data.words,
  91. publish_time:data.publish_time
  92. })
  93. }
  94. CMD.init = async function(){
  95. redis_help.connect((results)=>{
  96. if(results){
  97. processTask();
  98. }
  99. })
  100. }
  101. CMD.init()