|
24 | 24 | var dlServerType = '' // FIXME: cant queue different server types together
|
25 | 25 | var dlAggregateLinks = '' // stores all the download links as a single string
|
26 | 26 | var ts = document.getElementsByTagName('body')[0].dataset['ts'] // ts is needed to send API requests
|
| 27 | + var animeName = document.querySelectorAll('h1.title')[0].innerText |
| 28 | + // metadata stores relevant information about the |
| 29 | + // downloaded videos. It is especially helpful in |
| 30 | + // the case of RapidVideo where the filenames cant |
| 31 | + // be modified using any url params and have to be |
| 32 | + // renamed manually or by using a separate program |
| 33 | + var metadata = { |
| 34 | + animeName: animeName, |
| 35 | + animeUrl: window.location.href, |
| 36 | + files: [] |
| 37 | + } |
27 | 38 |
|
28 | 39 | // Apply styles
|
29 | 40 | var styles = [
|
|
101 | 112 | }
|
102 | 113 |
|
103 | 114 | /********************************************************************************************************************/
|
| 115 | + /** |
| 116 | + * Generates the name of the original mp4 file (RapidVideo). |
| 117 | + * @param url |
| 118 | + * @returns {*} |
| 119 | + */ |
| 120 | + function generateRVOriginal (url) { |
| 121 | + var re = /\/+[a-z0-9]+.mp4/gi |
| 122 | + var match = url.match(re) |
| 123 | + if (match.length > 0) { |
| 124 | + // since the regex us something like this |
| 125 | + // "/806FH0BFUQHP1LBGPWPZM.mp4" we need to |
| 126 | + // remove the starting slash |
| 127 | + return match[0].slice(1) |
| 128 | + } else { |
| 129 | + return '' |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Generates a 3 digit episode id from the given |
| 135 | + * id. This is id is helpful while sorting files. |
| 136 | + * @param {string} num - The episode id |
| 137 | + * @returns {string} - The 3 digit episode id |
| 138 | + */ |
| 139 | + function pad (num) { |
| 140 | + if (num.length >= 3) { |
| 141 | + return num |
| 142 | + } else { |
| 143 | + return ('000' + num).slice(-3) |
| 144 | + } |
| 145 | + } |
| 146 | + |
104 | 147 | /**
|
105 | 148 | * This function does the following
|
106 | 149 | * 1. fetch the RapidVideo page
|
|
183 | 226 | if (dlEpisodeIds.length !== 0) {
|
184 | 227 | window.dlTimeout = setTimeout(processGrabber, 2000)
|
185 | 228 | } else {
|
| 229 | + // Metadata only for RapidVideo |
| 230 | + if (dlServerType === 'RapidVideo') { |
| 231 | + // prepare the metadata |
| 232 | + metadata['timestamp'] = new Date().toISOString() |
| 233 | + metadata['server'] = dlServerType |
| 234 | + console.log(metadata) |
| 235 | + } |
| 236 | + |
186 | 237 | clearTimeout(window.dlTimeout)
|
187 | 238 | dlInProgress = false
|
188 | 239 | grabberStatus.innerHTML = 'All done. The completed links are copied to your clipboard.'
|
|
194 | 245 | * Handles the grabbing process.
|
195 | 246 | */
|
196 | 247 | function processGrabber () {
|
197 |
| - var epId = dlEpisodeIds.shift() |
198 |
| - grabberStatus.innerHTML = 'Fetching ' + epId |
| 248 | + var ep = dlEpisodeIds.shift() |
| 249 | + grabberStatus.innerHTML = 'Fetching ' + ep.num |
199 | 250 |
|
200 | 251 | var data = {
|
201 | 252 | ts: ts,
|
202 |
| - id: epId, |
| 253 | + id: ep.id, |
203 | 254 | update: 0
|
204 | 255 | }
|
205 | 256 | data['_'] = generateToken(data)
|
|
220 | 271 | getVideoLinksRV(resp['target'])
|
221 | 272 | .then(function (resp) {
|
222 | 273 | dlAggregateLinks += resp[0]['file'] + '\n'
|
223 |
| - grabberStatus.innerHTML = 'Completed ' + epId |
| 274 | + // Metadata only for RapidVideo |
| 275 | + metadata.files.push({ |
| 276 | + original: generateRVOriginal(resp[0]['file']), |
| 277 | + real: animeName + '-' + ep.num + '-' + resp[0]['label'] + '.mp4' |
| 278 | + }) |
| 279 | + grabberStatus.innerHTML = 'Completed ' + ep.num |
224 | 280 | requeue()
|
225 | 281 | })
|
226 | 282 | .catch(function () {
|
227 |
| - grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + epId + '</span>' |
| 283 | + grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + ep.num + '</span>' |
228 | 284 | requeue()
|
229 | 285 | })
|
230 | 286 | }
|
231 | 287 | })
|
232 | 288 | .catch(function () {
|
233 |
| - grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + epId + '</span>' |
| 289 | + grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + ep.num + '</span>' |
234 | 290 | requeue()
|
235 | 291 | })
|
236 | 292 | }
|
|
254 | 310 | var serverDiv = this.parentNode.parentNode
|
255 | 311 | var epLinks = serverDiv.getElementsByTagName('a')
|
256 | 312 | for (var i = 0; i < epLinks.length; i++) {
|
257 |
| - dlEpisodeIds.push(epLinks[i].dataset['id']) |
| 313 | + dlEpisodeIds.push({ |
| 314 | + num: pad(epLinks[i].dataset['base']), |
| 315 | + id: epLinks[i].dataset['id'] |
| 316 | + }) |
258 | 317 | }
|
259 | 318 | if (!dlInProgress) {
|
260 | 319 | grabberStatus.innerHTML = 'starting grabber...'
|
261 | 320 | dlServerType = this.dataset['type']
|
262 | 321 | dlInProgress = true
|
263 | 322 | dlAggregateLinks = ''
|
| 323 | + // Metadata only for RapidVideo |
| 324 | + if (dlServerType === 'RapidVideo') { |
| 325 | + metadata.files = [] |
| 326 | + } |
264 | 327 | processGrabber()
|
265 | 328 | }
|
266 | 329 | })
|
|
0 commit comments