preLoadFile.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. function getDaysAgo(n) {
  2. const timestamp = Date.now() - (n * 24 * 60 * 60 * 1000);
  3. const date = new Date(timestamp);
  4. const year = date.getFullYear();
  5. const month = String(date.getMonth() + 1).padStart(2, '0');
  6. const day = String(date.getDate()).padStart(2, '0');
  7. return `${year}-${month}-${day}`;
  8. }
  9. function csvToArray(csvString) {
  10. const lines = csvString.split('\n');
  11. const result = [];
  12. for (let line of lines) {
  13. // 跳过空行
  14. if (line.trim() === '') continue;
  15. // 按逗号分割,并去除每个字段的空白
  16. const row = line.split(',').map(field => field.trim());
  17. result.push(row);
  18. }
  19. return result;
  20. }
  21. function arrayToCSV(array) {
  22. return array.map(row =>
  23. row.map(field => String(field)).join(',')
  24. ).join('\n');
  25. }
  26. function creatToday(fileList) {
  27. for (let index = 1; index < fileList.length - 1; index++) {
  28. fileList[index][2] = fileList[index][5]
  29. fileList[index][3] = fileList[index][6]
  30. fileList[index][4] = fileList[index][7]
  31. fileList[index][5] = ""
  32. fileList[index][6] = ""
  33. fileList[index][7] = ""
  34. fileList[index][8] = ""
  35. fileList[index][9] = ""
  36. fileList[index][10] = ""
  37. }
  38. const fileData = {
  39. filename: getDaysAgo(0) + '.csv',
  40. content: arrayToCSV(fileList),
  41. encoding: "gb2312"
  42. };
  43. // 调用保存方法
  44. window.electronAPI.saveFile(fileData).then(result => {
  45. if (result.success) {
  46. console.log('文件保存成功:', result.path);
  47. } else {
  48. console.error('文件保存失败:', result.error);
  49. }
  50. });
  51. }
  52. function saveFile(adtaArr, next) {
  53. // 保存数据
  54. const fileData = {
  55. filename: getDaysAgo(0) + '.csv',
  56. content: arrayToCSV(adtaArr),
  57. encoding: "gb2312"
  58. };
  59. // 调用保存方法
  60. window.electronAPI.saveFile(fileData).then(result => {
  61. if (result.success) {
  62. console.log('文件保存成功:', result.path);
  63. setTimeout(() => {
  64. if (next) checkData(adtaArr, next)
  65. }, 1000);
  66. } else {
  67. console.error('文件保存失败:', result.error);
  68. }
  69. });
  70. }
  71. function cutString (original, before, after, index) {
  72. index = index || 0;
  73. if (typeof index === "number") {
  74. const P = original.indexOf(before, index);
  75. if (P > -1) {
  76. if (after) {
  77. const f = original.indexOf(after, P + before.length);
  78. return (f>-1)? original.slice(P + before.toString().length, f):"";
  79. } else {
  80. return original.slice(P + before.toString().length);
  81. }
  82. } else {
  83. console.error("Tool [在文本中找不到 参数一 " + before + "]");
  84. return ''
  85. }
  86. } else {
  87. console.error("Tool [sizeTransition:" + index + "不是一个整数!]");
  88. }
  89. }
  90. function checkText (adtaArr, index) {
  91. if (window.noRun) return
  92. if (document.querySelector('.style-scope.ytd-about-channel-renderer') || document.querySelector('ytm-about-channel-renderer').innerText) {
  93. window.noRun = true
  94. let pdInfo = document.querySelector('.style-scope.ytd-about-channel-renderer').innerText
  95. if (!pdInfo) {
  96. pdInfo = document.querySelector('ytm-about-channel-renderer').innerText
  97. }
  98. let bfsl = parseInt(cutString(pdInfo,'视频\n\n\t', '次观看').replaceAll(',', ''))
  99. let spsl = parseInt(cutString(pdInfo,'订阅者\n\n\t', '个视频').replaceAll(',', ''))
  100. let dyrs = parseInt(cutString(pdInfo,'注册\n\n\t', '位订阅者').replaceAll(',', ''))
  101. if (!spsl) {
  102. spsl = parseInt(cutString(pdInfo,'注册\n', '个视频').replaceAll(',', ''))
  103. }
  104. bfsl = bfsl ? bfsl : 0
  105. spsl = spsl ? spsl : 0
  106. dyrs = dyrs ? dyrs : 0
  107. if (document.title.includes('404')) {
  108. bfsl = -1
  109. spsl = -1
  110. dyrs = -1
  111. }
  112. console.log(bfsl, spsl, dyrs)
  113. adtaArr[index][5] = bfsl
  114. adtaArr[index][6] = spsl
  115. adtaArr[index][7] = dyrs
  116. adtaArr[index][8] = bfsl - parseInt(adtaArr[index][2])
  117. adtaArr[index][9] = spsl - parseInt(adtaArr[index][3])
  118. adtaArr[index][10] = dyrs - parseInt(adtaArr[index][4])
  119. saveFile(adtaArr, true)
  120. }
  121. }
  122. function checkData (adtaArr) {
  123. for (let index = 0; index < adtaArr.length; index++) {
  124. const element = adtaArr[index];
  125. console.log(element)
  126. if (element[1].includes('https://') && element[5] === '') {
  127. console.log(element[1])
  128. if (location.href.slice(0, 40) == element[1].slice(0, 40)) {
  129. setTimeout(() => {
  130. document.querySelector('.yt-truncated-text__absolute-button').click()
  131. setTimeout(() => {
  132. checkText(adtaArr, index)
  133. }, 3000);
  134. setTimeout(() => {
  135. checkText(adtaArr, index)
  136. }, 8000);
  137. setTimeout(() => {
  138. location.reload()
  139. }, 14000);
  140. }, 2000);
  141. } else {
  142. location.href = element[1]
  143. }
  144. return
  145. }
  146. }
  147. // 汇总计算
  148. adtaArr[adtaArr.length - 1][2] = 0
  149. adtaArr[adtaArr.length - 1][3] = 0
  150. adtaArr[adtaArr.length - 1][4] = 0
  151. adtaArr[adtaArr.length - 1][5] = 0
  152. adtaArr[adtaArr.length - 1][6] = 0
  153. adtaArr[adtaArr.length - 1][7] = 0
  154. adtaArr[adtaArr.length - 1][8] = 0
  155. adtaArr[adtaArr.length - 1][9] = 0
  156. adtaArr[adtaArr.length - 1][10] = 0
  157. for (let index = 1; index < adtaArr.length - 1; index++) {
  158. const element = adtaArr[index];
  159. adtaArr[adtaArr.length - 1][2] += parseInt(element[2])
  160. adtaArr[adtaArr.length - 1][3] += parseInt(element[3])
  161. adtaArr[adtaArr.length - 1][4] += parseInt(element[4])
  162. adtaArr[adtaArr.length - 1][5] += parseInt(element[5])
  163. adtaArr[adtaArr.length - 1][6] += parseInt(element[6])
  164. adtaArr[adtaArr.length - 1][7] += parseInt(element[7])
  165. adtaArr[adtaArr.length - 1][8] += parseInt(element[8])
  166. adtaArr[adtaArr.length - 1][9] += parseInt(element[9])
  167. adtaArr[adtaArr.length - 1][10] += parseInt(element[10])
  168. }
  169. saveFile(adtaArr, false)
  170. alert('所有项目已经检查完!')
  171. }
  172. // 调用保存方法
  173. window.electronAPI.readFile({filename: getDaysAgo(0) + '.csv', encoding: "gb2312"}).then(result => {
  174. if (result.success) {
  175. console.log(result.content);
  176. // 如果内容是空的,则读取昨天的
  177. if (result.content == '') {
  178. window.electronAPI.readFile({filename: getDaysAgo(1) + '.csv', encoding: "gb2312"}).then(result2 => {
  179. if (result2.success) {
  180. console.log(result2.content);
  181. // 如果内容是空的,则读取昨天的
  182. if (result2.content == '') {
  183. alert('缺少昨天的数据!')
  184. } else {
  185. let fileData = csvToArray(result2.content)
  186. creatToday(fileData)
  187. console.log(fileData)
  188. checkData(fileData)
  189. }
  190. } else {
  191. console.error('文件读取失败:', result2.error);
  192. }
  193. });
  194. } else {
  195. // 读取加载文件
  196. let fileData = csvToArray(result.content)
  197. console.log(fileData)
  198. checkData(fileData)
  199. }
  200. } else {
  201. console.error('文件读取失败:', result.error);
  202. }
  203. });