zy_mf_search_book.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. const fetch = require('node-fetch'); // Node.js 18以下版本需要安装 node-fetch
  2. const tools = require('../../../tools');
  3. const config = require('../../../etc/config.json');
  4. const redis_help = require('../../use_redis');
  5. const helper = require('../../helper');
  6. const { urlencoded } = require('express');
  7. const mysql = require('mysql2/promise');
  8. const mldbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  9. const CMD = {}
  10. CMD.search_id = async function(bookId) {
  11. try{
  12. let timestamp = helper.getCurrentUnixTimestamp()
  13. let token = await require('../zy_mf/get_zy_mf_token').get_token()
  14. let app_key = "shiyuestory_mini"
  15. let user_id = 7850
  16. let clinet = tools.getOneNewClinet()
  17. let param = `access_token=${token}&app_key=${app_key}&user_id=${user_id}&book_id=${bookId}&page=1&size=10`
  18. let response = await clinet.get(`https://openapi-gaia.zhangyue.com/opendata/reseller/free/book/list?${param}`)
  19. let list = null
  20. if(response.data.code!=0){
  21. console.error("search_id error:",response)
  22. throw response.data
  23. }else{
  24. list = response.data.body.list
  25. }
  26. if(list.length<=0){
  27. throw list
  28. }
  29. console.log('知乎书籍::响应状态:', response.status);
  30. let book_info = list[0]
  31. let info = {}
  32. info.words = book_info.word_count
  33. info.book_name = book_info.book_name
  34. info.book_id = book_info.book_id
  35. info.publish_time = book_info.create_time
  36. info.genre = 3;
  37. return info;
  38. }catch(error){
  39. console.error('掌阅书籍::请求错误:', error);
  40. return null;
  41. }
  42. }
  43. CMD.new_search_id = async function(bookId) {
  44. try{
  45. let authorization = await redis_help.getKeyValue("zy_mf_token")
  46. console.log("authorization:",authorization)
  47. let clinet = tools.getOneNewClinet({
  48. // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00",
  49. "authorization": authorization,
  50. })
  51. let list = null
  52. 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=${bookId}&book_content_type=&author=&copyright_type=&category_sex=&is_complete=&total=0&page=1&size=10&sort=&order=&psize=10&scheme_id=38`)
  53. if(response.data.code!=0){
  54. console.error("new_search_id error:",response)
  55. throw response.data
  56. }else{
  57. list = response.data.body.list
  58. }
  59. if(list.length<0){
  60. throw response.data
  61. }
  62. let book_info = list[0]
  63. let info = {}
  64. // console.log("book_info:",book_info)
  65. info.words = book_info.word_count
  66. info.book_name = book_info.book_name
  67. info.book_id = book_info.book_id
  68. info.publish_time = book_info.create_time
  69. info.totalChapterNum = book_info.chapter_count
  70. info.default_pay_section = book_info.default_pay_section //默认付费章节
  71. info.default_price = book_info.default_price // 加个
  72. info.fee_unit = book_info.fee_unit //10 = 整本, 20 = 千字
  73. info.author = book_info.author
  74. if(book_info.word_count<100000){
  75. info.genre = 3;
  76. }else{
  77. info.genre = 1;
  78. }
  79. return info;
  80. }catch(e){
  81. return null;
  82. }
  83. }
  84. CMD.search_name_by_lib = async function(bookName) {
  85. let connection = null
  86. try{
  87. //先搜索库里是否存在
  88. connection = await mysql.createConnection({
  89. ...mldbConfig,
  90. multipleStatements: true
  91. });
  92. let sql = `SELECT * FROM zy_mf_lib WHERE product_name ="${bookName}" LIMIT 1`
  93. let [info] = await connection.execute(sql)
  94. if(info.length<=0){
  95. throw 0
  96. }
  97. return info[0]
  98. }catch(e){
  99. console.log(e)
  100. return null
  101. }finally{
  102. if(connection!=null){
  103. connection.end()
  104. }
  105. }
  106. }
  107. CMD.new_search_name = async function(bookName) {
  108. try{
  109. let lib_book_info = await CMD.search_name_by_lib(bookName)
  110. if(bookName!=null){
  111. return {book_name:lib_book_info.product_name,book_id:lib_book_info.product_id}
  112. }
  113. let authorization = await redis_help.getKeyValue("zy_mf_token")
  114. console.log("authorization:",authorization)
  115. let clinet = tools.getOneNewClinet({
  116. // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00",
  117. "authorization": authorization,
  118. })
  119. let list = null
  120. let response = await clinet.get(`https://gaia.zhangyue.com/gaia/v1/open_api/search/pay/book/channel/price/getBookNameList?application_uniq_id=shiyuestory_mini&name=${encodeURIComponent(bookName)}&page=1&size=10&scheme_id=38&business_model=free`)
  121. if(response.data.code!=0){
  122. console.error("new_search_id error:",response)
  123. throw response.data
  124. }else{
  125. list = response.data.body.items
  126. }
  127. if(list.length<0){
  128. throw response.data
  129. }
  130. let book_info = list[0]
  131. let info = {}
  132. info.book_name = book_info.name
  133. info.book_id = book_info.id
  134. return info;
  135. }catch(e){
  136. return null
  137. }
  138. }
  139. module.exports = CMD;