-
-
Save viking2917/55fdc27fc2b06480cc9a5b641afb11d9 to your computer and use it in GitHub Desktop.
MediaFormat - A regex system for finding the media ID for each type of popular social site. Can identify YouTube, Vimeo, Spotify, and Soundcloud.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* mediaId | |
* format and return only needed pieces of media from their public sources | |
* Author: Trevor Clarke, minor changes from Mark Watkins | |
*/ | |
function mediaId (){ | |
// http://www.youtube.com/embed/m5yCOSHeYn4 | |
var ytRegEx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/; | |
// 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 "http://vimeo.com/" + ID; | |
}, | |
// returns only the ID | |
getSpotifyID: function ( src ){ | |
return getIDfromRegEx( src, spRegEx); | |
}, | |
// returns main link | |
getSpotifyUrl: function ( ID ){ | |
return "http://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 | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment