pull_zy_task.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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_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=9390&application_key=donglingstory_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=39`)
  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. // 转换时间戳为MySQL datetime格式
  44. const values = data.map(item => [
  45. item.book_id,
  46. item.book_name,
  47. item.create_time,
  48. item.chapter_count,
  49. item.default_pay_section,
  50. item.default_price,
  51. item.start_chapter,
  52. item.fee_unit,
  53. item.author,
  54. item.word_count
  55. ]);
  56. let table_name = "zy_lib"
  57. const insertSQL = `
  58. INSERT INTO ${table_name}
  59. (product_id, product_name, publish_time, totalChapterNum, default_pay_section,
  60. default_price, start_chapter, fee_unit, author,
  61. word_count)
  62. VALUES ?
  63. ON DUPLICATE KEY UPDATE
  64. product_id = VALUES(product_id)
  65. `;
  66. await connection.query(insertSQL, [values]);
  67. await connection.end();
  68. console.log(`成功插入 ${values.length} 条记录到表 ${table_name}`);
  69. } catch (error) {
  70. console.error('处理消息失败:', error);
  71. throw error;
  72. }
  73. }
  74. async function processTask(){
  75. let connection = null
  76. let isFinish = false
  77. try{
  78. let list = await getZyDataByPage(cur_page)
  79. if( list == null ){
  80. isFinish = true
  81. throw 0
  82. }
  83. await addBookListToLib(list)
  84. }catch(e){
  85. } finally{
  86. if(connection!=null){
  87. connection.end()
  88. }
  89. global.setTimeout(processTask, 60000);
  90. }
  91. }
  92. CMD.init = async function(){
  93. redis_help.connect(async (results)=>{
  94. if(results){
  95. await processTask();
  96. }
  97. })
  98. }
  99. CMD.init()