Merge branch 'tg/next/1.0' into next
[libquvi-scripts.git] / share / lua / website / cbsnews.lua
1
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.
11 --
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.
16 --
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
21 --
22
23 local CBSNews = {} -- Utility functions unique to this script
24
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
37
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)
43
44     local t = {}
45     for k,v in pairs(formats) do
46         table.insert(t, CBSNews.to_s(v))
47     end
48
49     table.sort(t)
50     self.formats = table.concat(t, "|")
51
52     return self
53 end
54
55 -- Parse media URL.
56 function parse(self)
57     self.host_id = "cbsnews"
58
59     local c = CBSNews.get_config(self)
60
61     self.title = c:match('<Title>.-CDATA%[(.-)%]')
62                   or error ("no match: media title")
63
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
74
75 --
76 -- Utility functions
77 --
78
79 function CBSNews.get_config(self)
80     local p = quvi.fetch(self.page_url)
81
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")
85
86     local s_fmt =
87       "http://api.cnet.com/restApi/v1.0/videoSearch?videoIds=%s"
88        .. "&iod=videoMedia"
89
90     local c_url = string.format(s_fmt, self.id)
91
92     return quvi.fetch(c_url, {fetch_type='config'})
93 end
94
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})
111     end
112     return t
113 end
114
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
121         end
122     end
123 --    for k,v in pairs(r) do print(k,v) end
124     return r
125 end
126
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
133         end
134     end
135 --    for k,v in pairs(r) do print(k,v) end
136     return r
137 end
138
139 function CBSNews.to_s(t)
140     return string.format("%s_%sk_%sp", t.container, t.bitrate, t.height)
141 end
142
143 -- vim: set ts=4 sw=4 tw=72 expandtab: