[grilo-plugins] lua-factory: Port Apple Trailers source to Lua



commit 069f7dcc4f2d7578a7efa7d0283783ff26434609
Author: Bastien Nocera <hadess hadess net>
Date:   Tue Jul 21 17:49:24 2015 +0200

    lua-factory: Port Apple Trailers source to Lua
    
    https://bugzilla.gnome.org/show_bug.cgi?id=752681

 src/lua-factory/sources/Makefile.am                |   11 +-
 .../sources/grl-appletrailers.gresource.xml        |    6 +
 src/lua-factory/sources/grl-appletrailers.lua      |  217 ++++++++++++++++++++
 src/lua-factory/sources/trailers.svg               |  107 ++++++++++
 4 files changed, 339 insertions(+), 2 deletions(-)
---
diff --git a/src/lua-factory/sources/Makefile.am b/src/lua-factory/sources/Makefile.am
index f49a328..325842c 100644
--- a/src/lua-factory/sources/Makefile.am
+++ b/src/lua-factory/sources/Makefile.am
@@ -18,7 +18,9 @@ lua_sources_DATA =                                    \
        grl-radiofrance.gresource                       \
        grl-video-title-parsing.lua                     \
        grl-pocket.lua                                  \
-       grl-pocket.gresource
+       grl-pocket.gresource                            \
+       grl-appletrailers.lua                           \
+       grl-appletrailers.gresource
 
 lua_sourcesdir = $(datadir)/$(LUA_FACTORY_SOURCE_LOCATION)
 
@@ -34,11 +36,15 @@ grl-radiofrance.gresource: grl-radiofrance.gresource.xml
 grl-pocket.gresource: grl-pocket.gresource.xml
        $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/grl-pocket.gresource.xml
 
+grl-appletrailers.gresource: grl-appletrailers.gresource.xml
+       $(AM_V_GEN) $(GLIB_COMPILE_RESOURCES) $(srcdir)/grl-appletrailers.gresource.xml
+
 CLEANFILES =                                           \
        grl-euronews.gresource                          \
        grl-guardianvideos.gresource                    \
        grl-radiofrance.gresource                       \
-       grl-pocket.gresource
+       grl-pocket.gresource                            \
+       grl-appletrailers.gresource
 
 EXTRA_DIST +=                                          \
        $(lua_sources_DATA)                             \
@@ -46,6 +52,7 @@ EXTRA_DIST +=                                         \
        grl-guardianvideos.gresource.xml                \
        grl-radiofrance.gresource.xml                   \
        grl-pocket.gresource.xml                        \
+       grl-appletrailers.gresource.xml                 \
        euronews.svg                                    \
        guardianvideos.svg                              \
        radiofrance.png                                 \
