2 Translate trailers.apple.com video webpages URLs to the corresponding
6 Copyright © 2007-2010 the VideoLAN team
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 return (vlc.access == "http" or vlc.access == "https")
26 and string.match( vlc.path, "^trailers%.apple%.com/trailers/.+/.+" )
29 function find( haystack, needle )
30 local _,_,r = string.find( haystack, needle )
34 function parse_json(url)
35 vlc.msg.dbg("Trying to parse JSON from " .. url)
36 local json = require ("dkjson")
38 -- Use vlc.stream to grab a remote json file, place it in a string,
39 -- decode it and return the decoded data.
40 local stream = vlc.stream(url)
44 if not stream then return false end
47 line = stream:readline()
48 if not line then break end
50 string = string .. line
53 return json.decode(string)
63 if not line then break end
65 if string.match(line, "FilmId%s+=%s+'%d+'") then
66 video_id = find(line, "FilmId%s+=%s+'(%d+)'")
67 vlc.msg.dbg("Found FilmId " .. video_id)
73 if video_id ~= nil then
74 -- Lookup info from the json endpoint
75 local info = filmid_info(video_id)
78 if info["clips"] == nil then
79 vlc.msg.err("Unexpected JSON response from Apple trailers")
83 local movietitle = lookup_keys(info, "details/locale/en/movie_title")
84 local desc = lookup_keys(info, "details/locale/en/synopsis")
86 for _, clip in ipairs(info["clips"]) do
89 if clip["title"] == nil then
90 item["name"] = movietitle
92 item["name"] = movietitle .. " (" .. clip["title"] .. ")"
94 item["path"] = get_preferred_src(clip)
95 item["artist"] = clip["artist"]
96 item["arturl"] = clip["thumb"]
97 item["description"] = desc
98 item["url"] = vlc.path
100 table.insert(playlist, item)
103 vlc.msg.err("Couldn't extract trailer video URL")
109 -- Request, parse and return the info for a FilmID
110 function filmid_info(id)
111 local film_url = "https://trailers.apple.com/trailers/feeds/data/" .. id .. ".json"
112 vlc.msg.dbg("Fetching FilmID info from " .. film_url)
114 return parse_json(film_url)
117 -- Get the user-preferred quality src
118 function get_preferred_src(clip)
119 local resolution = vlc.var.inherit(nil, "preferred-resolution")
120 if resolution == -1 then
121 return lookup_keys(clip, "versions/enus/sizes/hd1080/srcAlt")
123 if resolution >= 1080 then
124 return lookup_keys(clip, "versions/enus/sizes/hd1080/srcAlt")
126 if resolution >= 720 then
127 return lookup_keys(clip, "versions/enus/sizes/hd720/srcAlt")
129 return lookup_keys(clip, "versions/enus/sizes/sd/srcAlt")
132 -- Resolve a "path" in a table or return nil if any of
133 -- the keys are not found
134 function lookup_keys(table, path)
136 for token in path:gmatch( "[^/]+" ) do