磁力搜自动采集

添加一个采集按钮到页面右上角,执行自动采集解析规则的操作

目前为 2019-11-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 磁力搜自动采集
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.5
  5. // @description 添加一个采集按钮到页面右上角,执行自动采集解析规则的操作
  6. // @homeurl https://github.com/dengyuhan/LardMonkeyScripts
  7. // @homeurl https://gf.qytechs.cn/zh-CN/scripts/392361
  8. // @author denghaha
  9. // @match *://*/*
  10. // @grant none
  11. // ==/UserScript==
  12. (function () {
  13. 'use strict'
  14.  
  15. let button = document.createElement('button')
  16. button.innerText = "采集"
  17. button.style.position = 'fixed'
  18. button.style.padding = '2px 6px'
  19. button.style.top = '0'
  20. button.style.right = '0'
  21. button.style.zIndex = '99999'
  22. button.style.borderRadius = '4px'
  23. button.style.color = "#606266"
  24. button.style.border = "1px solid #dcdfe6"
  25. button.style.backgroundColor = "#fff"
  26. button.style.opacity = "0.8"
  27. button.onclick = exec
  28. document.body.appendChild(button)
  29.  
  30. function exec () {
  31. // 分析出关键词
  32. let inputNodes = document.getElementsByTagName('input')
  33. let keyword
  34. for (let i = 0; i < inputNodes.length; i++) {
  35. const value = inputNodes[i].value
  36. if (value && inputNodes[i].style.display !== "none"
  37. && inputNodes[i].style.visibility !== "hidden"
  38. && inputNodes[i].getAttribute('type') !== "hidden") {
  39. keyword = value
  40. break
  41. }
  42. }
  43. if (!keyword) {
  44. console.log('没有分析出关键词')
  45. return
  46. }
  47. console.info('关键词:%s', keyword)
  48.  
  49. // 遍历所有超链接 找出第3个包含关键词的节点
  50. let aNode
  51. let aNodes = document.getElementsByTagName('a')
  52. let aNodeCount = 0
  53. for (let i = 0; i < aNodes.length; i++) {
  54. if (new RegExp(`${keyword}`, 'gi').test(aNodes[i].innerText)
  55. || /magnet/gi.test(aNodes[i].getAttribute('href'))) {
  56. aNode = aNodes[i]
  57. aNodeCount++
  58. }
  59. if (aNodeCount >= 3) {
  60. break
  61. }
  62. }
  63. if (!aNode) {
  64. console.log('没有分析出合适的链接')
  65. return
  66. }
  67.  
  68. const dateRegx = '(\\d{4})-(\\d{1,2})-(\\d{1,2})|ago|前'
  69. const sizeRegx = '\\d.*(字节|bytes|KB|MB|GB|TB|PB|EB|ZB|YB)'
  70. const magnetRegx = '(magnet:\\?xt).*'
  71. const hotRegx = '(^\\d+$)|(热度|hot)'
  72.  
  73. // 递归向上找出group
  74. function findItemGroupNode (node) {
  75. const parentNode = node.parentNode
  76. const regx = new RegExp(`(?=[\\s\\S]*${keyword})(?=[\\s\\S]*(${dateRegx}))(?=[\\s\\S]*(${sizeRegx}))^[\\s\\S]*$`, 'gi')
  77. if (regx.test(parentNode.textContent)) {
  78. return parentNode
  79. } else {
  80. return findItemGroupNode(parentNode)
  81. }
  82. }
  83.  
  84. const childNodes = []
  85.  
  86. function findAllChildNode (node) {
  87. let children = node.children
  88. for (let i = 0; i < children.length; i++) {
  89. // 递归遍历
  90. childNodes.push(children[i])
  91. if (children[i].children.length > 0) {
  92. findAllChildNode(children[i])
  93. }
  94. }
  95. }
  96.  
  97. function findItemValueNode (regx, attr) {
  98. for (let i = childNodes.length - 1; i >= 0; i--) {
  99. // 先检查文字
  100. if (regx.test(childNodes[i].innerText)) {
  101. return {
  102. node: childNodes[i],
  103. attrPath: ''
  104. }
  105. }
  106.  
  107. // 再检查属性
  108. if (attr) {
  109. let attributeNames = childNodes[i].getAttributeNames()
  110. for (let j = 0; j < attributeNames.length; j++) {
  111. if (regx.test(childNodes[i].getAttribute(attributeNames[j]))) {
  112. return {
  113. node: childNodes[i],
  114. attrPath: `/@${attributeNames[j]}`
  115. }
  116. }
  117. }
  118. }
  119. }
  120. }
  121.  
  122. function getRootPathTo (element) {
  123. let tagName = element.tagName.toLowerCase()
  124. if (element.id) {
  125. return `${tagName}[@id='${element.id}']`
  126. }
  127. if (element.className) {
  128. return `${tagName}[@class='${element.className}']`
  129. }
  130. let parentNode = element.parentNode
  131. if (parentNode.className) {
  132. return `${parentNode.tagName.toLowerCase()}[@class='${parentNode.className}']/${tagName}`
  133. }
  134. if (element === document.body) {
  135. return tagName
  136. }
  137. return getNumberPathTo(element)
  138. }
  139.  
  140. function getNumberPathTo (element) {
  141. let tagName = element.tagName.toLowerCase()
  142. if (element === document.body) {
  143. return tagName
  144. }
  145. let ix = 0
  146. let siblings = element.parentNode.childNodes
  147. for (let i = 0; i < siblings.length; i++) {
  148. let sibling = siblings[i]
  149. if (sibling === element) {
  150. return `${getNumberPathTo(element.parentNode)}/${tagName}[${ix + 1}]`
  151. }
  152. if (sibling.nodeType === 1 && sibling.tagName.toLowerCase() === tagName) {
  153. ix++
  154. }
  155. }
  156. }
  157.  
  158. function findSortPaths (rootUrl) {
  159. function formatPath (path) {
  160. return decodeURIComponent(path).replace(rootUrl, '')
  161. .replace(new RegExp(`${keyword}`, 'gi'), '{k}')
  162. .replace(/\d+/, '{p}')
  163. }
  164.  
  165. let preset = formatPath(window.location.href.replace(rootUrl, ''))
  166. const paths = {preset}
  167.  
  168. const sortRegx = {
  169. time: /.*时间.*|.*time.*/gi,
  170. size: /.*大小.*|.*size.*/gi,
  171. hot: /..*点击.*|.*人气.*|.*次数.*|.*hot.*|.*count.*|.*click.*/gi
  172. }
  173. let aNodes = document.getElementsByTagName('a')
  174. for (let i = 0; i < aNodes.length; i++) {
  175. let linkText = aNodes[i].innerText
  176. let href = aNodes[i].getAttribute('href')
  177. for (let key in sortRegx) {
  178. if (href && sortRegx[key].test(linkText)) {
  179. const keyPath = formatPath(href)
  180. if (keyPath !== preset) {
  181. paths[key] = keyPath
  182. }
  183. }
  184. }
  185. }
  186. return paths
  187. }
  188.  
  189. const groupNode = findItemGroupNode(aNode)
  190. if (!groupNode) {
  191. console.log('没有分析出合适的Group')
  192. return
  193. }
  194. console.log('找到Group', groupNode)
  195. findAllChildNode(groupNode)
  196. const dateWrapper = findItemValueNode(new RegExp(dateRegx, 'gi'))
  197. const sizeWrapper = findItemValueNode(new RegExp(sizeRegx, 'gi'))
  198. let magnetWrapper = findItemValueNode(new RegExp(magnetRegx, 'gi'), true)
  199. const hotWrapper = findItemValueNode(new RegExp(hotRegx, 'gi'))
  200.  
  201. let hostnameArray = window.location.hostname.split('.')
  202. const id = hostnameArray[hostnameArray.length >= 3 ? Math.floor(hostnameArray.length / 2) : 0]
  203. const url = `${window.location.protocol}//${window.location.host}`
  204.  
  205. const titleSplit = document.title.split('-')
  206. const title = titleSplit[parseInt(titleSplit.length / 2)]
  207.  
  208. const icon = `${url}/favicon.ico`
  209. const paths = findSortPaths(url)
  210.  
  211. // xpath
  212. const groupNumberPath = getNumberPathTo(groupNode)
  213. const group = `//${getRootPathTo(groupNode)}`
  214. if (!magnetWrapper) {
  215. magnetWrapper = {node: aNode, attrPath: '/@href'}
  216. console.info('没有找到磁力链,将使用详情链接代替,请检查')
  217. }
  218. const magnet = `.${getNumberPathTo(magnetWrapper.node).replace(groupNumberPath, '')}${magnetWrapper.attrPath}`
  219. const name = `.${getNumberPathTo(aNode).replace(groupNumberPath, '')}`
  220. const size = `.${getNumberPathTo(sizeWrapper.node).replace(groupNumberPath, '')}`
  221. const date = `.${getNumberPathTo(dateWrapper.node).replace(groupNumberPath, '')}`
  222. const hot = hotWrapper ? `.${getNumberPathTo(hotWrapper.node).replace(groupNumberPath, '')}` : undefined
  223. const xpath = {
  224. group, magnet, name, size, date, hot
  225. }
  226. const item = {
  227. id, name: title, url, icon, paths, xpath
  228. }
  229.  
  230. const lowVerPath = {}
  231. for (let key in item.paths) {
  232. lowVerPath[key] = item.paths[key].replace(/{k}/g, '%s').replace(/{p}/g, '%d')
  233. }
  234. const lowVer = {
  235. site: item.name,
  236. group: item.xpath.group,
  237. magnet: item.xpath.magnet,
  238. name: item.xpath.name,
  239. size: item.xpath.size,
  240. date: item.xpath.date,
  241. hot: item.xpath.hot,
  242. url: item.url,
  243. paths: lowVerPath
  244. }
  245.  
  246. const xVerUrl = item.url + item.paths[Object.keys(item.paths)[0]]
  247. const xVer = {
  248. site: item.name,
  249. waiting: "0",
  250. group: item.xpath.group,
  251. magnet: item.xpath.magnet,
  252. name: item.xpath.name,
  253. size: item.xpath.size,
  254. count: item.xpath.date,
  255. source: xVerUrl.replace(/{k}/g, 'XXX').replace(/{p}/g, 'PPP'),
  256. }
  257. console.info('\nmagnetX 规则如下')
  258. console.info(JSON.stringify(xVer, '\t', 2))
  259. console.info('\nmagnetW 2.x 规则如下')
  260. console.info(JSON.stringify(lowVer, ' ', 2))
  261. console.info('\nmagnetW 3.x 规则如下')
  262. console.info(JSON.stringify(item, '\t', 2))
  263. }
  264. })()

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址