diff --git a/src/lua-factory/sources/grl-appletrailers.gresource.xml 
b/src/lua-factory/sources/grl-appletrailers.gresource.xml
new file mode 100644
index 0000000..9e788ed
--- /dev/null
+++ b/src/lua-factory/sources/grl-appletrailers.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/grilo/plugins/appletrailers">
+    <file compressed="true">trailers.svg</file>
+  </gresource>
+</gresources>
diff --git a/src/lua-factory/sources/grl-appletrailers.lua b/src/lua-factory/sources/grl-appletrailers.lua
new file mode 100644
index 0000000..f02eab1
--- /dev/null
+++ b/src/lua-factory/sources/grl-appletrailers.lua
@@ -0,0 +1,217 @@
+--[[
+ * Copyright (C) 2015 Grilo Project
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ *
+--]]
+
+---------------------------
+-- Source initialization --
+---------------------------
+
+source = {
+  id = "grl-appletrailers-lua",
+  name = "Apple Movie Trailers",
+  description = "Apple Trailers",
+  supported_media = 'video',
+  supported_keys = { 'author', 'publication-date', 'description', 'duration', 'genre', 'id', 'thumbnail', 
'title', 'url', 'certificate', 'studio', 'license', 'performer', 'size' },
+  config_keys = {
+    optional = { 'definition' },
+  },
+  icon = 'resource:///org/gnome/grilo/plugins/appletrailers/trailers.svg',
+  tags = { 'country:us', 'cinema', 'net:internet', 'net:plaintext' },
+}
+
+-- Global table to store config data
+ldata = {}
+
+-- Global table to store parse results
+cached_xml = nil
+
+function grl_source_init(configs)
+  ldata.hd = (configs.definition and configs.definition == 'hd')
+  return true
+end
+
+---------------------------------
+-- Handlers of Grilo functions --
+---------------------------------
+
+APPLE_TRAILERS_CURRENT_SD = "http://trailers.apple.com/trailers/home/xml/current_480p.xml";
+APPLE_TRAILERS_CURRENT_HD = "http://trailers.apple.com/trailers/home/xml/current_720p.xml";
+
+function grl_source_browse()
+  local skip = grl.get_options("skip")
+  local count = grl.get_options("count")
+
+  -- Make sure to reset the cache when browsing again
+  if skip == 0 then
+    cached_xml = nil
+  end
+
+  if cached_xml then
+    parse_results(cached_xml)
+  else
+    local url = APPLE_TRAILERS_CURRENT_SD
+    if ldata.hd then
+      url = APPLE_TRAILERS_CURRENT_HD
+    end
+
+    grl.debug('Fetching URL: ' .. url .. ' (count: ' .. count .. ' skip: ' .. skip .. ')')
+    grl.fetch(url, "fetch_results_cb")
+  end
+end
+
+---------------
+-- Utilities --
+---------------
+
+-- simili-XML parsing from
+-- http://lua-users.org/wiki/LuaXml
+function parseargs(s)
+  local arg = {}
+  string.gsub(s, "([%-%w]+)=([\"'])(.-)%2", function (w, _, a)
+    arg[w] = a
+  end)
+  return arg
+end
+
+function collect(s)
+  local stack = {}
+  local top = {}
+  table.insert(stack, top)
+  local ni,c,label,xarg, empty
+  local i, j = 1, 1
+  while true do
+    ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
+    if not ni then break end
+    local text = string.sub(s, i, ni-1)
+    if not string.find(text, "^%s*$") then
+      table.insert(top, text)
+    end
+    if empty == "/" then  -- empty element tag
+      table.insert(top, {label=label, xarg=parseargs(xarg), empty=1})
+    elseif c == "" then   -- start tag
+      top = {label=label, xarg=parseargs(xarg)}
+      table.insert(stack, top)   -- new level
+    else  -- end tag
+      local toclose = table.remove(stack)  -- remove top
+      top = stack[#stack]
+      if #stack < 1 then
+        error("nothing to close with "..label)
+      end
+      if toclose.label ~= label then
+        error("trying to close "..toclose.label.." with "..label)
+      end
+      table.insert(top, toclose)
+    end
+    i = j+1
+  end
+  local text = string.sub(s, i)
+  if not string.find(text, "^%s*$") then
+    table.insert(stack[#stack], text)
+  end
+  if #stack > 1 then
+    error("unclosed "..stack[#stack].label)
+  end
+  return stack[1]
+end
+
+function flatten_array(array)
+  local t = {}
+
+  for i, v in ipairs(array) do
+    if v.label == 'movieinfo' then
+      v.label = v.xarg.id
+    end
+
+    if v.xarg and v.xarg.filesize then
+      t['filesize'] = v.xarg.filesize
+    end
+
+    if v.label then
+      if (type(v) == "table") then
+        -- t['name'] already exists, append to it
+        if t[v.label] then
+          table.insert(t[v.label], v[1])
+        else
+          t[v.label] = flatten_array(v)
+        end
+      else
+        t[v.label] = v
+      end
+    else
+      if (type(v) == "table") then
+        table.insert(t, flatten_array(v))
+      else
+        table.insert(t, v)
+      end
+    end
+  end
+
+  return t
+end
+
+function fetch_results_cb(results)
+  if not results then
+    grl.warning('Failed to fetch XML file')
+    grl.callback()
+    return
+  end
+
+  local array = collect(results)
+  cached_xml = flatten_array(array)
+
+  parse_results(cached_xml)
+end
+
+function parse_results(results)
+  local count = grl.get_options("count")
+  local skip = grl.get_options("skip")
+
+  for i, item in pairs(results.records) do
+    local media = {}
+
+    media.type = 'video'
+    media.id = i
+    if item.cast then media.performer = item.cast.name end
+    media.genre = item.genre.name
+    media.license = item.info.copyright[1]
+    media.description = item.info.description[1]
+    media.director = item.info.director[1]
+    media.publication_date = item.info.releasedate[1]
+    media.certificate = item.info.rating[1]
+    media.studio = item.info.studio[1]
+    media.title = item.info.title[1]
+    media.thumbnail = item.poster.xlarge[1]
+    media.url = item.preview.large[1]
+    media.size = item.preview.filesize
+
+    if skip > 0 then
+      skip = skip - 1
+    else
+      count = count - 1
+      grl.callback(media, count)
+      if count == 0 then
+        return
+      end
+    end
+  end
+
+  if count ~= 0 then
+    grl.callback()
+  end
+end
diff --git a/src/lua-factory/sources/trailers.svg b/src/lua-factory/sources/trailers.svg
new file mode 100644
index 0000000..e58eeea
--- /dev/null
+++ b/src/lua-factory/sources/trailers.svg
@@ -0,0 +1,107 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<!-- Created with Inkscape (http://www.inkscape.org/) -->
+
+<svg
+   xmlns:dc="http://purl.org/dc/elements/1.1/";
+   xmlns:cc="http://creativecommons.org/ns#";
+   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
+   xmlns:svg="http://www.w3.org/2000/svg";
+   xmlns="http://www.w3.org/2000/svg";
+   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd";
+   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape";
+   width="256"
+   height="256"
+   id="svg2"
+   version="1.1"
+   inkscape:version="0.48.4 r9939"
+   sodipodi:docname="channel-bliptv.svg">
+  <defs
+     id="defs4">
+    <clipPath
+       id="clipPath6193"
+       clipPathUnits="userSpaceOnUse">
+      <path
+         id="path6195"
+         d="m 1600,2252.8 5020,0 0,3650 -5020,0 0,-3650 z" />
+    </clipPath>
+    <linearGradient
+       id="linearGradient6181"
+       spreadMethod="pad"
+       gradientTransform="matrix(-3.593e-5,822,822,3.593e-5,411,0)"
+       gradientUnits="userSpaceOnUse"
+       y2="0"
+       x2="1"
+       y1="0"
+       x1="0">
+      <stop
+         id="stop6183"
+         offset="0"
+         style="stop-opacity:1;stop-color:#c01e25" />
+      <stop
+         id="stop6185"
+         offset="1"
+         style="stop-opacity:1;stop-color:#e62426" />
+    </linearGradient>
+    <clipPath
+       id="clipPath6177"
+       clipPathUnits="userSpaceOnUse">
+      <path
+         id="path6179"
+         d="M 8220,0 0,0 l 0,8220 8220,0 0,-8220 m -6620,5902.8 0,-3650 5020,0 0,3650 -5020,0" />
+    </clipPath>
+  </defs>
+  <sodipodi:namedview
+     id="base"
+     pagecolor="#505050"
+     bordercolor="#666666"
+     borderopacity="1.0"
+     inkscape:pageopacity="1"
+     inkscape:pageshadow="2"
+     inkscape:zoom="1"
+     inkscape:cx="222.0001"
+     inkscape:cy="70.020428"
+     inkscape:document-units="px"
+     inkscape:current-layer="g7299"
+     showgrid="false"
+     borderlayer="true"
+     inkscape:showpageshadow="false"
+     inkscape:window-width="2560"
+     inkscape:window-height="1374"
+     inkscape:window-x="0"
+     inkscape:window-y="27"
+     inkscape:window-maximized="1" />
+  <metadata
+     id="metadata7">
+    <rdf:RDF>
+      <cc:Work
+         rdf:about="">
+        <dc:format>image/svg+xml</dc:format>
+        <dc:type
+           rdf:resource="http://purl.org/dc/dcmitype/StillImage"; />
+        <dc:title></dc:title>
+      </cc:Work>
+    </rdf:RDF>
+  </metadata>
+  <g
+     inkscape:label="Layer 1"
+     inkscape:groupmode="layer"
+     id="layer1"
+     transform="translate(0,-796.36218)">
+    <g
+       transform="matrix(630.025,0,0,-458.9,-77.5125,1218.8747)"
+       id="g6197" />
+    <g
+       id="g7288"
+       transform="translate(551.5,989.9982)">
+      <g
+         id="g7299"
+         transform="matrix(0.89344262,0,0,0.89344262,-396.25,-32.797277)">
+        <path
+           
style="color:#000000;fill:#73a444;fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:1;marker:none;visibility:visible;display:inline;overflow:visible;enable-background:accumulate"
+           d="M 128 20 C 67.800965 20 19 68.800965 19 129 C 19 189.19904 67.800965 238 128 238 C 188.19904 
238 237 189.19904 237 129 C 237 68.800965 188.19904 20 128 20 z M 114.0625 80.375 C 122.24545 80.375 
129.30417 84.775651 133.1875 91.34375 C 137.07083 84.775651 144.12955 80.375 152.3125 80.375 C 164.63538 
80.375 174.625 90.364625 174.625 102.6875 C 174.625 109.82355 171.27566 116.13442 166.0625 120.21875 C 
167.40093 121.38569 168.25 123.07666 168.25 125 C 168.23725 137.75 168.25 144.45727 168.25 163.25 C 168.25 
166.78175 165.40675 169.625 161.875 169.625 L 104.5 169.625 C 100.96825 169.625 98.125 166.78175 98.125 
163.25 L 98.125 149.5 L 81.8125 161.65625 C 81.688825 161.73913 81.547137 161.7953 81.40625 161.84375 C 
81.209262 161.87563 81.009488 161.87563 80.8125 161.84375 C 80.680538 161.8565 80.538212 161.8565 80.40625 
161.84375 C 80.265362 161.79275 80.123675 161.73913 80 161.65625 C 79.859112 161.60525 79.717426 161.55226 
79.59375 161.46875 C 79.522355 161.405 79.46
 6813 161.3214 79.40625 161.25 C 79.334855 161.18625 79.279313 161.1339 79.21875 161.0625 C 79.13524 
160.93882 79.04845 160.79714 79 160.65625 C 78.987251 160.52429 78.987251 160.38196 79 160.25 L 79 121.625 C 
78.987251 121.49304 78.987251 121.35071 79 121.21875 C 79.050998 121.07786 79.135238 120.93617 79.21875 
120.8125 C 79.282497 120.7411 79.334851 120.68556 79.40625 120.625 C 79.469997 120.5536 79.522351 120.46681 
79.59375 120.40625 C 79.657497 120.33485 79.7411 120.27931 79.8125 120.21875 C 79.876247 120.14735 79.9286 
120.09181 80 120.03125 C 80.123675 119.94837 80.265363 119.86095 80.40625 119.8125 C 80.603238 119.78062 
80.803012 119.78062 81 119.8125 C 81.066302 119.80866 81.121199 119.80866 81.1875 119.8125 C 81.258895 
119.87625 81.345687 119.95985 81.40625 120.03125 C 81.547137 120.08225 81.688825 120.13524 81.8125 120.21875 
L 98.125 132.375 L 98.125 125 C 98.125 123.07666 98.974068 121.38569 100.3125 120.21875 C 95.099344 116.13441 
91.75 109.82355 91.75 102.6875 C 9
 1.75 90.364625 101.73962 80.375 114.0625 80.375 z M 114.0625 93.125 C 108.78126 93.125 104.5 97.406259 104.5 
102.6875 C 104.5 107.96874 108.78126 112.25 114.0625 112.25 C 119.34374 112.25 123.625 107.96874 123.625 
102.6875 C 123.625 97.406259 119.34374 93.125 114.0625 93.125 z M 152.3125 93.125 C 147.03126 93.125 142.75 
97.406259 142.75 102.6875 C 142.75 107.96874 147.03126 112.25 152.3125 112.25 C 157.59374 112.25 161.875 
107.96874 161.875 102.6875 C 161.875 97.406259 157.59374 93.125 152.3125 93.125 z M 133.1875 114.03125 C 
132.17968 115.73586 131.03426 117.25093 129.625 118.625 L 136.78125 118.625 C 135.37199 117.25093 134.19532 
115.73586 133.1875 114.03125 z "
+           transform="matrix(1.1192661,0,0,1.1192661,-173.76606,-180.02135)"
+           id="path7297" />
+      </g>
+    </g>
+  </g>
+</svg>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]