forked from Zod-/jsVideoUrlParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.js
83 lines (67 loc) · 1.85 KB
/
template.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const {
combineParams,
getTime,
} = require('../util');
function Template() {
//Main urls that the provider users
this.provider = 'template';
//Alternatives such as shortened versions e.g. youtube.com, youtu.be
this.alternatives = ['temp'];
this.defaultFormat = 'long';
//List of formats and their functions
this.formats = {
long: this.createLongUrl,
short: this.createShortUrl,
};
this.mediaTypes = {
VIDEO: 'video',
PLAYLIST: 'playlist',
};
}
module.exports = Template;
Template.prototype.parse = function(url, params) {
//Set up the videoInfo object with relevant information
var result = {
params: params,
mediaType: this.mediaTypes.VIDEO,
};
//Parse the url with regex or the query parameters might contain the id
var match = url.match(
/com\/(?:example\/id\/)?([\w-]+)/
);
result.id = match ? match[1] : params.id;
//Parse time parameters to turn it from the string 1m30s into the number 90
if (params.start) {
result.params.start = getTime(params.start);
}
//Return nothing when parsing failed
if (!result.id) {
return undefined;
}
return result;
};
Template.prototype.createLongUrl = function(vi, params) {
var url = '';
//Create the url depending on the media type
if (vi.mediaType === this.mediaTypes.VIDEO && vi.id) {
url += 'https://template.com/example/id/' + vi.id;
} else {
return undefined;
}
//Add query parameters back e.g.
//https://template.com/example/id/abcde?foo=bar&baz=qux
url += combineParams(params);
return url;
};
Template.prototype.createShortUrl = function(vi, params) {
var url = '';
//Create shortened urls
if (vi.mediaType === this.mediaTypes.VIDEO && vi.id) {
url += 'https://temp.com/' + vi.id;
} else {
return undefined;
}
url += combineParams(params);
return url;
};
require('../base').bind(new Template());