1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global.urlParser = factory());
|
5 | }(this, (function () { 'use strict';
|
6 |
|
7 | function _typeof(obj) {
|
8 | if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
|
9 | _typeof = function (obj) {
|
10 | return typeof obj;
|
11 | };
|
12 | } else {
|
13 | _typeof = function (obj) {
|
14 | return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
|
15 | };
|
16 | }
|
17 |
|
18 | return _typeof(obj);
|
19 | }
|
20 |
|
21 | var getQueryParams = function getQueryParams(qs) {
|
22 | if (typeof qs !== 'string') {
|
23 | return {};
|
24 | }
|
25 |
|
26 | qs = qs.split('+').join(' ');
|
27 | var params = {};
|
28 | var match = qs.match(/(?:[?](?:[^=]+)=(?:[^&#]*)(?:[&](?:[^=]+)=(?:[^&#]*))*(?:[#].*)?)|(?:[#].*)/);
|
29 | var split;
|
30 |
|
31 | if (match === null) {
|
32 | return {};
|
33 | }
|
34 |
|
35 | split = match[0].substr(1).split(/[&#=]/);
|
36 |
|
37 | for (var i = 0; i < split.length; i += 2) {
|
38 | params[decodeURIComponent(split[i])] = decodeURIComponent(split[i + 1] || '');
|
39 | }
|
40 |
|
41 | return params;
|
42 | };
|
43 |
|
44 | var combineParams = function combineParams(op) {
|
45 | if (_typeof(op) !== 'object') {
|
46 | return '';
|
47 | }
|
48 |
|
49 | op.params = op.params || {};
|
50 | var combined = '',
|
51 | i = 0,
|
52 | keys = Object.keys(op.params);
|
53 |
|
54 | if (keys.length === 0) {
|
55 | return '';
|
56 | }
|
57 |
|
58 |
|
59 | keys.sort();
|
60 |
|
61 | if (!op.hasParams) {
|
62 | combined += '?' + keys[0] + '=' + op.params[keys[0]];
|
63 | i += 1;
|
64 | }
|
65 |
|
66 | for (; i < keys.length; i += 1) {
|
67 | combined += '&' + keys[i] + '=' + op.params[keys[i]];
|
68 | }
|
69 |
|
70 | return combined;
|
71 | };
|
72 |
|
73 |
|
74 | function getLetterTime(timeString) {
|
75 | var totalSeconds = 0;
|
76 | var timeValues = {
|
77 | 's': 1,
|
78 | 'm': 1 * 60,
|
79 | 'h': 1 * 60 * 60,
|
80 | 'd': 1 * 60 * 60 * 24,
|
81 | 'w': 1 * 60 * 60 * 24 * 7
|
82 | };
|
83 | var timePairs;
|
84 |
|
85 | timeString = timeString.replace(/([smhdw])/g, ' $1 ').trim();
|
86 | timePairs = timeString.split(' ');
|
87 |
|
88 | for (var i = 0; i < timePairs.length; i += 2) {
|
89 | totalSeconds += parseInt(timePairs[i], 10) * timeValues[timePairs[i + 1] || 's'];
|
90 | }
|
91 |
|
92 | return totalSeconds;
|
93 | }
|
94 |
|
95 |
|
96 | function getColonTime(timeString) {
|
97 | var totalSeconds = 0;
|
98 | var timeValues = [1, 1 * 60, 1 * 60 * 60, 1 * 60 * 60 * 24, 1 * 60 * 60 * 24 * 7];
|
99 | var timePairs = timeString.split(':');
|
100 |
|
101 | for (var i = 0; i < timePairs.length; i++) {
|
102 | totalSeconds += parseInt(timePairs[i], 10) * timeValues[timePairs.length - i - 1];
|
103 | }
|
104 |
|
105 | return totalSeconds;
|
106 | }
|
107 |
|
108 | var getTime = function getTime(timeString) {
|
109 | if (typeof timeString === 'undefined') {
|
110 | return 0;
|
111 | }
|
112 |
|
113 | if (timeString.match(/^(\d+[smhdw]?)+$/)) {
|
114 | return getLetterTime(timeString);
|
115 | }
|
116 |
|
117 | if (timeString.match(/^(\d+:?)+$/)) {
|
118 | return getColonTime(timeString);
|
119 | }
|
120 |
|
121 | return 0;
|
122 | };
|
123 |
|
124 | var util = {
|
125 | getQueryParams: getQueryParams,
|
126 | combineParams: combineParams,
|
127 | getTime: getTime
|
128 | };
|
129 |
|
130 | var getQueryParams$1 = util.getQueryParams;
|
131 |
|
132 | function UrlParser() {
|
133 | var _arr = ['parseProvider', 'parse', 'bind', 'create'];
|
134 |
|
135 | for (var _i = 0; _i < _arr.length; _i++) {
|
136 | var key = _arr[_i];
|
137 | this[key] = this[key].bind(this);
|
138 | }
|
139 |
|
140 | this.plugins = {};
|
141 | }
|
142 |
|
143 | var urlParser = UrlParser;
|
144 |
|
145 | UrlParser.prototype.parseProvider = function (url) {
|
146 | var match = url.match(/(?:(?:https?:)?\/\/)?(?:[^.]+\.)?(\w+)\./i);
|
147 | return match ? match[1] : undefined;
|
148 | };
|
149 |
|
150 | UrlParser.prototype.parse = function (url) {
|
151 | if (typeof url === 'undefined') {
|
152 | return undefined;
|
153 | }
|
154 |
|
155 | var provider = this.parseProvider(url);
|
156 | var result;
|
157 | var plugin = this.plugins[provider];
|
158 |
|
159 | if (!provider || !plugin || !plugin.parse) {
|
160 | return undefined;
|
161 | }
|
162 |
|
163 | result = plugin.parse.call(plugin, url, getQueryParams$1(url));
|
164 |
|
165 | if (result) {
|
166 | result = removeEmptyParameters(result);
|
167 | result.provider = plugin.provider;
|
168 | }
|
169 |
|
170 | return result;
|
171 | };
|
172 |
|
173 | UrlParser.prototype.bind = function (plugin) {
|
174 | this.plugins[plugin.provider] = plugin;
|
175 |
|
176 | if (plugin.alternatives) {
|
177 | for (var i = 0; i < plugin.alternatives.length; i += 1) {
|
178 | this.plugins[plugin.alternatives[i]] = plugin;
|
179 | }
|
180 | }
|
181 | };
|
182 |
|
183 | UrlParser.prototype.create = function (op) {
|
184 | var vi = op.videoInfo;
|
185 | var params = op.params;
|
186 | var plugin = this.plugins[vi.provider];
|
187 | params = params === 'internal' ? vi.params : params || {};
|
188 |
|
189 | if (plugin) {
|
190 | op.format = op.format || plugin.defaultFormat;
|
191 |
|
192 | if (plugin.formats.hasOwnProperty(op.format)) {
|
193 | return plugin.formats[op.format].apply(plugin, [vi, Object.assign({}, params)]);
|
194 | }
|
195 | }
|
196 |
|
197 | return undefined;
|
198 | };
|
199 |
|
200 | function removeEmptyParameters(result) {
|
201 | if (result.params && Object.keys(result.params).length === 0) {
|
202 | delete result.params;
|
203 | }
|
204 |
|
205 | return result;
|
206 | }
|
207 |
|
208 | var parser = new urlParser();
|
209 | var base = parser;
|
210 |
|
211 | var combineParams$1 = util.combineParams;
|
212 |
|
213 | function CanalPlus() {
|
214 | this.provider = 'canalplus';
|
215 | this.defaultFormat = 'embed';
|
216 | this.formats = {
|
217 | embed: this.createEmbedUrl
|
218 | };
|
219 | this.mediaTypes = {
|
220 | VIDEO: 'video'
|
221 | };
|
222 | }
|
223 |
|
224 | CanalPlus.prototype.parseParameters = function (params) {
|
225 | delete params.vid;
|
226 | return params;
|
227 | };
|
228 |
|
229 | CanalPlus.prototype.parse = function (url, params) {
|
230 | var _this = this;
|
231 |
|
232 | var result = {
|
233 | mediaType: this.mediaTypes.VIDEO,
|
234 | id: params.vid
|
235 | };
|
236 | result.params = _this.parseParameters(params);
|
237 |
|
238 | if (!result.id) {
|
239 | return undefined;
|
240 | }
|
241 |
|
242 | return result;
|
243 | };
|
244 |
|
245 | CanalPlus.prototype.createEmbedUrl = function (vi, params) {
|
246 | var url = 'http://player.canalplus.fr/embed/';
|
247 | params.vid = vi.id;
|
248 | url += combineParams$1({
|
249 | params: params
|
250 | });
|
251 | return url;
|
252 | };
|
253 |
|
254 | base.bind(new CanalPlus());
|
255 |
|
256 | var combineParams$2 = util.combineParams;
|
257 |
|
258 | function Coub() {
|
259 | this.provider = 'coub';
|
260 | this.defaultFormat = 'long';
|
261 | this.formats = {
|
262 | long: this.createLongUrl,
|
263 | embed: this.createEmbedUrl
|
264 | };
|
265 | this.mediaTypes = {
|
266 | VIDEO: 'video'
|
267 | };
|
268 | }
|
269 |
|
270 | Coub.prototype.parseUrl = function (url) {
|
271 | var match = url.match(/(?:embed|view)\/([a-zA-Z\d]+)/i);
|
272 | return match ? match[1] : undefined;
|
273 | };
|
274 |
|
275 | Coub.prototype.parse = function (url, params) {
|
276 | var result = {
|
277 | mediaType: this.mediaTypes.VIDEO,
|
278 | params: params,
|
279 | id: this.parseUrl(url)
|
280 | };
|
281 |
|
282 | if (!result.id) {
|
283 | return undefined;
|
284 | }
|
285 |
|
286 | return result;
|
287 | };
|
288 |
|
289 | Coub.prototype.createUrl = function (baseUrl, vi, params) {
|
290 | var url = baseUrl + vi.id;
|
291 | url += combineParams$2({
|
292 | params: params
|
293 | });
|
294 | return url;
|
295 | };
|
296 |
|
297 | Coub.prototype.createLongUrl = function (vi, params) {
|
298 | return this.createUrl('https://coub.com/view/', vi, params);
|
299 | };
|
300 |
|
301 | Coub.prototype.createEmbedUrl = function (vi, params) {
|
302 | return this.createUrl('//coub.com/embed/', vi, params);
|
303 | };
|
304 |
|
305 | base.bind(new Coub());
|
306 |
|
307 | var combineParams$3 = util.combineParams;
|
308 | var getTime$1 = util.getTime;
|
309 |
|
310 | function Dailymotion() {
|
311 | this.provider = 'dailymotion';
|
312 | this.alternatives = ['dai'];
|
313 | this.defaultFormat = 'long';
|
314 | this.formats = {
|
315 | short: this.createShortUrl,
|
316 | long: this.createLongUrl,
|
317 | embed: this.createEmbedUrl
|
318 | };
|
319 | this.mediaTypes = {
|
320 | VIDEO: 'video'
|
321 | };
|
322 | }
|
323 |
|
324 | Dailymotion.prototype.parseParameters = function (params) {
|
325 | return this.parseTime(params);
|
326 | };
|
327 |
|
328 | Dailymotion.prototype.parseTime = function (params) {
|
329 | if (params.start) {
|
330 | params.start = getTime$1(params.start);
|
331 | }
|
332 |
|
333 | return params;
|
334 | };
|
335 |
|
336 | Dailymotion.prototype.parseUrl = function (url) {
|
337 | var match = url.match(/(?:\/video|ly)\/([A-Za-z0-9]+)/i);
|
338 | return match ? match[1] : undefined;
|
339 | };
|
340 |
|
341 | Dailymotion.prototype.parse = function (url, params) {
|
342 | var _this = this;
|
343 |
|
344 | var result = {
|
345 | mediaType: this.mediaTypes.VIDEO,
|
346 | params: _this.parseParameters(params),
|
347 | id: _this.parseUrl(url)
|
348 | };
|
349 | return result.id ? result : undefined;
|
350 | };
|
351 |
|
352 | Dailymotion.prototype.createUrl = function (base$$2, vi, params) {
|
353 | return base$$2 + vi.id + combineParams$3({
|
354 | params: params
|
355 | });
|
356 | };
|
357 |
|
358 | Dailymotion.prototype.createShortUrl = function (vi) {
|
359 | return this.createUrl('https://dai.ly/', vi, {});
|
360 | };
|
361 |
|
362 | Dailymotion.prototype.createLongUrl = function (vi, params) {
|
363 | return this.createUrl('https://dailymotion.com/video/', vi, params);
|
364 | };
|
365 |
|
366 | Dailymotion.prototype.createEmbedUrl = function (vi, params) {
|
367 | return this.createUrl('//www.dailymotion.com/embed/video/', vi, params);
|
368 | };
|
369 |
|
370 | base.bind(new Dailymotion());
|
371 |
|
372 | var combineParams$4 = util.combineParams;
|
373 | var getTime$2 = util.getTime;
|
374 |
|
375 | function Twitch() {
|
376 | this.provider = 'twitch';
|
377 | this.defaultFormat = 'long';
|
378 | this.formats = {
|
379 | long: this.createLongUrl,
|
380 | embed: this.createEmbedUrl
|
381 | };
|
382 | this.mediaTypes = {
|
383 | VIDEO: 'video',
|
384 | STREAM: 'stream',
|
385 | CLIP: 'clip'
|
386 | };
|
387 | }
|
388 |
|
389 | Twitch.prototype.seperateId = function (id) {
|
390 | return {
|
391 | pre: id[0],
|
392 | id: id.substr(1)
|
393 | };
|
394 | };
|
395 |
|
396 | Twitch.prototype.parseChannel = function (result, params) {
|
397 | var channel = params.channel || params.utm_content || result.channel;
|
398 | delete params.utm_content;
|
399 | delete params.channel;
|
400 | return channel;
|
401 | };
|
402 |
|
403 | Twitch.prototype.parseUrl = function (url, result, params) {
|
404 | var match;
|
405 | match = url.match(/(clips\.)?twitch\.tv\/(?:(?:videos\/(\d+))|(\w+))?/i);
|
406 |
|
407 | if (match && match[2]) {
|
408 |
|
409 | result.id = 'v' + match[2];
|
410 | } else if (params.video) {
|
411 |
|
412 | result.id = params.video;
|
413 | delete params.video;
|
414 | } else if (params.clip) {
|
415 |
|
416 | result.id = params.clip;
|
417 | result.isClip = true;
|
418 | delete params.clip;
|
419 | } else if (match && match[1] && match[3]) {
|
420 |
|
421 | result.id = match[3];
|
422 | result.isClip = true;
|
423 | } else if (match && match[3]) {
|
424 | result.channel = match[3];
|
425 | }
|
426 |
|
427 | return result;
|
428 | };
|
429 |
|
430 | Twitch.prototype.parseMediaType = function (result) {
|
431 | var mediaType;
|
432 |
|
433 | if (result.channel) {
|
434 | mediaType = this.mediaTypes.STREAM;
|
435 | } else if (result.id) {
|
436 | if (result.isClip) {
|
437 | mediaType = this.mediaTypes.CLIP;
|
438 | delete result.isClip;
|
439 | } else {
|
440 | mediaType = this.mediaTypes.VIDEO;
|
441 | }
|
442 |
|
443 | delete result.channel;
|
444 | }
|
445 |
|
446 | return mediaType;
|
447 | };
|
448 |
|
449 | Twitch.prototype.parseParameters = function (params) {
|
450 | if (params.t) {
|
451 | params.start = getTime$2(params.t);
|
452 | delete params.t;
|
453 | }
|
454 |
|
455 | return params;
|
456 | };
|
457 |
|
458 | Twitch.prototype.parse = function (url, params) {
|
459 | var _this = this;
|
460 |
|
461 | var result = {};
|
462 | result = _this.parseUrl(url, result, params);
|
463 | result.channel = _this.parseChannel(result, params);
|
464 | result.mediaType = _this.parseMediaType(result);
|
465 | result.params = _this.parseParameters(params);
|
466 | return result.channel || result.id ? result : undefined;
|
467 | };
|
468 |
|
469 | Twitch.prototype.createLongUrl = function (vi, params) {
|
470 | var url = '';
|
471 |
|
472 | if (vi.mediaType === this.mediaTypes.STREAM) {
|
473 | url = 'https://twitch.tv/' + vi.channel;
|
474 | }
|
475 |
|
476 | if (vi.mediaType === this.mediaTypes.VIDEO) {
|
477 | var sep = this.seperateId(vi.id);
|
478 | url = 'https://twitch.tv/videos/' + sep.id;
|
479 |
|
480 | if (params.start) {
|
481 | params.t = params.start + 's';
|
482 | delete params.start;
|
483 | }
|
484 | }
|
485 |
|
486 | if (vi.mediaType === this.mediaTypes.CLIP) {
|
487 | url = 'https://clips.twitch.tv/' + vi.id;
|
488 | }
|
489 |
|
490 | url += combineParams$4({
|
491 | params: params
|
492 | });
|
493 | return url;
|
494 | };
|
495 |
|
496 | Twitch.prototype.createEmbedUrl = function (vi, params) {
|
497 | var url = 'https://player.twitch.tv/';
|
498 |
|
499 | if (vi.mediaType === this.mediaTypes.STREAM) {
|
500 | params.channel = vi.channel;
|
501 | }
|
502 |
|
503 | if (vi.mediaType === this.mediaTypes.VIDEO) {
|
504 | params.video = vi.id;
|
505 |
|
506 | if (params.start) {
|
507 | params.t = params.start + 's';
|
508 | delete params.start;
|
509 | }
|
510 | }
|
511 |
|
512 | if (vi.mediaType === this.mediaTypes.CLIP) {
|
513 | url = 'https://clips.twitch.tv/embed';
|
514 | params.clip = vi.id;
|
515 | }
|
516 |
|
517 | url += combineParams$4({
|
518 | params: params
|
519 | });
|
520 | return url;
|
521 | };
|
522 |
|
523 | base.bind(new Twitch());
|
524 |
|
525 | var combineParams$5 = util.combineParams;
|
526 | var getTime$3 = util.getTime;
|
527 |
|
528 | function Vimeo() {
|
529 | this.provider = 'vimeo';
|
530 | this.alternatives = ['vimeopro'];
|
531 | this.defaultFormat = 'long';
|
532 | this.formats = {
|
533 | long: this.createLongUrl,
|
534 | embed: this.createEmbedUrl
|
535 | };
|
536 | this.mediaTypes = {
|
537 | VIDEO: 'video'
|
538 | };
|
539 | }
|
540 |
|
541 | Vimeo.prototype.parseUrl = function (url) {
|
542 | var match = url.match(/(?:\/(?:channels\/[\w]+|(?:(?:album\/\d+|groups\/[\w]+)\/)?videos?))?\/(\d+)/i);
|
543 | return match ? match[1] : undefined;
|
544 | };
|
545 |
|
546 | Vimeo.prototype.parseParameters = function (params) {
|
547 | return this.parseTime(params);
|
548 | };
|
549 |
|
550 | Vimeo.prototype.parseTime = function (params) {
|
551 | if (params.t) {
|
552 | params.start = getTime$3(params.t);
|
553 | delete params.t;
|
554 | }
|
555 |
|
556 | return params;
|
557 | };
|
558 |
|
559 | Vimeo.prototype.parse = function (url, params) {
|
560 | var result = {
|
561 | mediaType: this.mediaTypes.VIDEO,
|
562 | params: this.parseParameters(params),
|
563 | id: this.parseUrl(url)
|
564 | };
|
565 | return result.id ? result : undefined;
|
566 | };
|
567 |
|
568 | Vimeo.prototype.createUrl = function (baseUrl, vi, params) {
|
569 | var url = baseUrl + vi.id;
|
570 | var startTime = params.start;
|
571 | delete params.start;
|
572 | url += combineParams$5({
|
573 | params: params
|
574 | });
|
575 |
|
576 | if (startTime) {
|
577 | url += '#t=' + startTime;
|
578 | }
|
579 |
|
580 | return url;
|
581 | };
|
582 |
|
583 | Vimeo.prototype.createLongUrl = function (vi, params) {
|
584 | return this.createUrl('https://vimeo.com/', vi, params);
|
585 | };
|
586 |
|
587 | Vimeo.prototype.createEmbedUrl = function (vi, params) {
|
588 | return this.createUrl('//player.vimeo.com/video/', vi, params);
|
589 | };
|
590 |
|
591 | base.bind(new Vimeo());
|
592 |
|
593 | var combineParams$6 = util.combineParams;
|
594 | var getTime$4 = util.getTime;
|
595 |
|
596 | function Wistia() {
|
597 | this.provider = 'wistia';
|
598 | this.alternatives = [];
|
599 | this.defaultFormat = 'long';
|
600 | this.formats = {
|
601 | long: this.createLongUrl,
|
602 | embed: this.createEmbedUrl,
|
603 | embedjsonp: this.createEmbedJsonpUrl
|
604 | };
|
605 | this.mediaTypes = {
|
606 | VIDEO: 'video',
|
607 | EMBEDVIDEO: 'embedvideo'
|
608 | };
|
609 | }
|
610 |
|
611 | Wistia.prototype.parseUrl = function (url) {
|
612 | var match = url.match(/(?:(?:medias|iframe)\/|wvideo=)([\w-]+)/);
|
613 | return match ? match[1] : undefined;
|
614 | };
|
615 |
|
616 | Wistia.prototype.parseChannel = function (url) {
|
617 | var match = url.match(/(?:(?:https?:)?\/\/)?([^.]*)\.wistia\./);
|
618 | var channel = match ? match[1] : undefined;
|
619 |
|
620 | if (channel === 'fast' || channel === 'content') {
|
621 | return undefined;
|
622 | }
|
623 |
|
624 | return channel;
|
625 | };
|
626 |
|
627 | Wistia.prototype.parseParameters = function (params, result) {
|
628 | if (params.wtime) {
|
629 | params.start = getTime$4(params.wtime);
|
630 | delete params.wtime;
|
631 | }
|
632 |
|
633 | if (params.wvideo === result.id) {
|
634 | delete params.wvideo;
|
635 | }
|
636 |
|
637 | return params;
|
638 | };
|
639 |
|
640 | Wistia.prototype.parseMediaType = function (result) {
|
641 | if (result.id && result.channel) {
|
642 | return this.mediaTypes.VIDEO;
|
643 | } else if (result.id) {
|
644 | delete result.channel;
|
645 | return this.mediaTypes.EMBEDVIDEO;
|
646 | } else {
|
647 | return undefined;
|
648 | }
|
649 | };
|
650 |
|
651 | Wistia.prototype.parse = function (url, params) {
|
652 | var result = {
|
653 | id: this.parseUrl(url),
|
654 | channel: this.parseChannel(url)
|
655 | };
|
656 | result.params = this.parseParameters(params, result);
|
657 | result.mediaType = this.parseMediaType(result);
|
658 |
|
659 | if (!result.id) {
|
660 | return undefined;
|
661 | }
|
662 |
|
663 | return result;
|
664 | };
|
665 |
|
666 | Wistia.prototype.createUrl = function (vi, params, url) {
|
667 | if (params.start) {
|
668 | params.wtime = params.start;
|
669 | delete params.start;
|
670 | }
|
671 |
|
672 | url += combineParams$6({
|
673 | params: params
|
674 | });
|
675 | return url;
|
676 | };
|
677 |
|
678 | Wistia.prototype.createLongUrl = function (vi, params) {
|
679 | if (vi.mediaType !== this.mediaTypes.VIDEO) {
|
680 | return '';
|
681 | }
|
682 |
|
683 | var url = 'https://' + vi.channel + '.wistia.com/medias/' + vi.id;
|
684 | return this.createUrl(vi, params, url);
|
685 | };
|
686 |
|
687 | Wistia.prototype.createEmbedUrl = function (vi, params) {
|
688 | var url = 'https://fast.wistia.com/embed/iframe/' + vi.id;
|
689 | return this.createUrl(vi, params, url);
|
690 | };
|
691 |
|
692 | Wistia.prototype.createEmbedJsonpUrl = function (vi) {
|
693 | return 'https://fast.wistia.com/embed/medias/' + vi.id + '.jsonp';
|
694 | };
|
695 |
|
696 | base.bind(new Wistia());
|
697 |
|
698 | var combineParams$7 = util.combineParams;
|
699 |
|
700 | function Youku() {
|
701 | this.provider = 'youku';
|
702 | this.defaultFormat = 'long';
|
703 | this.formats = {
|
704 | embed: this.createEmbedUrl,
|
705 | long: this.createLongUrl,
|
706 | flash: this.createFlashUrl,
|
707 | static: this.createStaticUrl
|
708 | };
|
709 | this.mediaTypes = {
|
710 | VIDEO: 'video'
|
711 | };
|
712 | }
|
713 |
|
714 | Youku.prototype.parseUrl = function (url) {
|
715 | var match = url.match(/(?:(?:embed|sid)\/|v_show\/id_|VideoIDS=)([a-zA-Z0-9]+)/);
|
716 | return match ? match[1] : undefined;
|
717 | };
|
718 |
|
719 | Youku.prototype.parseParameters = function (params) {
|
720 | if (params.VideoIDS) {
|
721 | delete params.VideoIDS;
|
722 | }
|
723 |
|
724 | return params;
|
725 | };
|
726 |
|
727 | Youku.prototype.parse = function (url, params) {
|
728 | var _this = this;
|
729 |
|
730 | var result = {
|
731 | mediaType: this.mediaTypes.VIDEO,
|
732 | id: _this.parseUrl(url),
|
733 | params: _this.parseParameters(params)
|
734 | };
|
735 |
|
736 | if (!result.id) {
|
737 | return undefined;
|
738 | }
|
739 |
|
740 | return result;
|
741 | };
|
742 |
|
743 | Youku.prototype.createUrl = function (baseUrl, vi, params) {
|
744 | var url = baseUrl + vi.id;
|
745 | url += combineParams$7({
|
746 | params: params
|
747 | });
|
748 | return url;
|
749 | };
|
750 |
|
751 | Youku.prototype.createEmbedUrl = function (vi, params) {
|
752 | return this.createUrl('http://player.youku.com/embed/', vi, params);
|
753 | };
|
754 |
|
755 | Youku.prototype.createLongUrl = function (vi, params) {
|
756 | return this.createUrl('http://v.youku.com/v_show/id_', vi, params);
|
757 | };
|
758 |
|
759 | Youku.prototype.createStaticUrl = function (vi, params) {
|
760 | return this.createUrl('http://static.youku.com/v1.0.0638/v/swf/loader.swf?VideoIDS=', vi, params);
|
761 | };
|
762 |
|
763 | Youku.prototype.createFlashUrl = function (vi, params) {
|
764 | var url = 'http://player.youku.com/player.php/sid/' + vi.id + '/v.swf';
|
765 | url += combineParams$7({
|
766 | params: params
|
767 | });
|
768 | return url;
|
769 | };
|
770 |
|
771 | base.bind(new Youku());
|
772 |
|
773 | var combineParams$8 = util.combineParams;
|
774 | var getTime$5 = util.getTime;
|
775 |
|
776 | function YouTube() {
|
777 | this.provider = 'youtube';
|
778 | this.alternatives = ['youtu', 'ytimg'];
|
779 | this.defaultFormat = 'long';
|
780 | this.formats = {
|
781 | short: this.createShortUrl,
|
782 | long: this.createLongUrl,
|
783 | embed: this.createEmbedUrl,
|
784 | shortImage: this.createShortImageUrl,
|
785 | longImage: this.createLongImageUrl
|
786 | };
|
787 | this.imageQualities = {
|
788 | '0': '0',
|
789 | '1': '1',
|
790 | '2': '2',
|
791 | '3': '3',
|
792 | DEFAULT: 'default',
|
793 | HQDEFAULT: 'hqdefault',
|
794 | SDDEFAULT: 'sddefault',
|
795 | MQDEFAULT: 'mqdefault',
|
796 | MAXRESDEFAULT: 'maxresdefault'
|
797 | };
|
798 | this.defaultImageQuality = this.imageQualities.HQDEFAULT;
|
799 | this.mediaTypes = {
|
800 | VIDEO: 'video',
|
801 | PLAYLIST: 'playlist',
|
802 | SHARE: 'share',
|
803 | CHANNEL: 'channel'
|
804 | };
|
805 | }
|
806 |
|
807 | YouTube.prototype.parseVideoUrl = function (url) {
|
808 | var match = url.match(/(?:(?:v|vi|be|videos|embed)\/(?!videoseries)|(?:v|ci)=)([\w-]{11})/i);
|
809 | return match ? match[1] : undefined;
|
810 | };
|
811 |
|
812 | YouTube.prototype.parseChannelUrl = function (url) {
|
813 |
|
814 | var match = url.match(/\/channel\/([\w-]+)/);
|
815 |
|
816 | if (match) {
|
817 | return {
|
818 | id: match[1],
|
819 | mediaType: this.mediaTypes.CHANNEL
|
820 | };
|
821 | }
|
822 |
|
823 |
|
824 |
|
825 | match = url.match(/\/(?:c|user)\/([\w-]+)/);
|
826 |
|
827 | if (match) {
|
828 | return {
|
829 | name: match[1],
|
830 | mediaType: this.mediaTypes.CHANNEL
|
831 | };
|
832 | }
|
833 | };
|
834 |
|
835 | YouTube.prototype.parseParameters = function (params, result) {
|
836 | if (params.start || params.t) {
|
837 | params.start = getTime$5(params.start || params.t);
|
838 | delete params.t;
|
839 | }
|
840 |
|
841 | if (params.v === result.id) {
|
842 | delete params.v;
|
843 | }
|
844 |
|
845 | if (params.list === result.id) {
|
846 | delete params.list;
|
847 | }
|
848 |
|
849 | return params;
|
850 | };
|
851 |
|
852 | YouTube.prototype.parseMediaType = function (result) {
|
853 | if (result.params.list) {
|
854 | result.list = result.params.list;
|
855 | delete result.params.list;
|
856 | }
|
857 |
|
858 | if (result.id && !result.params.ci) {
|
859 | result.mediaType = this.mediaTypes.VIDEO;
|
860 | } else if (result.list) {
|
861 | delete result.id;
|
862 | result.mediaType = this.mediaTypes.PLAYLIST;
|
863 | } else if (result.params.ci) {
|
864 | delete result.params.ci;
|
865 | result.mediaType = this.mediaTypes.SHARE;
|
866 | } else {
|
867 | return undefined;
|
868 | }
|
869 |
|
870 | return result;
|
871 | };
|
872 |
|
873 | YouTube.prototype.parse = function (url, params) {
|
874 | var channelResult = this.parseChannelUrl(url);
|
875 |
|
876 | if (channelResult) {
|
877 | return channelResult;
|
878 | } else {
|
879 | var result = {
|
880 | params: params,
|
881 | id: this.parseVideoUrl(url)
|
882 | };
|
883 | result.params = this.parseParameters(params, result);
|
884 | result = this.parseMediaType(result);
|
885 | return result;
|
886 | }
|
887 | };
|
888 |
|
889 | YouTube.prototype.createShortUrl = function (vi, params) {
|
890 | var url = 'https://youtu.be/' + vi.id;
|
891 |
|
892 | if (params.start) {
|
893 | url += '#t=' + params.start;
|
894 | }
|
895 |
|
896 | return url;
|
897 | };
|
898 |
|
899 | YouTube.prototype.createLongUrl = function (vi, params) {
|
900 | var url = '';
|
901 | var startTime = params.start;
|
902 | delete params.start;
|
903 |
|
904 | if (vi.mediaType === this.mediaTypes.CHANNEL) {
|
905 | if (vi.id) {
|
906 | url += 'https://www.youtube.com/channel/' + vi.id;
|
907 | } else if (vi.name) {
|
908 | url += 'https://www.youtube.com/c/' + vi.name;
|
909 | }
|
910 | }
|
911 |
|
912 | if (vi.mediaType === this.mediaTypes.PLAYLIST) {
|
913 | params.feature = 'share';
|
914 | url += 'https://www.youtube.com/playlist';
|
915 | }
|
916 |
|
917 | if (vi.mediaType === this.mediaTypes.VIDEO) {
|
918 | params.v = vi.id;
|
919 | url += 'https://www.youtube.com/watch';
|
920 | }
|
921 |
|
922 | if (vi.mediaType === this.mediaTypes.SHARE) {
|
923 | params.ci = vi.id;
|
924 | url += 'https://www.youtube.com/shared';
|
925 | }
|
926 |
|
927 | if (vi.list) {
|
928 | params.list = vi.list;
|
929 | }
|
930 |
|
931 | url += combineParams$8({
|
932 | params: params
|
933 | });
|
934 |
|
935 | if (vi.mediaType !== this.mediaTypes.PLAYLIST && startTime) {
|
936 | url += '#t=' + startTime;
|
937 | }
|
938 |
|
939 | return url;
|
940 | };
|
941 |
|
942 | YouTube.prototype.createEmbedUrl = function (vi, params) {
|
943 | var url = 'https://www.youtube.com/embed';
|
944 |
|
945 | if (vi.mediaType === this.mediaTypes.PLAYLIST) {
|
946 | params.listType = 'playlist';
|
947 | } else {
|
948 | url += '/' + vi.id;
|
949 |
|
950 | if (params.loop === '1') {
|
951 | params.playlist = vi.id;
|
952 | }
|
953 | }
|
954 |
|
955 | if (vi.list) {
|
956 | params.list = vi.list;
|
957 | }
|
958 |
|
959 | url += combineParams$8({
|
960 | params: params
|
961 | });
|
962 | return url;
|
963 | };
|
964 |
|
965 | YouTube.prototype.createImageUrl = function (baseUrl, vi, params) {
|
966 | var url = baseUrl + vi.id + '/';
|
967 | var quality = params.imageQuality || this.defaultImageQuality;
|
968 | return url + quality + '.jpg';
|
969 | };
|
970 |
|
971 | YouTube.prototype.createShortImageUrl = function (vi, params) {
|
972 | return this.createImageUrl('https://i.ytimg.com/vi/', vi, params);
|
973 | };
|
974 |
|
975 | YouTube.prototype.createLongImageUrl = function (vi, params) {
|
976 | return this.createImageUrl('https://img.youtube.com/vi/', vi, params);
|
977 | };
|
978 |
|
979 | base.bind(new YouTube());
|
980 |
|
981 | var combineParams$9 = util.combineParams;
|
982 | var getTime$6 = util.getTime;
|
983 |
|
984 | function SoundCloud() {
|
985 | this.provider = 'soundcloud';
|
986 | this.defaultFormat = 'long';
|
987 | this.formats = {
|
988 | long: this.createLongUrl,
|
989 | embed: this.createEmbedUrl
|
990 | };
|
991 | this.mediaTypes = {
|
992 | TRACK: 'track',
|
993 | PLAYLIST: 'playlist',
|
994 | APITRACK: 'apitrack',
|
995 | APIPLAYLIST: 'apiplaylist'
|
996 | };
|
997 | }
|
998 |
|
999 | SoundCloud.prototype.parseUrl = function (url, result) {
|
1000 | var match = url.match(/soundcloud\.com\/(?:([\w-]+)\/(sets\/)?)([\w-]+)/i);
|
1001 |
|
1002 | if (!match) {
|
1003 | return result;
|
1004 | }
|
1005 |
|
1006 | result.channel = match[1];
|
1007 |
|
1008 | if (match[1] === 'playlists' || match[2]) {
|
1009 |
|
1010 | result.list = match[3];
|
1011 | } else {
|
1012 |
|
1013 | result.id = match[3];
|
1014 | }
|
1015 |
|
1016 | return result;
|
1017 | };
|
1018 |
|
1019 | SoundCloud.prototype.parseParameters = function (params) {
|
1020 | if (params.t) {
|
1021 | params.start = getTime$6(params.t);
|
1022 | delete params.t;
|
1023 | }
|
1024 |
|
1025 | return params;
|
1026 | };
|
1027 |
|
1028 | SoundCloud.prototype.parseMediaType = function (result) {
|
1029 | if (result.id) {
|
1030 | if (result.channel === 'tracks') {
|
1031 | delete result.channel;
|
1032 | delete result.params.url;
|
1033 | result.mediaType = this.mediaTypes.APITRACK;
|
1034 | } else {
|
1035 | result.mediaType = this.mediaTypes.TRACK;
|
1036 | }
|
1037 | }
|
1038 |
|
1039 | if (result.list) {
|
1040 | if (result.channel === 'playlists') {
|
1041 | delete result.channel;
|
1042 | delete result.params.url;
|
1043 | result.mediaType = this.mediaTypes.APIPLAYLIST;
|
1044 | } else {
|
1045 | result.mediaType = this.mediaTypes.PLAYLIST;
|
1046 | }
|
1047 | }
|
1048 |
|
1049 | return result;
|
1050 | };
|
1051 |
|
1052 | SoundCloud.prototype.parse = function (url, params) {
|
1053 | var result = {};
|
1054 | result = this.parseUrl(url, result);
|
1055 | result.params = this.parseParameters(params);
|
1056 | result = this.parseMediaType(result);
|
1057 |
|
1058 | if (!result.id && !result.list) {
|
1059 | return undefined;
|
1060 | }
|
1061 |
|
1062 | return result;
|
1063 | };
|
1064 |
|
1065 | SoundCloud.prototype.createLongUrl = function (vi, params) {
|
1066 | var url = '';
|
1067 | var startTime = params.start;
|
1068 | delete params.start;
|
1069 |
|
1070 | if (vi.mediaType === this.mediaTypes.TRACK) {
|
1071 | url = 'https://soundcloud.com/' + vi.channel + '/' + vi.id;
|
1072 | }
|
1073 |
|
1074 | if (vi.mediaType === this.mediaTypes.PLAYLIST) {
|
1075 | url = 'https://soundcloud.com/' + vi.channel + '/sets/' + vi.list;
|
1076 | }
|
1077 |
|
1078 | if (vi.mediaType === this.mediaTypes.APITRACK) {
|
1079 | url = 'https://api.soundcloud.com/tracks/' + vi.id;
|
1080 | }
|
1081 |
|
1082 | if (vi.mediaType === this.mediaTypes.APIPLAYLIST) {
|
1083 | url = 'https://api.soundcloud.com/playlists/' + vi.list;
|
1084 | }
|
1085 |
|
1086 | url += combineParams$9({
|
1087 | params: params
|
1088 | });
|
1089 |
|
1090 | if (startTime) {
|
1091 | url += '#t=' + startTime;
|
1092 | }
|
1093 |
|
1094 | return url;
|
1095 | };
|
1096 |
|
1097 | SoundCloud.prototype.createEmbedUrl = function (vi, params) {
|
1098 | var url = 'https://w.soundcloud.com/player/';
|
1099 | delete params.start;
|
1100 |
|
1101 | if (vi.mediaType === this.mediaTypes.APITRACK) {
|
1102 | params.url = 'https%3A//api.soundcloud.com/tracks/' + vi.id;
|
1103 | }
|
1104 |
|
1105 | if (vi.mediaType === this.mediaTypes.APIPLAYLIST) {
|
1106 | params.url = 'https%3A//api.soundcloud.com/playlists/' + vi.list;
|
1107 | }
|
1108 |
|
1109 | url += combineParams$9({
|
1110 | params: params
|
1111 | });
|
1112 | return url;
|
1113 | };
|
1114 |
|
1115 | base.bind(new SoundCloud());
|
1116 |
|
1117 | var lib = base;
|
1118 |
|
1119 | return lib;
|
1120 |
|
1121 | })));
|