//同步番茄免费的发布时间 const config = require("../etc/config.json"); const mysql = require('mysql2/promise'); const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql const redis_help = require('../src/use_redis'); const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers'); const helper = require("../src/helper"); const CMD = {} async function processTask(){ let right_status = true let connection = null try{ connection = await mysql.createConnection({ ...taskdbConfig, multipleStatements: true }); //先获取100本没有发布时间的番茄免费书籍 let sql = `SELECT * FROM video_product WHERE book_platform = ${config.platform_fanqiemf} AND product_parent_id !="" AND publish_time IS NULL LIMIT 5` console.log("sql:",sql) const [rows] = await connection.execute( sql ); if(rows.length<=0){ throw 0 } for (let index = 0; index < rows.length; index++) { let video_product_info = rows[index] if(video_product_info.product_parent_id==video_product_info.product_id){ await connection.execute( ` UPDATE video_product SET publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} ` ); }else{ let book_info = await video_product_controllers.getProductData({book_id:video_product_info.product_parent_id}) if(book_info.success){ if(book_info.data.publish_time!=null){ let publish_time = helper.getDate7DaysBefore(book_info.data.publish_time,null,'YYYY-MM-DD') await connection.execute( `UPDATE video_product SET publish_time = ? WHERE id = ${video_product_info.id} `,[publish_time] ); } }else{ //添加到番茄付费里 let data = await require('../src/api/fq/fq_search_book').search_new_id(video_product_info.product_parent_id) if(data!=null&&data!=""){ let publish_time = data.on_shelf_time if(publish_time==""){ publish_time = data.latest_update_time } let words = data.word_count let book_name = data.book_name; let genre = 1 if(words>100000){ genre = 1; }else{ genre = 3; } await CMD.insert_product({ tg_platform_id:config.platform_fanqie, book_name:book_name, genre:genre, book_id:video_product_info.product_parent_id, words:words, publish_time:publish_time }) }else{ console.error("没找父书:",video_product_info.product_parent_id) await connection.execute( ` UPDATE video_product SET status = 0 , publish_time = '2000-01-01 01:01:01' WHERE id = ${video_product_info.id} ` ); console.error("查无此书:",video_product_info) } } } } }catch(e){ if(e==0){ right_status = false } console.error("processTask error:",e) } finally{ if(connection!=null){ connection.end() } global.setTimeout(processTask, 2000); } } CMD.insert_product = async function(data){ await video_product_controllers.createProductData({ book_platform:data.tg_platform_id, product_name:data.book_name, genre:data.genre, product_id:data.book_id, words:data.words, publish_time:data.publish_time }) } CMD.init = async function(){ redis_help.connect((results)=>{ if(results){ processTask(); } }) } CMD.init()