123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- //同步番茄免费的匹配书籍
- const config = require("../etc/config.json");
- const mysql = require('mysql2/promise');
- const mldbConfig = config.isDebug?config.debug_mysql:config.release_mysql
- 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 schedule = require('node-schedule');
- const tools = require("../tools");
- const CMD = {}
- let cur_page = 1
- async function getZyDataByPage(page) {
- try{
- let authorization = await redis_help.getKeyValue("zy_mf_token")
- let clinet = tools.getOneNewClinet({
- // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00",
- "authorization": authorization,
- })
- let response = await clinet.get(`https://gaia.zhangyue.com/gaia/v1/open_api/pay/book/getBookInfoList?reseller_id=9886&application_key=shiyuestory_mini&book_ids=&book_content_type=&author=©right_type=&category_sex=&is_complete=&total=0&page=${page}&size=50&sort=&order=&psize=50&scheme_id=38&business_model=free`)
- let list = []
- if(response.data.code!=0){
- console.error("new_search_id error:",response)
- throw response.data
- }else{
- list = response.data.body.list
- }
- if(list.length<=0){
- return null
- }
- return list
- }catch(e){
- return null
- }
- }
- async function addBookListToLib(data) {
- try {
- const connection = await mysql.createConnection({
- ...mldbConfig,
- multipleStatements: true
- });
-
- // await processVideoTitles(connection, data.map(item => item.title));
- console.log("data:",data)
- // 转换时间戳为MySQL datetime格式
- const values = data.map(item => [
- item.book_id,
- item.book_name,
- item.create_time,
- item.chapter_count,
- item.default_pay_section,
- item.default_price,
- item.start_chapter,
- item.fee_unit,
- item.author,
- item.word_count
- ]);
- let table_name = "zy_mf_lib"
- const insertSQL = `
- INSERT INTO ${table_name}
- (product_id, product_name, publish_time, totalChapterNum, default_pay_section,
- default_price, start_chapter, fee_unit, author,
- word_count)
- VALUES ?
- ON DUPLICATE KEY UPDATE
- product_id = VALUES(product_id)
- `;
-
- await connection.query(insertSQL, [values]);
- await connection.end();
-
- console.log(`成功插入 ${values.length} 条记录到表 ${table_name}`);
- } catch (error) {
- console.error('处理消息失败:', error);
- throw error;
- }
- }
- async function processTask(){
- let connection = null
- let isFinish = false
- try{
-
- let list = await getZyDataByPage(cur_page)
- if( list == null ){
- isFinish = true
- throw 0
- }
- await addBookListToLib(list)
- }catch(e){
- } finally{
- if(connection!=null){
- connection.end()
- }
- global.setTimeout(processTask, 60000);
- }
- }
- CMD.init = async function(){
- redis_help.connect(async (results)=>{
- if(results){
- await processTask();
- }
- })
- }
- CMD.init()
|