Update NEWS for v0.9.20130520
[libquvi-scripts.git] / share / lua / website / mgnetwork.lua
1 --
2 -- libquvi-scripts
3 -- Copyright (C) 2011  quvi project
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
23 --
24 -- NOTE: Media General is a company that owns many local news stations
25 -- and newspapers in the southeast US. They all use the same system for
26 -- videos.
27 --  <http://sourceforge.net/apps/trac/quvi/ticket/78>
28 --
29
30 local MGNetwork = {} -- Utility functions unique to this script
31
32 -- Identify the script.
33 function ident(self)
34     package.path = self.script_dir .. '/?.lua'
35     local C      = require 'quvi/const'
36     local r      = {}
37     local domains= {'alabamas13%.com', 'counton2%.com',
38         'dailyprogress%.com', 'dothaneagle%.com', 'eprisenow%.com',
39         'eufaulatribune%.com', 'godanriver%.com', 'hickoryrecord%.com',
40         'independenttribune%.com', 'insidenova%.com', 'jcfloridan%.com',
41         'journalnow%.com', 'oanow%.com', 'morganton%.com',
42         'nbc4i%.com', 'nbc17%.com', 'newsadvance%.com',
43         'newsvirginian%.com', 'richmond%.com', 'sceneon7%.com',
44         'scnow%.com', 'starexponent%.com', 'statesville%.com',
45         'tbo%.com', 'timesdispatch%.com', 'tricities%.com',
46         'turnto10%.com', 'wnct%.com', 'whlt%.com', 'wjbf%.com',
47         'wjtv%.com', 'wkrg%.com', 'wrbl%.com', 'wsav%.com', 'wsls%.com',
48         'wspa%.com', 'vteffect%.com'}
49     r.domain     = 'mgnetwork%.com'
50     r.formats    = 'default|best'
51     r.categories = C.proto_http
52     local U      = require 'quvi/util'
53     r.handles    = U.handles(self.page_url,
54             domains,
55             {"/%w+/.+%-%d+"})
56     return r
57 end
58
59 -- Query available formats.
60 function query_formats(self)
61     local config  = MGNetwork.get_config(self)
62     local formats = MGNetwork.iter_formats(config)
63
64     local t = {}
65     for _,v in pairs(formats) do
66         table.insert(t, MGNetwork.to_s(v))
67     end
68
69     table.sort(t)
70     self.formats = table.concat(t, "|")
71
72     return self
73 end
74
75 -- Parse media URL.
76 function parse(self)
77     self.host_id = 'mgnetwork'
78
79     local xml = MGNetwork.get_config(self)
80
81     self.title = xml:match('<item>.-<title>(.-)</title>')
82                     or error("no match: media title")
83
84     local formats = MGNetwork.iter_formats(xml)
85     local U       = require 'quvi/util'
86     local format  = U.choose_format(self, formats,
87                                     MGNetwork.choose_best,
88                                     MGNetwork.choose_default,
89                                     MGNetwork.to_s)
90                         or error("unable to choose format")
91     self.url = {format.url or error("no match: media url")}
92
93     -- Optional: we can live without these
94
95     self.thumbnail_url = xml:match('<media:thumbnail url="(.-)"') or ''
96
97     local d = xml:match('duration="(.-)"') or 0
98     self.duration = math.ceil(d) * 1000 -- to msec
99
100     return self
101 end
102
103 --
104 -- Utility functions
105 --
106
107 function MGNetwork.get_config(self)
108     -- local config_url = string.format(
109     --    'http://%s/video/get/media_response_related_' ..
110     --    'for_content/%s/%d/', d, v, self.id)
111     local d,v,s = self.page_url:match("http://([%w%.]+)/.+%-(%w+)%-(%d+)/")
112     self.id = s or error("no match: media id")
113
114     -- Ideally, we'd skip this step and just get the config.
115     -- Do this so that we can check whether the video exists.
116     local d = quvi.fetch(self.page_url)
117     local p = "if %(!mrss_link%){ mrss_link = '(.-)'; }"
118     local s = d:match(p) or error('no match: no video found on page')
119
120     return quvi.fetch(s, {fetch_type = 'config'})
121 end
122
123 function MGNetwork.iter_formats(config)
124     local p = 'content url="(.-)" type="%w+/(.-)"'
125     local t = {}
126     for u,c in config:gmatch(p) do
127         table.insert(t, {url=u, container=c})
128         --print(u,c)
129     end
130     return t
131 end
132
133 function MGNetwork.choose_best(t) -- Expect the first to be the 'best'
134   return t[1]
135 end
136
137 function MGNetwork.choose_default(t) -- Expect the last to be the 'default'
138   return t[#t]
139 end
140
141 function MGNetwork.to_s(t)
142   return t.container
143 end
144
145 -- vim: set ts=4 sw=4 tw=72 expandtab: