[vlc-devel] [PATCH] appletrailers.lua: Fix script for website changes

Marvin Scholz epirat07 at gmail.com
Wed Oct 26 15:58:50 CEST 2016


Fix the Script to work again with the changed
Apple trailers website.
---
 share/lua/playlist/appletrailers.lua | 135 +++++++++++++++++++++++------------
 1 file changed, 91 insertions(+), 44 deletions(-)

diff --git a/share/lua/playlist/appletrailers.lua b/share/lua/playlist/appletrailers.lua
index d8c3cc2..f68487b 100644
--- a/share/lua/playlist/appletrailers.lua
+++ b/share/lua/playlist/appletrailers.lua
@@ -20,11 +20,10 @@
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 --]]
 
--- Probe function.
+-- Probe function
 function probe()
-    return vlc.access == "http"
-        and string.match( vlc.path, "trailers.apple.com" )
-        and string.match( vlc.path, "web.inc" )
+    return (vlc.access == "http" or vlc.access == "https")
+        and string.match( vlc.path, "^trailers%.apple%.com/trailers/.+/.+" )
 end
 
 function find( haystack, needle )
@@ -32,62 +31,110 @@ function find( haystack, needle )
     return r
 end
 
-function sort(a, b)
-    if(a == nil) then return false end
-    if(b == nil) then return false end
+-- JSON parser lazy initialization
+local json = nil
 
-    local str_a
-    local str_b
+function json_init()
+    if json ~= nil then return false end
 
-    if(string.find(a.name, '%(') == 1) then
-        str_a = tonumber(string.sub(a.name, 2, string.find(a.name, 'p') - 1))
-        str_b = tonumber(string.sub(b.name, 2, string.find(b.name, 'p') - 1))
-    else
-        str_a = string.sub(a.name, 1, string.find(a.name, '%(') - 2)
-        str_b = string.sub(b.name, 1, string.find(b.name, '%(') - 2)
-        if(str_a == str_b) then
-            str_a = tonumber(string.sub(a.name, string.len(str_a) + 3, string.find(a.name, 'p', string.len(str_a) + 3) - 1))
-            str_b = tonumber(string.sub(b.name, string.len(str_b) + 3, string.find(b.name, 'p', string.len(str_b) + 3) - 1))
-        end
+    vlc.msg.dbg("JSON parser lazy initialization")
+    json = require ("dkjson")
+
+    -- Use vlc.stream to grab a remote json file, place it in a string,
+    -- decode it and return the decoded data.
+    json["parse_url"] = function(url)
+        local stream = vlc.stream(url)
+        local string = ""
+        local line   = ""
+
+        repeat
+            line = stream:readline()
+            string = string..line
+        until line ~= nil
+
+        return json.decode(string)
     end
-    if(str_a > str_b) then return false else return true end
 end
 
 -- Parse function.
 function parse()
+    local video_id = nil
     local playlist = {}
-    local description = ''
-    local art_url = ''
 
     while true
-    do 
+    do
         line = vlc.readline()
         if not line then break end
 
-        if string.match( line, "h3>.-</h3" ) then
-            description = find( line, "h3>(.-)</h3")
-            vlc.msg.dbg(description)
-        end
-        if string.match( line, 'img src=') then
-            for img in string.gmatch(line, '<img src="(http://.*%.jpg)" ') do
-                art_url = img
-            end
-            for i,value in pairs(playlist) do
-                if value.arturl == '' then
-                    playlist[i].arturl = art_url
-                end
-            end
+        if string.match(line, "FilmId%s+=%s+'%d+'") then
+            video_id = find(line, "FilmId%s+=%s+'(%d+)'")
+            vlc.msg.dbg("Found FilmId " .. video_id)
+            break
         end
-        if string.match( line, 'class="hd".-%.mov') then
-            for urlline,resolution in string.gmatch(line, 'class="hd".-href="(.-%.mov)".->(%d+.-p)') do
-                urlline = string.gsub( urlline, "_"..resolution, "_h"..resolution )
-                table.insert( playlist, { path = urlline,
-                                          name = description.." "..resolution,
-                                          arturl = art_url,
-                                          options = {":http-user-agent=QuickTime/7.5", ":play-and-pause", ":demux=avformat"} } )
-            end
+    end
+
+    -- Found a video id
+    if video_id ~= nil then
+        -- Lookup info from the json endpoint
+        local info = filmid_info(video_id)
+
+        -- Parse data
+        if info["clips"] == nil then return playlist end
+        local movietitle = lookup_keys(info, "details/locale/en/movie_title")
+        local desc       = lookup_keys(info, "details/locale/en/synopsis")
+
+        for _, clip in ipairs(info["clips"]) do
+            local item          = {}
+            item["path"]        = get_preferred_src(clip)
+            item["name"]        = movietitle .. " (" .. clip["title"] .. ")"
+            item["artist"]      = clip["artist"]
+            item["arturl"]      = clip["thumb"]
+            item["description"] = desc
+            item["url"]         = vlc.path
+
+            table.insert(playlist, item)
         end
+    else
+        vlc.msg.err("Couldn't extract trailer video URL")
     end
 
     return playlist
 end
+
+-- Request, parse and return the info for a FilmID
+function filmid_info(id)
+    json_init()
+
+    local film_url = "https://trailers.apple.com/trailers/feeds/data/" .. id .. ".json"
+    vlc.msg.dbg("Fetching FilmID info from " .. film_url)
+
+    return json.parse_url(film_url)
+end
+
+-- Get the user-preferred quality src
+function get_preferred_src(clip)
+    local resolution = vlc.var.inherit(nil, "preferred-resolution")
+    if resolution == -1 then
+        return lookup_keys(clip, "versions/enus/sizes/hd1080/srcAlt")
+    end
+    if resolution >= 1080 then
+        return lookup_keys(clip, "versions/enus/sizes/hd1080/srcAlt")
+    end
+    if resolution >= 720 then
+        return lookup_keys(clip, "versions/enus/sizes/hd720/srcAlt")
+    end
+    return lookup_keys(clip, "versions/enus/sizes/sd/srcAlt")
+end
+
+-- Resolve a "path" in a table or return nil if any of
+-- the keys are not found
+function lookup_keys(table, path)
+    local value = table
+    for token in path:gmatch( "[^/]+" ) do
+        value = value[token]
+        if value == nil then
+            break
+        end
+    end
+    return value
+end
-- 
2.8.4 (Apple Git-73)



More information about the vlc-devel mailing list