3 -- Copyright (C) 2011 Lionel Elie Mamane <lionel@mamane.lu>
5 -- This file is part of quvi <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
:find('"item":(%b{})')
68 error("no match: item")
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
85 self
.thumbnail_url
= best_thumb_url
88 local formats
= Foxnews
.iter_formats_js(item
, U
)
89 self
.url
= {U
.choose_format(self
, formats
,
91 Foxnews
.choose_default
,
93 or error("no match: media url")}
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
114 self
.page_url
:gsub("/v/video%-embed%.html%?video_id=(%d+)",
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"')
125 error("no match: media-content")
129 for attrs
in formats
:gfind('"@attributes":(%b{})') do
131 for key
,value
in attrs
:gfind('"(.-)":"(.-)"') do
134 if format.rel
== "stream" then
135 local a
= {"framerate","bitrate","height","duration","width"}
136 for _
, key
in pairs(a
) do
138 format[key
]=tonumber(format[key
])
141 local _
,_
,s
= format.url
:find('FNC_([%w%p]-)%.')
143 format.description
=string.lower(s
)
145 table.insert(t
, format)
152 function Foxnews
.iter_thumbnails_js(page
, U
)
153 local thumbs
= Foxnews
.find_set(page
, '"media%-thumbnail"')
159 for attrs
in thumbs
:gfind('"@attributes":(%b{})') do
161 for key
,value
in attrs
:gfind('"(.-)":"(.-)"') do
164 table.insert(t
, thumb
)
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
178 -- for k,v in pairs(r) do print(k,v) end
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
190 -- for k,v in pairs(r) do print(k,v) end
194 function Foxnews
.to_s(t
)
195 if t
.description
then
198 return string.format("%s_%sp", t
.width
, t
.height
)
202 function Foxnews
.find_set(str
, label
)
203 local _
, _
, r
= str
:find(label
.. ':(%b[])')
205 _
, _
, r
= str
:find(label
.. ':(%b{})')
210 -- Local Variables: **
211 -- indent-tabs-mode: () **
212 -- lua-indent-level: 4 **
214 -- vim: set ts=4 sw=4 tw=72 expandtab: