]> git.sesse.net Git - vlc/blob - share/lua/playlist/vimeo.lua
youtube.lua: descramble signatures by parsing javascript code
[vlc] / share / lua / playlist / vimeo.lua
1 --[[
2  $Id$
3
4  Copyright © 2009 the VideoLAN team
5
6  Authors: Konstantin Pavlov (thresh@videolan.org)
7           François Revol (revol@free.fr)
8
9  This program is free software; you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 --]]
23
24 function get_prefres()
25     local prefres = -1
26     if vlc.var and vlc.var.inherit then
27         prefres = vlc.var.inherit(nil, "preferred-resolution")
28         if prefres == nil then
29             prefres = -1
30         end
31     end
32     return prefres
33 end
34
35 -- Probe function.
36 function probe()
37     return vlc.access == "http"
38         and string.match( vlc.path, "vimeo.com/%d+$" )
39         -- do not match other addresses,
40         -- else we'll also try to decode the actual video url
41 end
42
43 -- Parse function.
44 function parse()
45     agent = vlc.var.inherit(nil,"http-user-agent")
46
47     if string.match( string.lower(agent), ".*vlc.*" ) then
48         vlc.msg.dbg("Wrong agent, adapting...")
49         return { { path = vlc.access .. "://" .. vlc.path; options = {":http-user-agent=Mozilla/5.0" } } }
50     end
51
52     _,_,id = string.find( vlc.path, "vimeo.com/([0-9]*)")
53     prefres = get_prefres()
54     ishd = false
55     quality = "sd"
56     codec = nil
57     line2 = ""
58     while true do
59         line = vlc.readline()
60         if not line then break end
61         if string.match( line, "{config:.*") then
62                 line2 = line;
63                 while not string.match( line2, "}};") do
64                         line2 = vlc.readline()
65                         if not line2 then break end
66                         line = line .. line2;
67                 end
68         end
69         -- Try to find the video's title
70         if string.match( line, "<meta property=\"og:title\"" ) then
71             _,_,name = string.find (line, "content=\"(.*)\">" )
72         end
73         if string.match( line, "{config:.*\"title\":\"" ) then
74             _,_,name = string.find (line, "\"title\":\"([^\"]*)\"," )
75         end
76         -- Try to find image for thumbnail
77         if string.match( line, "<meta property=\"og:image\"" ) then
78             _,_,arturl = string.find (line, "content=\"(.*)\">" )
79         end
80         if string.match( line, "<meta itemprop=\"thumbnailUrl\"" ) then
81             _,_,arturl = string.find (line, "content=\"(.*)\">" )
82         end
83         -- Try to find duration
84         if string.match( line, "{config:.*\"duration\":" ) then
85             _,_,duration = string.find (line, "\"duration\":([0-9]*)," )
86         end
87         -- Try to find request signature (needed to construct video url)
88         if string.match( line, "{config:.*\"signature\":" ) then
89             _,_,rsig = string.find (line, "\"signature\":\"([0-9a-f]*)\"," )
90         end
91         -- Try to find request signature time (needed to construct video url)
92         if string.match( line, "{config:.*\"timestamp\":" ) then
93             _,_,tstamp = string.find (line, "\"timestamp\":([0-9]*)," )
94         end
95         -- Try to find the available codecs
96         if string.match( line, "{config:.*,\"files\":{\"vp6\":" ) then
97             codec = "vp6"
98         end
99         if string.match( line, "{config:.*,\"files\":{\"vp8\":" ) then
100             codec = "vp8"
101         end
102         if string.match( line, "{config:.*,\"files\":{\"h264\":" ) then
103             codec = "h264"
104         end
105         -- Try to find whether video is HD actually
106         if string.match( line, "{config:.*,\"hd\":1" ) then
107             ishd = true
108         end
109         if string.match( line, "{config:.*\"height\":" ) then
110             _,_,height = string.find (line, "\"height\":([0-9]*)," )
111         end
112         if not line2 then break end
113     end
114
115     if not codec then
116         vlc.msg.err("unable to find codec info")
117         return {}
118     end
119
120     if ishd and ( not height or prefres < 0 or prefres >= tonumber(height) ) then
121         quality = "hd"
122     end
123     path = "http://player.vimeo.com/play_redirect?quality="..quality.."&codecs="..codec.."&clip_id="..id.."&time="..tstamp.."&sig="..rsig.."&type=html5_desktop_local"
124     return { { path = path; name = name; arturl = arturl; duration = duration } }
125 end