1-- libquvi-scripts v0.9.20131130
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.
11--
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.
16--
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/>.
20--
21
22local Lego = {} -- Utility functions unique to this script
23
24-- Identify the media script.
25function ident(qargs)
26  return {
27    can_parse_url = Lego.can_parse_url(qargs),
28    domains = table.concat({'lego.com'}, ',')
29  }
30end
31
32-- Parse media properties.
33function parse(qargs)
34  local p = quvi.http.fetch(qargs.input_url).data
35
36  if p:match('FirstVideoData') then
37    Lego.parse_movies(qargs, p)
38  else
39    Lego.parse_videos(qargs ,p)
40  end
41
42  return qargs
43end
44
45--
46-- Utility functions.
47--
48
49function 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
61end
62
63function 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)}
68end
69
70function Lego.movies_thumb(qargs, p)
71  local t = {'thumbNavigation.+', '<img src="(.-)" alt="',qargs.title, '"/>',}
72  qargs.thumb_url = p:match(table.concat(t) or '')
73end
74
75function Lego.parse_movies(qargs, p) -- /movies/
76  local d = p:match('FirstVideoData = (.-);')
77              or error('no match: FirstVideoData')
78
79  local J = require 'json'
80  local j = J.decode(d)
81
82  qargs.title = j['Name'] or ''
83  Lego.movies_thumb(qargs, p)
84
85  qargs.streams = Lego.movies_iter_streams(j)
86  qargs.id = j['LikeObjectGuid'] or ''
87end
88
89function 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)}
93end
94
95function 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')
98
99  local L = require 'quvi/lxph'
100  local P = require 'lxp.lom'
101
102  local x = P.parse(d)
103  local m = L.find_first_tag(x, 'movieplayer')
104
105  local c = L.find_first_tag(m, 'content')
106  local i = L.find_first_tag(c, 'item')
107
108  qargs.title = L.find_first_tag(i, 'trackingName')[1]
109  qargs.thumb_url = L.find_first_tag(i, 'cover')[1]
110
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()
114
115  qargs.streams = Lego.videos_iter_streams(L, i)
116end
117
118-- vim: set ts=2 sw=2 tw=72 expandtab:
119