Update NEWS for v0.9.20131130
[libquvi-scripts.git] / share / lua / website / foxnews.lua
Commit [+]AuthorDateLineData
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +03001
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.
11--
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.
16--
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
21--
22
23local Foxnews = {} -- Utility functions unique to this script.
24
25-- Identify the script.
26function 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
40end
41
42-- Query available formats.
43function 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)
47
48 local t = {}
49 for _,v in pairs(formats) do
50 table.insert(t, Foxnews.to_s(v))
51 end
52
53 table.sort(t)
54 self.formats = table.concat(t, "|")
55
56 return self
57end
58
59-- Parse media URL.
60function parse(self)
61 self.host_id = "foxnews"
62
ba7bf48b
TG
Toni Gundogdu2012-03-17 23:34:55 +020063 local U = require 'quvi/util'
64 local js = Foxnews.fetch_feed_js(self, U)
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030065
ba7bf48b
TG
Toni Gundogdu2012-03-17 23:34:55 +020066 local item = js:match('"item":(%b{})')
67 or error("no match: item")
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +030068
ba7bf48b
TG
Toni Gundogdu2012-03-17 23:34:55 +020069 self.title = js:match('"title":"(.-)"')
70 or error("no match: media title")
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030071
72 do
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
85
86 local formats = Foxnews.iter_formats_js(item, U)
8a40fa04 Toni Gundogdu2011-11-24 17:38:25 +020087 local format = U.choose_format(self, formats,
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030088 Foxnews.choose_best,
89 Foxnews.choose_default,
8a40fa04
TG
Toni Gundogdu2011-11-24 17:38:25 +020090 Foxnews.to_s)
91 or error("unable to choose format")
92 self.url = {format.url or error("no match: media url")}
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +030093 return self
94end
95
96--
97-- Utility functions
98--
99
100function Foxnews.fetch_feed_js(self, U)
101 self.page_url = Foxnews.normalize(self)
102
103 return quvi.fetch('http://video.foxnews.com/v/feed/video/'
104 .. self.id .. '.js?template=grab')
105end
106
107function Foxnews.normalize(self) -- "Normalize" embedded URLs
ba7bf48b Toni Gundogdu2012-03-17 23:34:55 +0200108 if self.page_url:match("/v/embed.js?id=",1,true) then
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300109 self.page_url = self.page_url:gsub("/v/embed%.js%?id=(%d+)", "/v/%1/")
ba7bf48b Toni Gundogdu2012-03-17 23:34:55 +0200110 elseif self.page_url:match("/v/video-embed.html?video_id=",1,true) then
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300111 self.page_url =
112 self.page_url:gsub("/v/video%-embed%.html%?video_id=(%d+)",
113 "/v/%1/")
114 end
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)
118end
119
120function 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")
124 end
125
126 local t = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +0200127 for attrs in formats:gmatch('"@attributes":(%b{})') do
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300128 local format = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +0200129 for key,value in attrs:gmatch('"(.-)":"(.-)"') do
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300130 format[key]=value
131 end
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])
137 end
138 end
ba7bf48b Toni Gundogdu2012-03-17 23:34:55 +0200139 local s = format.url:match('FNC_([%w%p]-)%.')
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300140 if s then
141 format.description=string.lower(s)
142 end
143 table.insert(t, format)
144 end
145 end
146
147 return t
148end
149
150function Foxnews.iter_thumbnails_js(page, U)
151 local thumbs = Foxnews.find_set(page, '"media%-thumbnail"')
152 if not thumbs then
153 return {}
154 end
155
156 local t = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +0200157 for attrs in thumbs:gmatch('"@attributes":(%b{})') do
62cc25f1 Toni Gundogdu2011-09-19 19:39:08 +0300158 local thumb = {}
b5ae4e17 Toni Gundogdu2012-03-18 12:42:45 +0200159 for key,value in attrs:gmatch('"(.-)":"(.-)"') do
62cc25f1
TG
Toni Gundogdu2011-09-19 19:39:08 +0300160 thumb[key]=value
161 end
162 table.insert(t, thumb)
163 end
164
165 return t
166end
167
168function 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
174 end
175 end
176-- for k,v in pairs(r) do print(k,v) end
177 return r
178end
179
180function 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
186 end
187 end
188-- for k,v in pairs(r) do print(k,v) end
189 return r
190end
191
192function 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)
197 end
198end
199
200function Foxnews.find_set(str, label)
201 local _, _, r = str:find(label .. ':(%b[])')
202 if not r then
203 _, _, r = str:find(label .. ':(%b{})')
204 end
205 return r
206end
207
208-- Local Variables: **
209-- indent-tabs-mode: () **
210-- lua-indent-level: 4 **
211-- End: **
212-- vim: set ts=4 sw=4 tw=72 expandtab: