Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / cbsnews.lua
blob0776aa2d453d9d9720a6ed17e4d7373148548047
2 -- quvi
3 -- Copyright (C) 2010,2011 Toni Gundogdu <legatvs@gmail.com>
4 --
5 -- This file is part of quvi <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.
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
20 -- 02110-1301 USA
23 local CBSNews = {} -- Utility functions unique to this script
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
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)
44 local t = {}
45 for k,v in pairs(formats) do
46 table.insert(t, CBSNews.to_s(v))
47 end
49 table.sort(t)
50 self.formats = table.concat(t, "|")
52 return self
53 end
55 -- Parse media URL.
56 function parse(self)
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 self.url = {U.choose_format(self, formats,
66 CBSNews.choose_best,
67 CBSNews.choose_default,
68 CBSNews.to_s).url
69 or error("no match: media url")}
71 return self
72 end
75 -- Utility functions
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"
86 .. "&iod=videoMedia"
88 local config_url = string.format(s_fmt, self.id)
90 return quvi.fetch(config_url, {fetch_type = 'config'})
91 end
93 function CBSNews.iter_formats(config) -- Iterate available formats
94 local p = '<Width>(%d+)<'
95 .. '.-<Height>(%d+)<'
96 .. '.-<BitRate>(%d+)<'
97 .. '.-<DeliveryUrl>.-'
98 .. 'CDATA%[(.-)%]'
99 local t = {}
100 for w,h,b,u in config:gfind(p) do
101 local _,_,s = u:find('%.(%w+)$')
102 -- print(w,h,b,s,u)
103 table.insert(t,
104 {width=tonumber(w),
105 height=tonumber(h),
106 bitrate=tonumber(b),
107 url=u,
108 container=s})
110 return t
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
118 r = v
121 -- for k,v in pairs(r) do print(k,v) end
122 return r
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
130 r = v
133 -- for k,v in pairs(r) do print(k,v) end
134 return r
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: