Fanbox Batch Downloader

Batch Download on creator, not post

当前为 2020-01-02 提交的版本,查看 最新版本

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