zy_search_book.js 5.4 KB

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