forked from Butonix/ruqqus-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmedia-format.js
55 lines (53 loc) · 2.8 KB
/
media-format.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
/**
* MediaFormat
* format and return only needed pieces of media from their public sources
* Author: Trevor Clarke
*/
export default function MediaFormat() {
// http://www.youtube.com/embed/m5yCOSHeYn4
var ytRegEx = /^(?:https?:\/\/)?(?:i\.|www\.|img\.)?(?:youtu\.be\/|youtube\.com\/|ytimg\.com\/)(?:embed\/|v\/|vi\/|vi_webp\/|watch\?v=|watch\?.+&v=)((\w|-){11})(?:\S+)?$/;
// http://vimeo.com/3116167, https://player.vimeo.com/video/50489180, http://vimeo.com/channels/3116167, http://vimeo.com/channels/staffpicks/113544877
var vmRegEx = /https?:\/\/(?:vimeo\.com\/|player\.vimeo\.com\/)(?:video\/|(?:channels\/staffpicks\/|channels\/)|)((\w|-){7,9})/;
// http://open.spotify.com/track/06TYfe9lyGQA6lfqo5szIi, https://embed.spotify.com/?uri=spotify:track:78z8O6X1dESVSwUPAAPdme
var spRegEx = /https?:\/\/(?:embed\.|open\.)(?:spotify\.com\/)(?:track\/|\?uri=spotify:track:)((\w|-){22})/;
// https://soundcloud.com/aviciiofficial/avicii-you-make-me-diplo-remix, https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/29395900&color=ff5500&auto_play=false&hide_related=false&show_comments=true&show_user=true&show_reposts=false
//var scRegEx = /https?:\/\/(?:w\.|www\.|)(?:soundcloud\.com\/)(?:(?:player\/\?url=https\%3A\/\/api.soundcloud.com\/tracks\/)|)(((\w|-)[^A-z]{7})|([A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*(?!\/sets(?:\/|$))(?:\/[A-Za-z0-9]+(?:[-_][A-Za-z0-9]+)*){1,2}))/;
function getIDfromRegEx ( src, regExpy ){
return (src.match(regExpy)) ? RegExp.$1 : null;
}
return {
// returns only the ID
getYoutubeID: function ( src ){
return getIDfromRegEx( src, ytRegEx);
},
// returns main link
getYoutubeUrl: function ( ID ){
return "https://www.youtube.com/watch?v=" + ID;
},
// returns only the ID
getVimeoID: function ( src ){
return getIDfromRegEx( src, vmRegEx);
},
// returns main link
getVimeoUrl: function ( ID ){
return "https://vimeo.com/" + ID;
},
// returns only the ID
getSpotifyID: function ( src ){
return getIDfromRegEx( src, spRegEx);
},
// returns main link
getSpotifyUrl: function ( ID ){
return "https://open.spotify.com/track/" + ID;
},
// // returns only the ID
// getSoundcloudID: function ( src ){
// return getIDfromRegEx( src, scRegEx);
// },
// // returns main link
// // NOTE: this one really sucks since soundcloud doesnt have good API without js library
// getSoundcloudUrl: function ( ID ){
// return "https://w.soundcloud.com/player/?url=https%3A//api.soundcloud.com/tracks/" + ID; //only way to link to the track currently
// }
};
}