2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 -- This program is free software: you can redistribute it and/or
7 -- modify it under the terms of the GNU Affero General Public
8 -- License as published by the Free Software Foundation, either
9 -- version 3 of the License, or (at your option) any later version.
11 -- This program is distributed in the hope that it will be useful,
12 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
13 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 -- GNU Affero General Public License for more details.
16 -- You should have received a copy of the GNU Affero General
17 -- Public License along with this program. If not, see
18 -- <http://www.gnu.org/licenses/>.
21 local CBSNews
= {} -- Utility functions unique to this script
23 -- Identify the script.
26 can_parse_url
= CBSNews
.can_parse_url(qargs
),
27 domains
= table.concat({'cbsnews.com'}, ',')
31 -- Parse media properties.
33 local c
= CBSNews
.get_data(qargs
)
35 local L
= require
'quvi/lxph'
36 local U
= require
'quvi/util'
37 local P
= require
'lxp.lom'
41 local v
= CBSNews
.parse_optional(qargs
, x
, U
, L
)
43 qargs
.streams
= CBSNews
.iter_streams(U
, L
, v
)
52 function CBSNews
.can_parse_url(qargs
)
53 local U
= require
'socket.url'
54 local t
= U
.parse(qargs
.input_url
)
55 if t
and t
.scheme
and t
.scheme
:lower():match('^http$')
56 and t
.host
and t
.host
:lower():match('cbsnews%.com$')
57 and t
.path
and t
.path
:lower():match('^/video/watch/')
58 and t
.query
and t
.query
:lower():match('^id=%w+$')
66 -- Queries the video data from the server.
67 function CBSNews
.get_data(qargs
)
68 local p
= quvi
.http
.fetch(qargs
.input_url
).data
70 -- Make mandatory for the reason we need it to fetch the config.
71 qargs
.id
= qargs
.input_url
:match('id=(%d+)') or error('no match: media ID')
73 local s
= "http://api.cnet.com/restApi/v1.0/videoSearch?videoIds=%s"
76 return quvi
.http
.fetch(string.format(s
, qargs
.id
)).data
79 -- Parse optional properties, e.g. title.
80 function CBSNews
.parse_optional(qargs
, x
, U
, L
)
81 local v
= L
.find_first_tag(L
.find_first_tag(x
, 'Videos'), 'Video')
83 qargs
.title
= U
.trim(L
.find_first_tag(v
, 'Title')[1]) or ''
85 local t
= L
.find_first_tag(v
, 'ThumbnailImage')
86 qargs
.thumb_url
= U
.trim(L
.find_first_tag(t
, 'ImageURL')[1]) or ''
91 -- Iterates the available streams.
92 function CBSNews
.iter_streams(U
, L
, v
)
93 local m
= L
.find_first_tag(v
, 'VideoMedias')
94 local S
= require
'quvi/stream'
98 if m
[i
].tag == 'VideoMedia' then
99 local u
= U
.trim(L
.find_first_tag(m
[i
], 'DeliveryUrl')[1])
100 local t
= S
.stream_new(u
)
101 t
.video
.bitrate_kbit_s
= tonumber(L
.find_first_tag(m
[i
], 'BitRate')[1])
102 t
.video
.height
= tonumber(L
.find_first_tag(m
[i
], 'Height')[1])
103 t
.video
.width
= tonumber(L
.find_first_tag(m
[i
], 'Width')[1])
104 t
.video
.encoding
= ''
105 t
.container
= u
:match('%.(%w+)$') or ''
106 t
.id
= CBSNews
.to_id(t
)
112 CBSNews
.ch_best(S
, r
) -- Pick a stream that of the 'best' quality.
118 -- Return an ID for a stream.
119 function CBSNews
.to_id(t
)
120 return string.format("%s_%dk_%dp", t
.container
,
121 t
.video
.bitrate_kbit_s
,
125 -- Picks the stream with the highest video height property.
126 function CBSNews
.ch_best(S
, t
)
127 local r
= t
[1] -- Set the first stream as the default 'best'.
129 for _
,v
in pairs(t
) do
130 if v
.video
.height
> r
.video
.height
then
131 r
= S
.swap_best(r
, v
)
136 -- vim: set ts=2 sw=2 tw=72 expandtab: