sync_publish_time_fq.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 CMD = {}
  7. async function processTask(){
  8. let right_status = true
  9. let connection = null
  10. try{
  11. connection = await mysql.createConnection({
  12. ...taskdbConfig,
  13. multipleStatements: true
  14. });
  15. //先获取100本没有发布时间的七猫书籍
  16. let sql = `SELECT * FROM video_product WHERE book_platform = ${config.platform_fanqie} AND publish_time IS NULL LIMIT 500`
  17. console.log("sql:",sql)
  18. const [rows] = await connection.execute(
  19. sql
  20. );
  21. if(rows.length<=0){
  22. throw 0
  23. }
  24. for (let index = 0; index < rows.length; index++) {
  25. let video_product_info = rows[index]
  26. let data = await require('../src/api/fq/fq_search_book').search_new_id(video_product_info.product_id)
  27. if(data!=null){
  28. let publish_time = data.publish_time
  29. await connection.execute(
  30. `UPDATE video_product SET publish_time = "${publish_time}" WHERE id = ${video_product_info.id} `
  31. );
  32. await new Promise(resolve => setTimeout(resolve,1000));
  33. }else{
  34. await connection.execute(
  35. ` UPDATE video_product SET publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  36. );
  37. console.error("查无此书:",video_product_info)
  38. }
  39. }
  40. }catch(e){
  41. if(e==0){
  42. right_status = false
  43. }
  44. console.error("processTask error:",e)
  45. } finally{
  46. if(connection!=null){
  47. connection.end()
  48. }
  49. global.setTimeout(processTask, 2000);
  50. }
  51. }
  52. CMD.init = async function(){
  53. redis_help.connect((results)=>{
  54. if(results){
  55. processTask();
  56. }
  57. })
  58. }
  59. CMD.init()