3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
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
23 local Foxnews
= {} -- Utility functions unique to this script.
25 -- Identify the script.
27 package
.path
= self
.script_dir
.. '/?.lua'
28 local C
= require
'quvi/const'
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"},
37 or U
.handles(self
.page_url
, {r
.domain
},
38 {"/v/video%-embed%.html"}, {"video_id=%d+"})
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
)
49 for _
,v
in pairs(formats
) do
50 table.insert(t
, Foxnews
.to_s(v
))
54 self
.formats
= table.concat(t
, "|")
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
83 self
.thumbnail_url
= best_thumb_url
86 local formats
= Foxnews
.iter_formats_js(item
, U
)
87 local format = U
.choose_format(self
, formats
,
89 Foxnews
.choose_default
,
91 or error("unable to choose format")
92 self
.url
= {format.url
or error("no match: media url")}
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
112 self
.page_url
:gsub("/v/video%-embed%.html%?video_id=(%d+)",
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"')
123 error("no match: media-content")
127 for attrs
in formats
:gmatch('"@attributes":(%b{})') do
129 for key
,value
in attrs
:gmatch('"(.-)":"(.-)"') do
132 if format.rel
== "stream" then
133 local a
= {"framerate","bitrate","height","duration","width"}
134 for _
, key
in pairs(a
) do
136 format[key
]=tonumber(format[key
])
139 local s
= format.url
:match('FNC_([%w%p]-)%.')
141 format.description
=string.lower(s
)
143 table.insert(t
, format)
150 function Foxnews
.iter_thumbnails_js(page
, U
)
151 local thumbs
= Foxnews
.find_set(page
, '"media%-thumbnail"')
157 for attrs
in thumbs
:gmatch('"@attributes":(%b{})') do
159 for key
,value
in attrs
:gmatch('"(.-)":"(.-)"') do
162 table.insert(t
, thumb
)
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
176 -- for k,v in pairs(r) do print(k,v) end
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
188 -- for k,v in pairs(r) do print(k,v) end
192 function Foxnews
.to_s(t
)
193 if t
.description
then
196 return string.format("%s_%sp", t
.width
, t
.height
)
200 function Foxnews
.find_set(str
, label
)
201 local _
, _
, r
= str
:find(label
.. ':(%b[])')
203 _
, _
, r
= str
:find(label
.. ':(%b{})')
208 -- Local Variables: **
209 -- indent-tabs-mode: () **
210 -- lua-indent-level: 4 **
212 -- vim: set ts=4 sw=4 tw=72 expandtab: