Video Url Parser

urlParser to get extract information like provider, videoId and other from urls

当前为 2014-06-27 提交的版本,查看 最新版本

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/2859/8038/Video%20Url%20Parser.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           Video Url Parser
// @namespace      Zod-
// @description    urlParser to get extract information like provider, videoId and other from urls
// @source         https://github.com/Zod-/jsVideoUrlParser
// @copyright      Julian Hangstörfer
// @version        0.1
// @license        GPL 3
// ==/UserScript==

var urlParser = (function () {
    "use strict";
    var plugins = {};

    return {
        'parse': function (url) {
            var match = url.match(/(https?:\/\/)?([^\.]+\.)?(\w+)\./i),
                provider = match ? match[3] : undefined,
                result,
                createdUrl;
            if (match && provider && plugins[provider]) {
                result = plugins[provider].parse.call(this, url);
                if (result) {
                    result.provider = plugins[provider].provider;
                    return result;
                }
            }
            return undefined;
        },
        'bind': function (plugin) {
            plugins[plugin.provider] = plugin;
            if (plugin.alternatives) {
                var i;
                for (i = 0; i < plugin.alternatives.length; i += 1) {
                    plugins[plugin.alternatives[i]] = plugin;
                }
            }
        },
        'create': function (videoInfo) {
            if (plugins[videoInfo.provider].create) {
                return plugins[videoInfo.provider].create.call(this, videoInfo);
            }
            return undefined;
        }
    };
}());
//parses strings like 1h30m20s to seconds
function getTime(timestring) {
    "use strict";
    var totalSeconds = 0,
        currentValue = 0,
        i,
        timeValues = {
            's': 1,
            'm': 1 * 60,
            'h': 1 * 60 * 60,
            'd': 1 * 60 * 60 * 24,
            'w': 1 * 60 * 60 * 24 * 7
        };

    //is the format 1h30m20s etc
    if (!timestring.match(/^(\d+[smhdw]?)+$/)) {
        return 0;
    }

    for (i = 0; i < timestring.length; i += 1) {
        if (timestring[i] >= '0' && timestring[i] <= '9') {
            //parse the string to decimal
            currentValue = 10 * currentValue + parseInt(timestring[i], 10);
        } else if (timestring[i] in timeValues) {
            //convert to seconds and delete the entry so there can only be one of this element e.g no 20s20s
            totalSeconds += timeValues[timestring[i]] * currentValue;
            delete timeValues[timestring[i]];
            currentValue = 0;
        } else {
            //discard the value if the format doesn't fit the others
            currentValue = 0;
        }
    }
    //if the last string was just numbers and the s tag hasn't been used before then add it as seconds
    if (currentValue !== 0 && 's' in timeValues) {
        totalSeconds += currentValue;
        delete timeValues.s;
    }
    return totalSeconds;
}
//http://joquery.com/2012/string-format-for-javascript
if (typeof String.prototype.format !== 'function') {
    String.prototype.format = function () {
        // The string containing the format items (e.g. "{0}")
        // will and always has to be the first argument.
        var theString = this,
            i,
            regEx;

        // start with the second argument (i = 1)
        for (i = 0; i < arguments.length; i += 1) {
            // "gm" = RegEx options for Global search (more than one instance)
            // and for Multiline search
            regEx = new RegExp("\\{" + (i) + "\\}", "gm");
            theString = theString.replace(regEx, arguments[i]);
        }
        return theString;
    };
}
urlParser.bind({
    'provider': 'dailymotion',
    'alternatives': ['dai'],
    'parse': function (url) {
        "use strict";
        var match,
            id,
            startTime,
            result = {};

        match = url.match(/((\/video)|(ly))\/([A-Za-z0-9]+)/i);
        id = match ? match[4] : undefined;

        match = url.match(/[#\?&]start=([A-Za-z0-9]+)/i);
        startTime = match ? getTime(match[1]) : undefined;

        if (!id) {
            return undefined;
        }
        result.mediaType = 'video';
        result.id = id;
        if (startTime) {
            result.startTime = startTime;
        }
        return result;
    },
    'create': function (videoInfo) {
        "use strict";
        if (videoInfo.startTime) {
            return 'http://www.dailymotion.com/video/{0}?start={1}'.format(videoInfo.id, videoInfo.startTime);
        }

        return 'http://dai.ly/{0}'.format(videoInfo.id);
    }
});
//not finished
urlParser.bind({
    'provider': 'livestream',
    'parse': function (url) {
        "use strict";
        var match,
            channel;
        match = url.match(/livestream\.com\/(\w+)/i);
        channel = match ? match[1] : undefined;
        if (!channel) {
            return undefined;
        }

        return {
            'mediaType': 'stream',
            'channel': channel
        };
    }
});
urlParser.bind({
    'provider': 'twitch',
    'parse': function (url) {
        "use strict";
        var match,
            id,
            channel,
            videoIdPrefix,
            result = {};

        match = url.match(/twitch\.tv\/(\w+)(\/(.)\/(\d+))?/i);
        channel = match ? match[1] : undefined;
        videoIdPrefix = match ? match[3] : undefined;
        id = match ? match[4] : undefined;

        match = url.match(/((\?channel)|(\&utm_content))=(\w+)/i);
        channel = match ? match[4] : channel;

        if (!channel) {
            return undefined;
        }
        if (id) {
            result.mediaType = 'video';
            result.id = id;
            result.videoIdPrefix = videoIdPrefix;
        } else {
            result.mediaType = 'stream';
        }
        result.channel = channel;

        return result;
    },
    'create': function (videoInfo) {
        "use strict";
        var url;
        if (videoInfo.mediaType === 'stream') {
            url = 'http://twitch.tv/{0}'.format(videoInfo.channel);
        } else if (videoInfo.mediaType === 'video') {
            url = 'http://twitch.tv/{0}/{1}/{2}'.format(videoInfo.channel, videoInfo.videoIdPrefix, videoInfo.id);
        }
        return url;
    }
});
urlParser.bind({
    'provider': 'vimeo',
    'alternatives': ['vimeopro'],
    'parse': function (url) {
        "use strict";
        var match,
            id;
        match = url.match(/(\/((channels\/[\w]+)|((album\/\d+\/)?videos?)))?\/(\d+)/i);
        id = match ? match[6] : undefined;
        if (!id) {
            return undefined;
        }
        return {
            'mediaType': 'video',
            'id': id
        };
    },
    'create': function (videoInfo) {
        "use strict";
        return 'http://vimeo.com/{0}'.format(videoInfo.id);
    }
});
urlParser.bind({
    'provider': 'youtube',
    'alternatives': ['youtu'],
    'parse': function (url) {
        "use strict";
        var match,
            id,
            playlistId,
            startTime,
            result = {};

        match = url.match(/(((v|be|videos)\/)|(v=))([\w\-]{11})/i);
        id = match ? match[5] : undefined;

        match = url.match(/list=([\w\-]+)/i);
        playlistId = match ? match[1] : undefined;

        match = url.match(/[#\?&](star)?t=([A-Za-z0-9]+)/i);
        startTime = match ? getTime(match[2]) : undefined;


        if (id) {
            result.mediaType = 'video';
            result.id = id;
            if (playlistId) {
                result.playlistId = playlistId;
            }
            if (startTime) {
                result.startTime = startTime;
            }
        } else if (playlistId) {
            result.mediaType = 'playlist';
            result.playlistId = playlistId;
        } else {
            return undefined;
        }

        return result;
    },
    'create': function (videoInfo) {
        "use strict";
        var url;
        if (videoInfo.mediaType === 'video') {
            if (!videoInfo.playlistId) {
                url = 'http://youtu.be/{0}'.format(videoInfo.id);
            } else {
                url = 'https://www.youtube.com/watch?v={0}&list={1}'.format(videoInfo.id, videoInfo.playlistId);
            }
            if (videoInfo.startTime) {
                url += '#t={0}'.format(videoInfo.startTime);
            }
        } else if (videoInfo.mediaType === 'playlist') {
            url = 'https://www.youtube.com/playlist?feature=share&list={0}'.format(videoInfo.playlistId);
        }
        return url;
    }
});