colexiu-all-xml.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const fetch = (...args) => import('node-fetch').then(({default: fetch}) => fetch(...args));
  2. const fs = require('fs')
  3. const path = require('path')
  4. const util = require('util')
  5. const streamPipeline = util.promisify(require('stream').pipeline)
  6. const stat = util.promisify(fs.stat)
  7. // const relationship = fs.createWriteStream('./scripts/colexiu-xmls/index.json')
  8. const xmlResult = fs.createWriteStream('./scripts/xmlresult.txt')
  9. const relationshipJSON = fs.readFileSync('./scripts/colexiu-xmls/index.json')
  10. const Authorization = 'bearer 54fa202e-3b32-40e5-b463-3d277f3f82d0'
  11. const LIST_URL = 'https://dev.colexiu.com/api-admin/music/sheet/list'
  12. const FetchList = async (page = 1, pageSize = 100) => {
  13. const response = await fetch(LIST_URL, {
  14. method: 'POST',
  15. headers: {
  16. Authorization,
  17. 'Content-Type': 'application/json'
  18. },
  19. body: JSON.stringify({
  20. page,
  21. limit: pageSize,
  22. })
  23. })
  24. const json = await response.json()
  25. console.log(json)
  26. return json
  27. }
  28. let page = 1
  29. let errortimes = 0
  30. /**
  31. * 从接口读取所有数据信息
  32. */
  33. const start = async () => {
  34. console.log(page)
  35. try {
  36. const json = await FetchList(page)
  37. for (const item of json.data.rows) {
  38. relationship.write(JSON.stringify(item) + '\n')
  39. }
  40. if (json.data.totalPage > page) {
  41. page++
  42. await start()
  43. }
  44. errortimes = 0
  45. } catch (error) {
  46. if (errortimes < 3) {
  47. await start()
  48. }
  49. console.log(error)
  50. }
  51. }
  52. // start()
  53. const getFilename = url => {
  54. return decodeURI(new URL(url).pathname).replace(/\//ig, ' ').trim().split(' ').join('-')
  55. }
  56. const FetchXml = async url => {
  57. const filename = getFilename(url)
  58. const response = await fetch(url, {
  59. method: 'GET',
  60. })
  61. console.log(url, response)
  62. if (response.status !== 200) throw new Error(`unexpected response ${response.statusText}`)
  63. await streamPipeline(response.body, fs.createWriteStream(path.join(__dirname, 'colexiu-xmls', filename)))
  64. }
  65. const ids = ['2366', '2365', '2364', '2363', '2353', '2279', '2223', '2178', '2162', '1822', '1127', '959', '942', '809', '798', '790', '726', '722', '21', '9', '6', '5', '4']
  66. /**
  67. * 验证数据
  68. */
  69. const errorlist = {}
  70. const verification = async () => {
  71. const list = await relationshipJSON.toString()
  72. const items = list.split('\n')
  73. for (const item of items) {
  74. try {
  75. const json = JSON.parse(item)
  76. // if (!ids.includes(json.id + '')) {
  77. // continue
  78. // }
  79. const txt = ['曲目id:' + json.id,'曲目名称:' + json.musicSheetName, '音频类型:' + json.audioType].join(' ')
  80. // xmlResult.write(txt + '\n')
  81. if (!json.xmlFileUrl) {
  82. errorlist[json.id] = '错误原因:没有xml文件'
  83. console.log(errorlist[json.id])
  84. }
  85. const subfixname = json.xmlFileUrl.split('.').pop()
  86. if (subfixname !== 'xml') {
  87. errorlist[json.id] = '错误原因:xml文件格式不对,文件是:' + subfixname
  88. }
  89. // console.log(json.xmlFileUrl)
  90. // try {
  91. // const filestat = fs.statSync(path.join(__dirname, 'colexiu-xmls', getFilename(json.xmlFileUrl)))
  92. // console.log(filestat)
  93. // } catch (error) {
  94. // console.log(error)
  95. // }
  96. if (!errorlist[json.id]) {
  97. try {
  98. // await FetchXml(json.xmlFileUrl)
  99. fs.statSync(path.join(__dirname, 'colexiu-xmls', getFilename(json.xmlFileUrl)))
  100. } catch (error) {
  101. console.log(error)
  102. errorlist[json.id] = '错误原因:文件下载失败'
  103. }
  104. }
  105. if (errorlist[json.id]) {
  106. xmlResult.write([json.id, json.musicSheetName, errorlist[json.id]].join(' ') + '\n')
  107. }
  108. } catch (error) {
  109. console.log('解析错误:' + item)
  110. }
  111. }
  112. // xmlResult.write('\n\n\n')
  113. // xmlResult.write(JSON.stringify(errorlist) + '\n')
  114. }
  115. // verification()
  116. const isXmlFile = async filepath => {
  117. try {
  118. const readerfile = util.promisify(fs.readFile)
  119. const xml = await readerfile(filepath)
  120. return xml.indexOf('<?xml ') === 0
  121. } catch (error) {
  122. return false
  123. }
  124. }
  125. /**
  126. * 验证是否是xml文件
  127. */
  128. const xmlVerification = async () => {
  129. const xmls = fs.readdirSync(path.join(__dirname, 'colexiu-xmls'))
  130. for (const xml of xmls) {
  131. const filepath = path.join(__dirname, 'colexiu-xmls', xml)
  132. const fileStat = await stat(filepath)
  133. if (xml.split('.').pop() !== 'xml' || !fileStat.isFile()) {
  134. continue
  135. }
  136. const isxml = await isXmlFile(filepath)
  137. if(!isxml) {
  138. console.log(xml)
  139. }
  140. }
  141. }
  142. xmlVerification()
  143. const overlay = async () => {
  144. const xmlinfo = {}
  145. const xmlstring = fs.readFileSync(path.join(__dirname, 'colexiu-xmls', 'index.json'))
  146. const list = xmlstring.toString().split('\n')
  147. for (const item of list) {
  148. try {
  149. const json = JSON.parse(item)
  150. if (!xmlinfo[json.xmlFileUrl]) {
  151. xmlinfo[json.xmlFileUrl] = 0
  152. }
  153. xmlinfo[json.xmlFileUrl] = xmlinfo[json.xmlFileUrl] + 1
  154. } catch (error) {}
  155. }
  156. console.log(xmlinfo)
  157. }
  158. // overlay()