FIX: media/cbsnews.lua: Adapt to website changes
[libquvi-scripts.git] / share / media / cbsnews.lua
blob3c37d07945926700c529a5c55bd04c8c99f16943
1 -- libquvi-scripts
2 -- Copyright (C) 2010-2013 Toni Gundogdu <legatvs@gmail.com>
3 --
4 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
5 --
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.
24 function ident(qargs)
25 return {
26 can_parse_url = CBSNews.can_parse_url(qargs),
27 domains = table.concat({'cbsnews.com'}, ',')
29 end
31 -- Parse media properties.
32 function parse(qargs)
33 local p = quvi.http.fetch(qargs.input_url).data
35 local d = p:match("data%-cbsvideoui%-options='(.-)'")
36 or error('no match: player options')
38 local J = require 'json'
39 local v = J.decode(d).state.video
41 qargs.duration_ms = (v.duration or 0) *1000
43 qargs.streams = CBSNews.iter_streams(v)
45 qargs.thumb_url = v.image.path or ''
47 qargs.title = v.title or ''
49 qargs.id = v.id or ''
51 return qargs
52 end
55 -- Utility functions
58 function CBSNews.can_parse_url(qargs)
59 local U = require 'socket.url'
60 local t = U.parse(qargs.input_url)
61 if t and t.scheme and t.scheme:lower():match('^http$')
62 and t.host and t.host:lower():match('^www%.cbsnews%.com$')
63 and t.path and t.path:lower():match('^/videos/.-/?$')
64 then
65 return true
66 else
67 return false
68 end
69 end
71 function CBSNews.iter_streams(v)
72 local S = require 'quvi/stream'
74 local m = v.medias
75 local r = {}
77 for k,_ in pairs(m) do
78 local a = m[k]
79 if type(a) == 'table' then
80 local t = S.stream_new(a.uri)
81 CBSNews.optional_stream_props(t, a)
82 t.id = CBSNews.to_id(t, k)
83 -- For lack of a better solution:
84 -- Set the 'desktop' stream as the 'best'
85 if k == 'desktop' then
86 t.flags.best = true
87 end
88 table.insert(r, t)
89 end
90 end
91 return r
92 end
94 function CBSNews.optional_stream_props(t, a)
95 t.container = t.url:match('%.(%w+)$') or ''
96 local b = tonumber(a.bitrate or 0)
97 if b >1000 then
98 b = b/1000
99 end
100 t.video.bitrate_kbit_s = b
103 function CBSNews.to_id(t, k)
104 return string.format('%s_%s_%dk', t.container, k, t.video.bitrate_kbit_s)
107 -- vim: set ts=2 sw=2 tw=72 expandtab: