Fanbox Batch Downloader

Batch Download on creator, not post

当前为 2019-12-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Fanbox Batch Downloader
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.51
  5. // @description Batch Download on creator, not post
  6. // @author https://github.com/amarillys
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/3.2.2/jszip.min.js
  8. // @match https://www.pixiv.net/fanbox/creator/*
  9. // @grant GM_xmlhttpRequest
  10. // @run-at document-end
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. /**
  15. * Update Log
  16. * > 191227
  17. * Code Reconstruct
  18. * Support downloading of artice
  19. * // 中文注释
  20. * 代码重构
  21. * 新增对文章的下载支持
  22. *
  23. * > 191226
  24. * Support downloading by batch(default: 100 files per batch)
  25. * Support donwloading by specific index
  26. * Change the filename
  27. * // 中文注释
  28. * 新增支持分批下载的功能(默认100个文件一个批次)
  29. * 新增支持按索引下载的功能
  30. * 修正命名
  31. * > 191223
  32. * Add support of files
  33. * Improve the detect of file extension
  34. * Change Download Request as await, for avoiding delaying.
  35. * Add manual package while click button use middle button of mouse
  36. * // 中文注释
  37. * 增加对附件下载的支持
  38. * 优化文件后缀名识别
  39. * 修改下载方式为按顺序下载,避免超时
  40. * 增加当鼠标中键点击时手动打包
  41. **/
  42.  
  43. /* global JSZip GM_xmlhttpRequest */
  44. (function () {
  45. 'use strict'
  46. let zip = null
  47. let amount = 0
  48. let uiInited = false
  49. const fetchOptions = {
  50. credentials: "include",
  51. headers: {
  52. Accept: "application/json, text/plain, */*"
  53. }
  54. }
  55.  
  56. class Zip {
  57. constructor(title) {
  58. this.title = title
  59. this.zip = new JSZip()
  60. this.size = 0
  61. this.partIndex = 0
  62. }
  63. file(filename, blob) {
  64. this.zip.file(filename, blob, {
  65. compression: "STORE"
  66. })
  67. this.size += blob.size
  68. }
  69. add(folder, name, blob) {
  70. if (this.size + blob.size >= Zip.MAX_SIZE) {
  71. let index = this.partIndex
  72. this.zip.generateAsync({ type: "blob" }).then(zipBlob =>
  73. saveBlob(zipBlob, `${this.title}-${index}.zip`))
  74. this.partIndex++
  75. this.zip = new JSZip()
  76. this.size = 0
  77. }
  78. this.zip.folder(folder).file(name, blob, {
  79. compression: "STORE"
  80. })
  81. this.size += blob.size
  82. }
  83. pack() {
  84. if (this.size === 0) return
  85. let index = this.partIndex
  86. this.zip.generateAsync({ type: "blob" }).then(zipBlob =>
  87. saveBlob(zipBlob, `${this.title}-${index}.zip`))
  88. this.partIndex++
  89. this.zip = new JSZip()
  90. this.size = 0
  91. }
  92. }
  93. Zip.MAX_SIZE = 1048576000
  94.  
  95. let init = async () => {
  96. let baseBtn = document.querySelector('[href="/fanbox/notification"]')
  97. let className = baseBtn.parentNode.className
  98. let parent = baseBtn.parentNode.parentNode
  99. let inputDiv = document.createElement("div")
  100. let creatorId = parseInt(document.URL.split("/")[5])
  101. inputDiv.innerHTML = `
  102. <input id="dlStart" style="width: 3rem" type="text" value="1"> -> <input id="dlEnd" style="width: 3rem" type="text">
  103. | 分批/Batch: <input id="dlStep" style="width: 3rem" type="text" value="100">`
  104. parent.appendChild(inputDiv)
  105.  
  106. let downloadBtn = document.createElement("div")
  107. downloadBtn.id = "FanboxDownloadBtn"
  108. downloadBtn.className = className
  109. downloadBtn.innerHTML = `
  110. <a href="javascript:void(0)">
  111. <div id="amarillys-download-progress"
  112. style="line-height: 32px;width: 8rem;height: 32px;background-color: rgba(232, 12, 2, 0.96);border-radius: 8px;color: #FFF;text-align: center">
  113. Initilizing/初始化中...
  114. </div>
  115. </a>`
  116. parent.appendChild(downloadBtn)
  117. uiInited = true
  118.  
  119. let creatorInfo = await getAllPostsByFanboxId(creatorId)
  120. amount = creatorInfo.posts.length
  121.  
  122. document.querySelector(
  123. "#amarillys-download-progress"
  124. ).innerHTML = ` Download/下载 `
  125. document.querySelector("#dlEnd").value = amount
  126. downloadBtn.addEventListener("mousedown", event => {
  127. if (event.button === 1) {
  128. zip.pack()
  129. } else {
  130. console.log("startDownloading")
  131. downloadByFanboxId(creatorInfo, creatorId)
  132. }
  133. })
  134. }
  135.  
  136. window.onload = () => {
  137. init()
  138. let timer = setInterval(() => {
  139. if (!uiInited && document.querySelector("#FanboxDownloadBtn") === null)
  140. init()
  141. else clearInterval(timer)
  142. }, 3000)
  143. }
  144.  
  145. function gmRequireImage(url) {
  146. return new Promise(resolve => {
  147. GM_xmlhttpRequest({
  148. method: "GET",
  149. url,
  150. responseType: "blob",
  151. onload: res => {
  152. resolve(res.response)
  153. }
  154. })
  155. })
  156. }
  157.  
  158. async function downloadByFanboxId(creatorInfo, creatorId) {
  159. let processed = 0
  160. let start = document.getElementById("dlStart").value - 1
  161. let end = document.getElementById("dlEnd").value
  162. zip = new Zip(`${creatorId}-${creatorInfo.name}-${start + 1}-${end}`)
  163. let stepped = 0
  164. let STEP = parseInt(document.querySelector("#dlStep").value)
  165. let textDiv = document.querySelector("#amarillys-download-progress")
  166. zip.file("cover.jpg", await gmRequireImage(creatorInfo.cover))
  167.  
  168. // start downloading
  169. for (let i = start, p = creatorInfo.posts; i < end; ++i) {
  170. let folder = `${p[i].title}-${p[i].id}`
  171. if (!p[i].body) continue
  172. let { blocks, imageMap, fileMap, files, images } = p[i].body
  173. let picIndex = 0
  174. let imageList = []
  175. let fileList = []
  176. if (p[i].type === "article") {
  177. let article = `# ${p[i].title}\n`
  178. for (let j = 0; j < blocks.length; ++j) {
  179. switch (blocks[j].type) {
  180. case "p": {
  181. article += `${blocks[j].text}\n\n`
  182. break
  183. }
  184. case "image": {
  185. picIndex++
  186. let image = imageMap[blocks[j].imageId]
  187. imageList.push(image)
  188. article += `![${p[i].title} - P${picIndex}](${blocks[j].imageId}.${image.extension})\n\n`
  189. break
  190. }
  191. case "file": {
  192. let file = fileMap[blocks[j].fileId]
  193. fileList.push(file)
  194. article += `[${p[i].title} - ${file.name}](${file.name}.${file.extension})\n\n`
  195. break
  196. }
  197. }
  198. }
  199. zip.add(folder, 'article.md', new Blob([article]))
  200. for (let j = 0; j < imageList.length; ++j) {
  201. zip.add(folder, `${imageList[j].id}.${imageList[j].extension}`,
  202. await gmRequireImage(imageList[j].originalUrl))
  203. }
  204. for (let j = 0; j < fileList.length; ++j)
  205. saveBlob(await gmRequireImage(fileList[j].url),
  206. `${creatorId}-${folder}_${j}-${fileList[j].name}.${fileList[j].extension}`)
  207. }
  208. if (files) {
  209. for (let j = 0; j < files.length; ++j) {
  210. let extension = files[j].url.split(".").slice(-1)[0]
  211. let blob = await gmRequireImage(files[j].url)
  212. saveBlob(blob, `${creatorId}-${creatorInfo.name}-${folder}_${j}.${extension}`)
  213. }
  214. }
  215. if (images) {
  216. for (let j = 0; j < images.length; ++j) {
  217. let extension = images[j].originalUrl.split(".").slice(-1)[0]
  218. textDiv.innerHTML = ` ${processed} / ${amount} `
  219. zip.add(folder, `${folder}_${j}.${extension}`, await gmRequireImage(images[j].originalUrl))
  220. }
  221. }
  222. processed++
  223. stepped++
  224. textDiv.innerHTML = ` ${processed} / ${amount} `
  225. console.log(` Progress: ${processed} / ${amount}`)
  226. if (stepped >= STEP) {
  227. zip.pack()
  228. stepped = 0
  229. }
  230. }
  231. zip.pack()
  232. textDiv.innerHTML = ` Okayed/完成 `
  233. }
  234.  
  235. async function getAllPostsByFanboxId(creatorId) {
  236. let fristUrl = `https://www.pixiv.net/ajax/fanbox/creator?userId=${creatorId}`
  237. let creatorInfo = {
  238. cover: null,
  239. posts: []
  240. }
  241. let firstData = await (await fetch(fristUrl, fetchOptions)).json()
  242. let body = firstData.body
  243. creatorInfo.cover = body.creator.coverImageUrl
  244. creatorInfo.name = body.creator.user.name
  245. creatorInfo.posts.push(...body.post.items.filter(p => p.body))
  246. let nextPageUrl = body.post.nextUrl
  247. while (nextPageUrl) {
  248. let nextData = await (await fetch(nextPageUrl, fetchOptions)).json()
  249. creatorInfo.posts.push(...nextData.body.items.filter(p => p.body))
  250. nextPageUrl = nextData.body.nextUrl
  251. }
  252. return creatorInfo
  253. }
  254.  
  255. function saveBlob(blob, fileName) {
  256. let downloadDom = document.createElement("a")
  257. document.body.appendChild(downloadDom)
  258. downloadDom.style = `display: none`
  259. let url = window.URL.createObjectURL(blob)
  260. downloadDom.href = url
  261. downloadDom.download = fileName
  262. downloadDom.click()
  263. window.URL.revokeObjectURL(url)
  264. }
  265. })()