sync_publish_time_fq_mf.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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&&data!=""){
  45. let publish_time = data.on_shelf_time
  46. if(publish_time==""){
  47. publish_time = data.latest_update_time
  48. }
  49. let words = data.word_count
  50. let book_name = data.book_name;
  51. let genre = 1
  52. if(words>100000){
  53. genre = 1;
  54. }else{
  55. genre = 3;
  56. }
  57. await CMD.insert_product({
  58. tg_platform_id:config.platform_fanqie,
  59. book_name:book_name,
  60. genre:genre,
  61. book_id:video_product_info.product_parent_id,
  62. words:words,
  63. publish_time:publish_time
  64. })
  65. }else{
  66. console.error("没找父书:",video_product_info.product_parent_id)
  67. await connection.execute(
  68. ` UPDATE video_product SET status = 0 , publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  69. );
  70. console.error("查无此书:",video_product_info)
  71. }
  72. }
  73. }
  74. }
  75. }catch(e){
  76. if(e==0){
  77. right_status = false
  78. }
  79. console.error("processTask error:",e)
  80. } finally{
  81. if(connection!=null){
  82. connection.end()
  83. }
  84. global.setTimeout(processTask, 2000);
  85. }
  86. }
  87. CMD.insert_product = async function(data){
  88. await video_product_controllers.createProductData({
  89. book_platform:data.tg_platform_id,
  90. product_name:data.book_name,
  91. genre:data.genre,
  92. product_id:data.book_id,
  93. words:data.words,
  94. publish_time:data.publish_time
  95. })
  96. }
  97. CMD.init = async function(){
  98. redis_help.connect((results)=>{
  99. if(results){
  100. processTask();
  101. }
  102. })
  103. }
  104. CMD.init()