ard.lua: Cleanup Ard.iter_formats
[libquvi-scripts.git] / share / lua / website / foxnews.lua
blobb8e891dc04de2ff3b5ff2a427586a8924df9dcbb
2 -- libquvi-scripts
3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
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.
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:match('"item":(%b{})')
67 or error("no match: item")
69 self.title = js:match('"title":"(.-)"')
70 or error("no match: media title")
73 local thumbs = Foxnews.iter_thumbnails_js(item)
74 local best_thumb_res = -1
75 local best_thumb_url = ''
76 for _,thumb in pairs(thumbs) do
77 local res = thumb.height*thumb.width
78 if res > best_thumb_res then
79 best_thumb_url = thumb.url
80 best_thumb_res = res
81 end
82 end
83 self.thumbnail_url = best_thumb_url
84 end
86 local formats = Foxnews.iter_formats_js(item, U)
87 local format = U.choose_format(self, formats,
88 Foxnews.choose_best,
89 Foxnews.choose_default,
90 Foxnews.to_s)
91 or error("unable to choose format")
92 self.url = {format.url or error("no match: media url")}
93 return self
94 end
97 -- Utility functions
100 function Foxnews.fetch_feed_js(self, U)
101 self.page_url = Foxnews.normalize(self)
103 return quvi.fetch('http://video.foxnews.com/v/feed/video/'
104 .. self.id .. '.js?template=grab')
107 function Foxnews.normalize(self) -- "Normalize" embedded URLs
108 if self.page_url:match("/v/embed.js?id=",1,true) then
109 self.page_url = self.page_url:gsub("/v/embed%.js%?id=(%d+)", "/v/%1/")
110 elseif self.page_url:match("/v/video-embed.html?video_id=",1,true) then
111 self.page_url =
112 self.page_url:gsub("/v/video%-embed%.html%?video_id=(%d+)",
113 "/v/%1/")
115 local _, ie, s = self.page_url:find("/v/(%d+)")
116 self.id = s or error("no match: media id")
117 self.page_url = self.page_url:sub(1, ie)
120 function Foxnews.iter_formats_js(page, U)
121 local formats = Foxnews.find_set(page, '"media%-content"')
122 if not formats then
123 error("no match: media-content")
126 local t = {}
127 for attrs in formats:gmatch('"@attributes":(%b{})') do
128 local format = {}
129 for key,value in attrs:gmatch('"(.-)":"(.-)"') do
130 format[key]=value
132 if format.rel == "stream" then
133 local a = {"framerate","bitrate","height","duration","width"}
134 for _, key in pairs(a) do
135 if format[key] then
136 format[key]=tonumber(format[key])
139 local s = format.url:match('FNC_([%w%p]-)%.')
140 if s then
141 format.description=string.lower(s)
143 table.insert(t, format)
147 return t
150 function Foxnews.iter_thumbnails_js(page, U)
151 local thumbs = Foxnews.find_set(page, '"media%-thumbnail"')
152 if not thumbs then
153 return {}
156 local t = {}
157 for attrs in thumbs:gmatch('"@attributes":(%b{})') do
158 local thumb = {}
159 for key,value in attrs:gmatch('"(.-)":"(.-)"') do
160 thumb[key]=value
162 table.insert(t, thumb)
165 return t
168 function Foxnews.choose_default(formats) -- Lowest quality available
169 local r = {width=0xffff, height=0xffff, url=nil, bitrate=0xfffffffffff}
170 local U = require 'quvi/util'
171 for _,v in pairs(formats) do
172 if U.is_lower_quality(v,r) then
173 r = v
176 -- for k,v in pairs(r) do print(k,v) end
177 return r
180 function Foxnews.choose_best(formats) -- Highest quality available
181 local r = {width=0, height=0, url=nil, bitrate=-1}
182 local U = require 'quvi/util'
183 for _,v in pairs(formats) do
184 if U.is_higher_quality(v,r) then
185 r = v
188 -- for k,v in pairs(r) do print(k,v) end
189 return r
192 function Foxnews.to_s(t)
193 if t.description then
194 return t.description
195 else
196 return string.format("%s_%sp", t.width, t.height)
200 function Foxnews.find_set(str, label)
201 local _, _, r = str:find(label .. ':(%b[])')
202 if not r then
203 _, _, r = str:find(label .. ':(%b{})')
205 return r
208 -- Local Variables: **
209 -- indent-tabs-mode: () **
210 -- lua-indent-level: 4 **
211 -- End: **
212 -- vim: set ts=4 sw=4 tw=72 expandtab: