Improve URL parsing

This commit is contained in:
calzoneman 2014-08-13 13:49:32 -05:00
parent fae1609a50
commit 735b2fcd07
1 changed files with 27 additions and 17 deletions

View File

@ -1171,6 +1171,16 @@ function playlistMove(from, after, cb) {
} }
} }
function extractQueryParam(query, param) {
var params = {};
query.split("&").forEach(function (kv) {
kv = kv.split("=");
params[kv[0]] = kv[1];
});
return params[param];
}
function parseMediaLink(url) { function parseMediaLink(url) {
if(typeof url != "string") { if(typeof url != "string") {
return { return {
@ -1196,28 +1206,28 @@ function parseMediaLink(url) {
} }
var m; var m;
if((m = url.match(/youtube\.com\/watch\?v=([^&#]+)/))) { if((m = url.match(/youtube\.com\/watch\?([^#]+)/))) {
return {
id: extractQueryParam(m[1], "v"),
type: "yt"
};
}
if((m = url.match(/youtu\.be\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "yt" type: "yt"
}; };
} }
if((m = url.match(/youtu\.be\/([^&#]+)/))) { if((m = url.match(/youtube\.com\/playlist\?([^#]+)/))) {
return { return {
id: m[1], id: extractQueryParam(m[1], "list"),
type: "yt"
};
}
if((m = url.match(/youtube\.com\/playlist\?list=([^&#]+)/))) {
return {
id: m[1],
type: "yp" type: "yp"
}; };
} }
if((m = url.match(/twitch\.tv\/([^&#]+)/))) { if((m = url.match(/twitch\.tv\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "tw" type: "tw"
@ -1231,42 +1241,42 @@ function parseMediaLink(url) {
}; };
} }
if((m = url.match(/livestream\.com\/([^&#]+)/))) { if((m = url.match(/livestream\.com\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "li" type: "li"
}; };
} }
if((m = url.match(/ustream\.tv\/([^&#]+)/))) { if((m = url.match(/ustream\.tv\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "us" type: "us"
}; };
} }
if((m = url.match(/vimeo\.com\/([^&#]+)/))) { if((m = url.match(/vimeo\.com\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "vi" type: "vi"
}; };
} }
if((m = url.match(/dailymotion\.com\/video\/([^&#]+)/))) { if((m = url.match(/dailymotion\.com\/video\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "dm" type: "dm"
}; };
} }
if((m = url.match(/imgur\.com\/a\/([^&#]+)/))) { if((m = url.match(/imgur\.com\/a\/([^\?&#]+)/))) {
return { return {
id: m[1], id: m[1],
type: "im" type: "im"
}; };
} }
if((m = url.match(/soundcloud\.com\/([^&#]+)/))) { if((m = url.match(/soundcloud\.com\/([^\?&#]+)/))) {
return { return {
id: url, id: url,
type: "sc" type: "sc"