Skip to content
This repository was archived by the owner on Mar 3, 2024. It is now read-only.

Commit b3613c9

Browse files
committed
feat: added support for episode metadata
This can then be made to a file and downloaded so that other programs can rename the downloaded files.
1 parent 5981913 commit b3613c9

File tree

1 file changed

+70
-7
lines changed

1 file changed

+70
-7
lines changed

grabber.user.js

+70-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,17 @@
2424
var dlServerType = '' // FIXME: cant queue different server types together
2525
var dlAggregateLinks = '' // stores all the download links as a single string
2626
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+
}
2738

2839
// Apply styles
2940
var styles = [
@@ -101,6 +112,38 @@
101112
}
102113

103114
/********************************************************************************************************************/
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+
104147
/**
105148
* This function does the following
106149
* 1. fetch the RapidVideo page
@@ -183,6 +226,14 @@
183226
if (dlEpisodeIds.length !== 0) {
184227
window.dlTimeout = setTimeout(processGrabber, 2000)
185228
} 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+
186237
clearTimeout(window.dlTimeout)
187238
dlInProgress = false
188239
grabberStatus.innerHTML = 'All done. The completed links are copied to your clipboard.'
@@ -194,12 +245,12 @@
194245
* Handles the grabbing process.
195246
*/
196247
function processGrabber () {
197-
var epId = dlEpisodeIds.shift()
198-
grabberStatus.innerHTML = 'Fetching ' + epId
248+
var ep = dlEpisodeIds.shift()
249+
grabberStatus.innerHTML = 'Fetching ' + ep.num
199250

200251
var data = {
201252
ts: ts,
202-
id: epId,
253+
id: ep.id,
203254
update: 0
204255
}
205256
data['_'] = generateToken(data)
@@ -220,17 +271,22 @@
220271
getVideoLinksRV(resp['target'])
221272
.then(function (resp) {
222273
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
224280
requeue()
225281
})
226282
.catch(function () {
227-
grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + epId + '</span>'
283+
grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + ep.num + '</span>'
228284
requeue()
229285
})
230286
}
231287
})
232288
.catch(function () {
233-
grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + epId + '</span>'
289+
grabberStatus.innerHTML = '<span class="grabber--fail">Failed ' + ep.num + '</span>'
234290
requeue()
235291
})
236292
}
@@ -254,13 +310,20 @@
254310
var serverDiv = this.parentNode.parentNode
255311
var epLinks = serverDiv.getElementsByTagName('a')
256312
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+
})
258317
}
259318
if (!dlInProgress) {
260319
grabberStatus.innerHTML = 'starting grabber...'
261320
dlServerType = this.dataset['type']
262321
dlInProgress = true
263322
dlAggregateLinks = ''
323+
// Metadata only for RapidVideo
324+
if (dlServerType === 'RapidVideo') {
325+
metadata.files = []
326+
}
264327
processGrabber()
265328
}
266329
})

0 commit comments

Comments
 (0)