qm_mf_book_async.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. const config = require("../etc/config.json");
  2. const helper = require("../src/helper");
  3. const HttpClient = require("../src/HttpClient");
  4. const redis_help = require('../src/use_redis');
  5. const mysql = require('mysql2/promise');
  6. const tools = require("../tools");
  7. const get_fq_book_tab_controllers = require("../src/data_manager/Controllers/get_fq_book_tab_controllers");
  8. const dbConfig = config.isDebug?config.debug_mysql:config.release_mysql
  9. const CMD = {}
  10. function getCodeFromUrl(url) {
  11. const codeMatch = url.match(/code=([^&]+)/);
  12. if (codeMatch && codeMatch[1]) {
  13. return codeMatch[1];
  14. }
  15. return null;
  16. }
  17. async function processTask(){
  18. let connection = null
  19. try{
  20. const today = helper.getLocalDate();
  21. let date = today.replace(/-/g, '');
  22. let table_name = `video_product`;
  23. // 创建数据库连接
  24. connection = await mysql.createConnection({
  25. ...dbConfig,
  26. multipleStatements: true
  27. });
  28. // 检查表是否存在
  29. const [tableCheck] = await connection.execute(
  30. `SELECT TABLE_NAME
  31. FROM information_schema.tables
  32. WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`,
  33. [dbConfig.database, table_name]
  34. );
  35. if (tableCheck.length === 0) {
  36. // 表不存在
  37. console.error(`表 ${table_name} 不存在`);
  38. throw new Error(`表 ${table_name} 不存在`);
  39. } else {
  40. const [rows] = await connection.execute(
  41. `SELECT * FROM ${table_name} WHERE book_platform = 3 LIMIT 6498`
  42. );
  43. if(rows.length<=0){
  44. throw "没有数据处理!"
  45. }
  46. // let id_list = []
  47. // let video_id_list = []
  48. // let video_data_list = []
  49. // rows.forEach((row, index) => {
  50. // id_list.push(row.id)
  51. // video_id_list.push(row.video_id)
  52. // video_data_list[row.video_id] = row
  53. // });
  54. // const [filter_rows] = await connection.execute(
  55. // `SELECT * FROM filter_data WHERE video_id IN (${video_id_list.join(",")})`
  56. // );
  57. // function getFilterById(video_id){
  58. // for (let index = 0; index < filter_rows.length; index++) {
  59. // const element = filter_rows[index];
  60. // if(element.video_id==video_id){
  61. // return element
  62. // }
  63. // }
  64. // return null
  65. // }
  66. // let need_insert_list = []
  67. // for (let index = 0; index < video_id_list.length; index++) {
  68. // const video_id = video_id_list[index];
  69. // let d = getFilterById(video_id)
  70. // if(d==null){
  71. // need_insert_list.push(video_data_list[video_id])
  72. // }
  73. // }
  74. // let client = new HttpClient()
  75. // let values = []
  76. // let sid_tt_status = true
  77. // for (let index = 0; index < need_insert_list.length; index++) {
  78. // const row = need_insert_list[index];
  79. // if(helper.checkChannel(row.guajian_link,"pages/theatre/index")){
  80. // let key = getCodeFromUrl(row.guajian_link)
  81. // let book_name = ""
  82. // let book_id = ""
  83. // if(key!=null){
  84. // let fq_book_result = await get_fq_book_tab_controllers.getData({ book_id: key });
  85. // if(fq_book_result.success){
  86. // book_name = JSON.parse(fq_book_result.data.info.info).book_name
  87. // book_id = fq_book_result.data.book_id
  88. // }else{
  89. // let sid_tt = await tools.getFqSidtt()
  90. // let {url,params,headers} = helper.getFqRequestOpt(key,sid_tt)
  91. // let response = await client.fqProxyGet(url, params,headers)
  92. // if(response.code==0){
  93. // let obj = response['data']
  94. // book_name = obj['video_info'].book_name
  95. // book_id = obj['video_info'].book_id
  96. // let fq_book = {}
  97. // fq_book.key = key
  98. // let fq_book_data = {book_name:book_name,book_id:book_id,info:JSON.stringify(obj['video_info'])}
  99. // await CMD.add_fq_book(fq_book,fq_book_data)
  100. // }else{
  101. // if(response==""){
  102. // sid_tt_status = false
  103. // }
  104. // console.log("error response:",response)
  105. // }
  106. // }
  107. // }else{
  108. // console.log("key = null:",row.guajian_link)
  109. // }
  110. // if(book_name!=""){
  111. // values.push({video_id:row.video_id,
  112. // video_link:row.video_link,
  113. // title:row.title,
  114. // book_name:book_name,
  115. // book_id:book_id,
  116. // tg_platform_id:config.platform_fanqie_dx,
  117. // like_num:row.like_num,
  118. // comment_num:row.comment_num,
  119. // shared_num:row.shared_num,
  120. // kepp_num:row.kepp_num,
  121. // create_time:new Date(),
  122. // create_day:date,
  123. // status:0,
  124. // data_source:2
  125. // })
  126. // }
  127. // }
  128. // }
  129. // console.log("values:",values)
  130. // if(values.length>0&&sid_tt_status){
  131. // const formattedValues = values.map(item => [
  132. // item.video_id,
  133. // item.video_link,
  134. // item.title,
  135. // item.book_name,
  136. // item.book_id,
  137. // item.tg_platform_id,
  138. // item.like_num,
  139. // item.comment_num,
  140. // item.shared_num,
  141. // item.kepp_num,
  142. // item.create_time,
  143. // item.create_day,
  144. // item.status,
  145. // item.data_source
  146. // ]);
  147. // // 插入数据
  148. // const insertSQL = `
  149. // INSERT IGNORE INTO filter_data
  150. // (video_id, video_link, title, book_name,
  151. // book_id, tg_platform_id, like_num, comment_num, shared_num,
  152. // kepp_num, create_time, create_day, status,data_source)
  153. // VALUES ?
  154. // `;
  155. // await connection.query(insertSQL, [formattedValues]);
  156. // }
  157. // const updateSQL = `
  158. // UPDATE xs_origin_data
  159. // SET sync_info_status = 1
  160. // WHERE FIND_IN_SET(id, ?)
  161. // `;
  162. // await connection.query(updateSQL, [id_list.join(',')]);
  163. }
  164. }catch(e){
  165. console.error("processTask error:",e)
  166. } finally{
  167. global.setTimeout(processTask, 1000);
  168. // if(connection!=null){
  169. // await connection.end();
  170. // }
  171. }
  172. }
  173. CMD.add_fq_book = async function(data,response){
  174. let book_name = response.book_name
  175. let book_id = response.book_id
  176. data.book_name = book_name
  177. data.book_id = book_id
  178. let tab = {
  179. book_key:data.key,
  180. book_id:data.book_id,
  181. info:JSON.stringify(response)
  182. }
  183. return await get_fq_book_tab_controllers.createData( tab)
  184. }
  185. CMD.init = async function(){
  186. redis_help.connect((results)=>{
  187. if(results){
  188. processTask();
  189. }
  190. })
  191. }
  192. CMD.init()