Skip to content

Instantly share code, notes, and snippets.

@sinslav
Created March 23, 2016 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sinslav/d2b8037b590a14324c1b to your computer and use it in GitHub Desktop.
Save sinslav/d2b8037b590a14324c1b to your computer and use it in GitHub Desktop.
function processURL(url, success){
var id;
if (url.indexOf('youtube.com') > -1) {
id = url.split('/')[1].split('v=')[1].split('&')[0];
return processYouTube(id);
} else if (url.indexOf('youtu.be') > -1) {
id = url.split('/')[1];
return processYouTube(id);
} else if (url.indexOf('vimeo.com') > -1) {
if (url.match(/^vimeo.com\/[0-9]+/)) {
id = url.split('/')[1];
} else if (url.match(/^vimeo.com\/channels\/[\d\w]+#[0-9]+/)) {
id = url.split('#')[1];
} else if (url.match(/vimeo.com\/groups\/[\d\w]+\/videos\/[0-9]+/)) {
id = url.split('/')[4];
} else {
throw new Error('Unsupported Vimeo URL');
}
$.ajax({
url: 'http://vimeo.com/api/v2/video/' + id + '.json',
dataType: 'jsonp',
success: function(data) {
sucess(data[0].thumbnail_large);
}
});
} else {
throw new Error('Unrecognised URL');
}
function processYouTube(id) {
if (!id) {
throw new Error('Unsupported YouTube URL');
}
sucess('http://i2.ytimg.com/vi/' + id + '/hqdefault.jpg');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment