Merge branch 'tg/add-videobash'
[quvi.git] / share / lua / website / foxnews.lua
blobca394c9f24b93b4cc74e32b813471b944aff40b0
2 -- quvi
3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
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 Foxnews = {} -- 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 = "video.foxnews.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}, {"/v/%d+"})
35 or U.handles(self.page_url, {r.domain}, {"/v/embed%.js"},
36 {"id%=%d+"})
37 or U.handles(self.page_url, {r.domain},
38 {"/v/video%-embed%.html"}, {"video_id=%d+"})
39 return r
40 end
42 -- Query available formats.
43 function query_formats(self)
44 local U = require 'quvi/util'
45 local js = Foxnews.fetch_feed_js(self, U)
46 local formats = Foxnews.iter_formats_js(js, U)
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Foxnews.to_s(v))
51 end
53 table.sort(t)
54 self.formats = table.concat(t, "|")
56 return self
57 end
59 -- Parse media URL.
60 function parse(self)
61 self.host_id = "foxnews"
63 local U = require 'quvi/util'
64 local js = Foxnews.fetch_feed_js(self, U)
66 local _,_,item = js:find('"item":(%b{})')
67 if not item then
68 error("no match: item")
69 end
71 local _,_,s = js:find('"title":"(.-)"')
72 self.title = s or error("no match: media title")
75 local thumbs = Foxnews.iter_thumbnails_js(item)
76 local best_thumb_res = -1
77 local best_thumb_url = ''
78 for _,thumb in pairs(thumbs) do
79 local res = thumb.height*thumb.width
80 if res > best_thumb_res then
81 best_thumb_url = thumb.url
82 best_thumb_res = res
83 end
84 end
85 self.thumbnail_url = best_thumb_url
86 end
88 local formats = Foxnews.iter_formats_js(item, U)
89 self.url = {U.choose_format(self, formats,
90 Foxnews.choose_best,
91 Foxnews.choose_default,
92 Foxnews.to_s).url
93 or error("no match: media url")}
95 return self
96 end
99 -- Utility functions
102 function Foxnews.fetch_feed_js(self, U)
103 self.page_url = Foxnews.normalize(self)
105 return quvi.fetch('http://video.foxnews.com/v/feed/video/'
106 .. self.id .. '.js?template=grab')
109 function Foxnews.normalize(self) -- "Normalize" embedded URLs
110 if self.page_url:find("/v/embed.js?id=",1,true) then
111 self.page_url = self.page_url:gsub("/v/embed%.js%?id=(%d+)", "/v/%1/")
112 elseif self.page_url:find("/v/video-embed.html?video_id=",1,true) then
113 self.page_url =
114 self.page_url:gsub("/v/video%-embed%.html%?video_id=(%d+)",
115 "/v/%1/")
117 local _, ie, s = self.page_url:find("/v/(%d+)")
118 self.id = s or error("no match: media id")
119 self.page_url = self.page_url:sub(1, ie)
122 function Foxnews.iter_formats_js(page, U)
123 local formats = Foxnews.find_set(page, '"media%-content"')
124 if not formats then
125 error("no match: media-content")
128 local t = {}
129 for attrs in formats:gfind('"@attributes":(%b{})') do
130 local format = {}
131 for key,value in attrs:gfind('"(.-)":"(.-)"') do
132 format[key]=value
134 if format.rel == "stream" then
135 local a = {"framerate","bitrate","height","duration","width"}
136 for _, key in pairs(a) do
137 if format[key] then
138 format[key]=tonumber(format[key])
141 local _,_,s = format.url:find('FNC_([%w%p]-)%.')
142 if s then
143 format.description=string.lower(s)
145 table.insert(t, format)
149 return t
152 function Foxnews.iter_thumbnails_js(page, U)
153 local thumbs = Foxnews.find_set(page, '"media%-thumbnail"')
154 if not thumbs then
155 return {}
158 local t = {}
159 for attrs in thumbs:gfind('"@attributes":(%b{})') do
160 local thumb = {}
161 for key,value in attrs:gfind('"(.-)":"(.-)"') do
162 thumb[key]=value
164 table.insert(t, thumb)
167 return t
170 function Foxnews.choose_default(formats) -- Lowest quality available
171 local r = {width=0xffff, height=0xffff, url=nil, bitrate=0xfffffffffff}
172 local U = require 'quvi/util'
173 for _,v in pairs(formats) do
174 if U.is_lower_quality(v,r) then
175 r = v
178 -- for k,v in pairs(r) do print(k,v) end
179 return r
182 function Foxnews.choose_best(formats) -- Highest quality available
183 local r = {width=0, height=0, url=nil, bitrate=-1}
184 local U = require 'quvi/util'
185 for _,v in pairs(formats) do
186 if U.is_higher_quality(v,r) then
187 r = v
190 -- for k,v in pairs(r) do print(k,v) end
191 return r
194 function Foxnews.to_s(t)
195 if t.description then
196 return t.description
197 else
198 return string.format("%s_%sp", t.width, t.height)
202 function Foxnews.find_set(str, label)
203 local _, _, r = str:find(label .. ':(%b[])')
204 if not r then
205 _, _, r = str:find(label .. ':(%b{})')
207 return r
210 -- Local Variables: **
211 -- indent-tabs-mode: () **
212 -- lua-indent-level: 4 **
213 -- End: **
214 -- vim: set ts=4 sw=4 tw=72 expandtab: