media/lego.lua: Update ID pattern
[libquvi-scripts.git] / share / media / lego.lua
blob112ac65ef3fb967abffa5e5f35899c4aab904ed5
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({'lego.com'}, ',')
30 end
32 -- Parse media properties.
33 function parse(qargs)
34 local p = quvi.http.fetch(qargs.input_url).data
36 if p:match('FirstVideoData') then
37 Lego.parse_movies(qargs, p)
38 else
39 Lego.parse_videos(qargs ,p)
40 end
42 return qargs
43 end
46 -- Utility functions.
49 function Lego.can_parse_url(qargs)
50 local U = require 'socket.url'
51 local t = U.parse(qargs.input_url)
52 if t and t.scheme and t.scheme:lower():match('^http$')
53 and t.host and t.host:lower():match('lego%.com$')
54 and t.path and (t.path:lower():match('/movies/')
55 or t.path:lower():match('/videos$'))
56 then
57 return true
58 else
59 return false
60 end
61 end
63 function Lego.movies_iter_streams(j)
64 local v = j['VideoHtml5'] or error('no match: VideoHtml5')
65 local u = v['Url'] or error('no match: media stream URL')
66 local S = require 'quvi/stream'
67 return {S.stream_new(u)}
68 end
70 function Lego.movies_thumb(qargs, p)
71 local t = {'thumbNavigation.+', '<img src="(.-)" alt="',qargs.title, '"/>',}
72 qargs.thumb_url = p:match(table.concat(t) or '')
73 end
75 function Lego.parse_movies(qargs, p) -- /movies/
76 local d = p:match('FirstVideoData = (.-);')
77 or error('no match: FirstVideoData')
79 local J = require 'json'
80 local j = J.decode(d)
82 qargs.title = j['Name'] or ''
83 Lego.movies_thumb(qargs, p)
85 qargs.streams = Lego.movies_iter_streams(j)
86 qargs.id = j['LikeObjectGuid'] or ''
87 end
89 function Lego.videos_iter_streams(L, i)
90 local u = L.find_first_tag(i, 'movie')[1]
91 local S = require 'quvi/stream'
92 return {S.stream_new(u)}
93 end
95 function Lego.parse_videos(qargs, p) -- /videos?video=ID
96 local d = p:match('name="flashvars" value="configxml=(.-</lego>)')
97 or error('no match: flashvars: configxml')
99 local L = require 'quvi/lxph'
100 local P = require 'lxp.lom'
102 local x = P.parse(d)
103 local m = L.find_first_tag(x, 'movieplayer')
105 local c = L.find_first_tag(m, 'content')
106 local i = L.find_first_tag(c, 'item')
108 qargs.title = L.find_first_tag(i, 'trackingName')[1]
109 qargs.thumb_url = L.find_first_tag(i, 'cover')[1]
111 local U = require 'socket.url'
112 local q = U.unescape(U.parse(qargs.input_url).query)
113 qargs.id = (q:match('video=%{(.-)%}') or ''):lower()
115 qargs.streams = Lego.videos_iter_streams(L, i)
118 -- vim: set ts=2 sw=2 tw=72 expandtab: