pull_zy_mf_task.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //同步番茄免费的匹配书籍
  2. const config = require("../etc/config.json");
  3. const mysql = require('mysql2/promise');
  4. const mldbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  5. const taskdbConfig = config.isDebug?config.debug_task_mysql:config.release_task_mysql
  6. const redis_help = require('../src/use_redis');
  7. const video_product_controllers = require('../src/data_manager/Controllers/video_product_controllers');
  8. const helper = require("../src/helper");
  9. const schedule = require('node-schedule');
  10. const tools = require("../tools");
  11. const CMD = {}
  12. let cur_page = 1
  13. async function getZyDataByPage(page) {
  14. try{
  15. let authorization = await redis_help.getKeyValue("zy_mf_token")
  16. let clinet = tools.getOneNewClinet({
  17. // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00",
  18. "authorization": authorization,
  19. })
  20. 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=&copyright_type=&category_sex=&is_complete=&total=0&page=${page}&size=50&sort=&order=&psize=50&scheme_id=38&business_model=free`)
  21. let list = []
  22. if(response.data.code!=0){
  23. console.error("new_search_id error:",response)
  24. throw response.data
  25. }else{
  26. list = response.data.body.list
  27. }
  28. if(list.length<=0){
  29. return null
  30. }
  31. return list
  32. }catch(e){
  33. return null
  34. }
  35. }
  36. async function addBookListToLib(data) {
  37. try {
  38. const connection = await mysql.createConnection({
  39. ...mldbConfig,
  40. multipleStatements: true
  41. });
  42. // await processVideoTitles(connection, data.map(item => item.title));
  43. console.log("data:",data)
  44. // 转换时间戳为MySQL datetime格式
  45. const values = data.map(item => [
  46. item.book_id,
  47. item.book_name,
  48. item.create_time,
  49. item.chapter_count,
  50. item.default_pay_section,
  51. item.default_price,
  52. item.start_chapter,
  53. item.fee_unit,
  54. item.author,
  55. item.word_count
  56. ]);
  57. let table_name = "zy_mf_lib"
  58. const insertSQL = `
  59. INSERT INTO ${table_name}
  60. (product_id, product_name, publish_time, totalChapterNum, default_pay_section,
  61. default_price, start_chapter, fee_unit, author,
  62. word_count)
  63. VALUES ?
  64. ON DUPLICATE KEY UPDATE
  65. product_id = VALUES(product_id)
  66. `;
  67. await connection.query(insertSQL, [values]);
  68. await connection.end();
  69. console.log(`成功插入 ${values.length} 条记录到表 ${table_name}`);
  70. } catch (error) {
  71. console.error('处理消息失败:', error);
  72. throw error;
  73. }
  74. }
  75. async function processTask(){
  76. let connection = null
  77. let isFinish = false
  78. try{
  79. let list = await getZyDataByPage(cur_page)
  80. if( list == null ){
  81. isFinish = true
  82. throw 0
  83. }
  84. await addBookListToLib(list)
  85. }catch(e){
  86. } finally{
  87. if(connection!=null){
  88. connection.end()
  89. }
  90. global.setTimeout(processTask, 60000);
  91. }
  92. }
  93. CMD.init = async function(){
  94. redis_help.connect(async (results)=>{
  95. if(results){
  96. await processTask();
  97. }
  98. })
  99. }
  100. CMD.init()