FIX: audioboo.lua: multiple patterns
[libquvi-scripts.git] / share / lua / website / cbsnews.lua
blobbad66e33012e643d64e9dfb37b66fd95a65b0a15
2 -- libquvi-scripts
3 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This library is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU Lesser General Public
9 -- License as published by the Free Software Foundation; either
10 -- version 2.1 of the License, or (at your option) any later version.
12 -- This library is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 -- Lesser General Public License for more details.
17 -- You should have received a copy of the GNU Lesser General Public
18 -- License along with this library; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 -- 02110-1301 USA
23 local CBSNews = {} -- Utility functions unique to this script
25 -- Identify the script.
26 function ident(self)
27 package.path = self.script_dir .. '/?.lua'
28 local C = require 'quvi/const'
29 local r = {}
30 r.domain = "cbsnews%.com"
31 r.formats = "default|best"
32 r.categories = C.proto_http
33 local U = require 'quvi/util'
34 r.handles = U.handles(self.page_url, {r.domain}, {"/video/watch/"})
35 return r
36 end
38 -- Query available formats.
39 function query_formats(self)
40 local U = require 'quvi/util'
41 local config = CBSNews.get_config(self)
42 local formats = CBSNews.iter_formats(config)
44 local t = {}
45 for k,v in pairs(formats) do
46 table.insert(t, CBSNews.to_s(v))
47 end
49 table.sort(t)
50 self.formats = table.concat(t, "|")
52 return self
53 end
55 -- Parse media URL.
56 function parse(self)
57 self.host_id = "cbsnews"
59 local c = CBSNews.get_config(self)
61 self.title = c:match('<Title>.-CDATA%[(.-)%]')
62 or error ("no match: media title")
64 local formats = CBSNews.iter_formats(c)
65 local U = require 'quvi/util'
66 local format = U.choose_format(self, formats,
67 CBSNews.choose_best,
68 CBSNews.choose_default,
69 CBSNews.to_s)
70 or error("unable to choose format")
71 self.url = {format.url or error("no match: media url")}
72 return self
73 end
76 -- Utility functions
79 function CBSNews.get_config(self)
80 local p = quvi.fetch(self.page_url)
82 -- Need "? because some videos have the " and some don't
83 self.id = p:match('CBSVideo.setVideoId%("?(.-)"?%);')
84 or error("no match: media id")
86 local s_fmt =
87 "http://api.cnet.com/restApi/v1.0/videoSearch?videoIds=%s"
88 .. "&iod=videoMedia"
90 local c_url = string.format(s_fmt, self.id)
92 return quvi.fetch(c_url, {fetch_type='config'})
93 end
95 function CBSNews.iter_formats(config) -- Iterate available formats
96 local p = '<Width>(%d+)<'
97 .. '.-<Height>(%d+)<'
98 .. '.-<BitRate>(%d+)<'
99 .. '.-<DeliveryUrl>.-'
100 .. 'CDATA%[(.-)%]'
101 local t = {}
102 for w,h,b,u in config:gmatch(p) do
103 local s = u:match('%.(%w+)$')
104 -- print(w,h,b,s,u)
105 table.insert(t,
106 {width=tonumber(w),
107 height=tonumber(h),
108 bitrate=tonumber(b),
109 url=u,
110 container=s})
112 return t
115 function CBSNews.choose_best(formats) -- Highest quality available
116 local r = {width=0, height=0, bitrate=0, url=nil}
117 local U = require 'quvi/util'
118 for _,v in pairs(formats) do
119 if U.is_higher_quality(v,r) then
120 r = v
123 -- for k,v in pairs(r) do print(k,v) end
124 return r
127 function CBSNews.choose_default(t) -- Lowest quality available
128 local r = {width=0xffff, height=0xffff, bitrate=0xffff, url=nil}
129 local U = require 'quvi/util'
130 for _,v in pairs(t) do
131 if U.is_lower_quality(v,r) then
132 r = v
135 -- for k,v in pairs(r) do print(k,v) end
136 return r
139 function CBSNews.to_s(t)
140 return string.format("%s_%sk_%sp", t.container, t.bitrate, t.height)
143 -- vim: set ts=4 sw=4 tw=72 expandtab: