3 -- Copyright (C) 2010-2011 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"
58 local config
= CBSNews
.get_config(self
)
60 local _
,_
,s
= config
:find ('<Title>.-CDATA%[(.-)%]')
61 self
.title
= s
or error ("no match: media title")
63 local formats
= CBSNews
.iter_formats(config
)
64 local U
= require
'quvi/util'
65 local format = U
.choose_format(self
, formats
,
67 CBSNews
.choose_default
,
69 or error("unable to choose format")
70 self
.url
= {format.url
or error("no match: media url")}
78 function CBSNews
.get_config(self
)
79 local page
= quvi
.fetch(self
.page_url
)
81 -- Need "? because some videos have the " and some don't
82 local _
,_
,s
= page
:find('CBSVideo.setVideoId%("?(.-)"?%);')
83 self
.id
= s
or error("no match: media id")
85 local s_fmt
= "http://api.cnet.com/restApi/v1.0/videoSearch?videoIds=%s"
88 local config_url
= string.format(s_fmt
, self
.id
)
90 return quvi
.fetch(config_url
, {fetch_type
= 'config'})
93 function CBSNews
.iter_formats(config
) -- Iterate available formats
94 local p
= '<Width>(%d+)<'
96 .. '.-<BitRate>(%d+)<'
97 .. '.-<DeliveryUrl>.-'
100 for w
,h
,b
,u
in config
:gfind(p
) do
101 local _
,_
,s
= u
:find('%.(%w+)$')
113 function CBSNews
.choose_best(formats
) -- Highest quality available
114 local r
= {width
=0, height
=0, bitrate
=0, url
=nil}
115 local U
= require
'quvi/util'
116 for _
,v
in pairs(formats
) do
117 if U
.is_higher_quality(v
,r
) then
121 -- for k,v in pairs(r) do print(k,v) end
125 function CBSNews
.choose_default(t
) -- Lowest quality available
126 local r
= {width
=0xffff, height
=0xffff, bitrate
=0xffff, url
=nil}
127 local U
= require
'quvi/util'
128 for _
,v
in pairs(t
) do
129 if U
.is_lower_quality(v
,r
) then
133 -- for k,v in pairs(r) do print(k,v) end
137 function CBSNews
.to_s(t
)
138 return string.format("%s_%sk_%sp", t
.container
, t
.bitrate
, t
.height
)
141 -- vim: set ts=4 sw=4 tw=72 expandtab: