3 -- Copyright (C) 2010-2012 Toni Gundogdu <legatvs@gmail.com>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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
23 local CBSNews
= {} -- Utility functions unique to this script
25 -- Identify the script.
27 package
.path
= self
.script_dir
.. '/?.lua'
28 local C
= require
'quvi/const'
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/"})
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
)
45 for k
,v
in pairs(formats
) do
46 table.insert(t
, CBSNews
.to_s(v
))
50 self
.formats
= table.concat(t
, "|")
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
,
68 CBSNews
.choose_default
,
70 or error("unable to choose format")
71 self
.url
= {format.url
or error("no match: media url")}
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")
87 "http://api.cnet.com/restApi/v1.0/videoSearch?videoIds=%s"
90 local c_url
= string.format(s_fmt
, self
.id
)
92 return quvi
.fetch(c_url
, {fetch_type
='config'})
95 function CBSNews
.iter_formats(config
) -- Iterate available formats
96 local p
= '<Width>(%d+)<'
98 .. '.-<BitRate>(%d+)<'
99 .. '.-<DeliveryUrl>.-'
102 for w
,h
,b
,u
in config
:gmatch(p
) do
103 local s
= u
:match('%.(%w+)$')
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
123 -- for k,v in pairs(r) do print(k,v) end
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
135 -- for k,v in pairs(r) do print(k,v) end
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: