sync_publish_time_fq.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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==""){
  28. console.error("接口失效")
  29. }else{
  30. if(data!=null){
  31. let publish_time = data.on_shelf_time
  32. if(data.on_shelf_time==""){
  33. publish_time = data.latest_update_time
  34. }
  35. await connection.execute(
  36. `UPDATE video_product SET publish_time = "${publish_time}" WHERE id = ${video_product_info.id} `
  37. );
  38. await new Promise(resolve => setTimeout(resolve,1000));
  39. }else{
  40. await connection.execute(
  41. ` UPDATE video_product SET status = 0 , publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} `
  42. );
  43. console.error("查无此书:",video_product_info)
  44. }
  45. }
  46. }
  47. }catch(e){
  48. if(e==0){
  49. right_status = false
  50. }
  51. console.error("processTask error:",e)
  52. } finally{
  53. if(connection!=null){
  54. connection.end()
  55. }
  56. global.setTimeout(processTask, 2000);
  57. }
  58. }
  59. CMD.init = async function(){
  60. redis_help.connect((results)=>{
  61. if(results){
  62. processTask();
  63. }
  64. })
  65. }
  66. CMD.init()