zy_mf_search_book.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. let gender = 0
  80. if(book_info.category_sex=='male'){
  81. gender = 1
  82. }else if(book_info.channel=='female'){
  83. gender = 2
  84. }
  85. info.gender = gender
  86. return info;
  87. }catch(e){
  88. return null;
  89. }
  90. }
  91. CMD.search_name_by_lib = async function(bookName) {
  92. let connection = null
  93. try{
  94. //先搜索库里是否存在
  95. connection = await mysql.createConnection({
  96. ...mldbConfig,
  97. multipleStatements: true
  98. });
  99. let sql = `SELECT * FROM zy_mf_lib WHERE product_name ="${bookName}" LIMIT 1`
  100. let [info] = await connection.execute(sql)
  101. if(info.length<=0){
  102. throw 0
  103. }
  104. return info[0]
  105. }catch(e){
  106. console.log(e)
  107. return null
  108. }finally{
  109. if(connection!=null){
  110. connection.end()
  111. }
  112. }
  113. }
  114. CMD.new_search_name = async function(bookName,isAuto=true) {
  115. try{
  116. let lib_book_info = await CMD.search_name_by_lib(bookName)
  117. if(lib_book_info!=null){
  118. return {book_name:lib_book_info.product_name,book_id:lib_book_info.product_id}
  119. }
  120. if(isAuto){
  121. return null
  122. }
  123. let authorization = await redis_help.getKeyValue("zy_mf_token")
  124. console.log("authorization:",authorization)
  125. let clinet = tools.getOneNewClinet({
  126. // "cookie":"acw_tc=1a0c639417498037411406820e006fb57d572ffdc9c83cd8a46aaf8d19ca00",
  127. "authorization": authorization,
  128. })
  129. let list = null
  130. 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`)
  131. if(response.data.code!=0){
  132. console.error("new_search_id error:",response)
  133. throw response.data
  134. }else{
  135. list = response.data.body.items
  136. }
  137. if(list.length<0){
  138. throw response.data
  139. }
  140. let book_info = list[0]
  141. let info = {}
  142. info.book_name = book_info.name
  143. info.book_id = book_info.id
  144. return info;
  145. }catch(e){
  146. return null
  147. }
  148. }
  149. module.exports = CMD;