From d70dfe461a4d3eb3dc9225cd201edb73fe302fe3 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Sun, 13 Jan 2013 11:59:16 +0200 Subject: [PATCH] quvi/youtube: Add functions: ident, can_parse_url Instead of duplicating the code used by different YouTube scripts, add them to the 'quvi/youtube' module so that they may be imported and reused. Signed-off-by: Toni Gundogdu --- share/common/quvi/youtube.lua | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/share/common/quvi/youtube.lua b/share/common/quvi/youtube.lua index 59ce055..db21ee3 100644 --- a/share/common/quvi/youtube.lua +++ b/share/common/quvi/youtube.lua @@ -1,5 +1,5 @@ -- libquvi-scripts --- Copyright (C) 2012 Toni Gundogdu +-- Copyright (C) 2012-2013 Toni Gundogdu -- -- This file is part of libquvi-scripts . -- @@ -21,6 +21,42 @@ local M = {} --[[ +Return the `ident' data for the {media,subtitle} scripts. +Parameters: + qargs .. quvi args +Returns: + A table containing the values expected by the library. +]]-- +function M.ident(qargs) + local u = M.normalize(qargs.input_url) + return { + domains = table.concat({'youtube.com'}, ','), + can_parse_url = M.can_parse_url(u) + } +end + +--[[ +Check if script can parse the URL. +Parameters: + url .. URL to check +Returns: + A boolean value. +]]-- +function M.can_parse_url(url) + local U = require 'socket.url' + local t = U.parse(url) + if t and t.scheme and t.scheme:lower():match('^https?$') + and t.host and t.host:lower():match('youtube%.com$') + and t.query and t.query:lower():match('^v=[%w-_]+') + and t.path and t.path:lower():match('^/watch') + then + return true + else + return false + end +end + +--[[ "Normalize" URL to YouTube media URL. See the test URLs for examples. Parameters: s .. URL to normalize -- 2.11.4.GIT