media/lego.lua: Rewrite parse function
[libquvi-scripts.git] / share / media / lego.lua
blobb4666f341e7343c8555ec62a639428790e709d56
1 -- libquvi-scripts
2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2012 Ross Burton <ross@burtonini.com>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program 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
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local Lego = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = Lego.can_parse_url(qargs),
28 domains = table.concat({'city.lego.com'}, ',')
30 end
32 -- Parse media properties.
33 function parse(qargs)
34 local p = quvi.http.fetch(qargs.input_url).data
36 local d = p:match('FirstVideoData = (.-);')
37 or error('no match: FirstVideoData')
39 local J = require 'json'
40 local j = J.decode(d)
42 qargs.id = j['LikeObjectGuid'] or '' -- Lack of a better one.
44 qargs.title = j['Name'] or ''
46 Lego.parse_thumb_url(qargs, p)
48 qargs.streams = Lego.iter_streams(j)
50 return qargs
51 end
54 -- Utility functions.
57 function Lego.can_parse_url(qargs)
58 local U = require 'socket.url'
59 local t = U.parse(qargs.input_url)
60 if t and t.scheme and t.scheme:lower():match('^http$')
61 and t.host and t.host:lower():match('^city%.lego%.com$')
62 and t.path and t.path:lower():match('/%w+%-%w+/movies/')
63 then
64 return true
65 else
66 return false
67 end
68 end
70 function Lego.iter_streams(j)
71 local v = j['VideoHtml5'] or error('no match: VideoHtml5')
72 local u = v['Url'] or error('no match: media stream URL')
73 local S = require 'quvi/stream'
74 return {S.stream_new(u)}
75 end
77 function Lego.parse_thumb_url(qargs, p)
78 local t = {'thumbNavigation.+', '<img src="(.-)" alt="',qargs.title, '"/>',}
79 qargs.thumb_url = p:match(table.concat(t,'') or '')
80 end
82 -- vim: set ts=2 sw=2 tw=72 expandtab: