preLoadFile.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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):console.error("Tool [在文本中找不到 参数三 "+after+"]");
  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 () {
  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', '次观看'))
  99. let spsl = parseInt(cutString(pdInfo,'订阅者\n\n\t', '个视频'))
  100. let dyrs = parseInt(cutString(pdInfo,'注册\n\n\t', '位订阅者'))
  101. if (!spsl) {
  102. spsl = parseInt(cutString(pdInfo,'注册\n', '个视频'))
  103. }
  104. bfsl = bfsl ? bfsl : 0
  105. spsl = spsl ? spsl : 0
  106. dyrs = dyrs ? dyrs : 0
  107. console.log(bfsl, spsl, dyrs)
  108. adtaArr[index][5] = bfsl
  109. adtaArr[index][6] = spsl
  110. adtaArr[index][7] = dyrs
  111. adtaArr[index][8] = bfsl - parseInt(adtaArr[index][2])
  112. adtaArr[index][9] = spsl - parseInt(adtaArr[index][3])
  113. adtaArr[index][10] = dyrs - parseInt(adtaArr[index][4])
  114. saveFile(adtaArr, true)
  115. }
  116. }
  117. function checkData (adtaArr) {
  118. for (let index = 0; index < adtaArr.length; index++) {
  119. const element = adtaArr[index];
  120. console.log(element)
  121. if (element[1].includes('https://') && element[5] === '') {
  122. console.log(element[1])
  123. if (location.href.slice(0, 40) == element[1].slice(0, 40)) {
  124. setTimeout(() => {
  125. document.querySelector('.yt-truncated-text__absolute-button').click()
  126. setTimeout(() => {
  127. checkText()
  128. }, 3000);
  129. setTimeout(() => {
  130. checkText()
  131. }, 8000);
  132. setTimeout(() => {
  133. location.reload()
  134. }, 14000);
  135. }, 2000);
  136. } else {
  137. location.href = element[1]
  138. }
  139. return
  140. }
  141. }
  142. // 汇总计算
  143. adtaArr[adtaArr.length - 1][2] = 0
  144. adtaArr[adtaArr.length - 1][3] = 0
  145. adtaArr[adtaArr.length - 1][4] = 0
  146. adtaArr[adtaArr.length - 1][5] = 0
  147. adtaArr[adtaArr.length - 1][6] = 0
  148. adtaArr[adtaArr.length - 1][7] = 0
  149. adtaArr[adtaArr.length - 1][8] = 0
  150. adtaArr[adtaArr.length - 1][9] = 0
  151. adtaArr[adtaArr.length - 1][10] = 0
  152. for (let index = 1; index < adtaArr.length - 1; index++) {
  153. const element = adtaArr[index];
  154. adtaArr[adtaArr.length - 1][2] += parseInt(element[2])
  155. adtaArr[adtaArr.length - 1][3] += parseInt(element[3])
  156. adtaArr[adtaArr.length - 1][4] += parseInt(element[4])
  157. adtaArr[adtaArr.length - 1][5] += parseInt(element[5])
  158. adtaArr[adtaArr.length - 1][6] += parseInt(element[6])
  159. adtaArr[adtaArr.length - 1][7] += parseInt(element[7])
  160. adtaArr[adtaArr.length - 1][8] += parseInt(element[8])
  161. adtaArr[adtaArr.length - 1][9] += parseInt(element[9])
  162. adtaArr[adtaArr.length - 1][10] += parseInt(element[10])
  163. }
  164. saveFile(adtaArr, false)
  165. alert('所有项目已经检查完!')
  166. }
  167. // 调用保存方法
  168. window.electronAPI.readFile({filename: getDaysAgo(0) + '.csv', encoding: "gb2312"}).then(result => {
  169. if (result.success) {
  170. console.log(result.content);
  171. // 如果内容是空的,则读取昨天的
  172. if (result.content == '') {
  173. window.electronAPI.readFile({filename: getDaysAgo(1) + '.csv', encoding: "gb2312"}).then(result2 => {
  174. if (result2.success) {
  175. console.log(result2.content);
  176. // 如果内容是空的,则读取昨天的
  177. if (result2.content == '') {
  178. alert('缺少昨天的数据!')
  179. } else {
  180. let fileData = csvToArray(result2.content)
  181. creatToday(fileData)
  182. console.log(fileData)
  183. checkData(fileData)
  184. }
  185. } else {
  186. console.error('文件读取失败:', result2.error);
  187. }
  188. });
  189. } else {
  190. // 读取加载文件
  191. let fileData = csvToArray(result.content)
  192. console.log(fileData)
  193. checkData(fileData)
  194. }
  195. } else {
  196. console.error('文件读取失败:', result.error);
  197. }
  198. });