1--[[
2 * Copyright (C) 2015 Grilo Project
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; version 2.1 of
7 * the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17 * 02110-1301 USA
18 *
19--]]
20
21---------------------------
22-- Source initialization --
23---------------------------
24
25source = {
26  id = "grl-appletrailers-lua",
27  name = "Apple Movie Trailers",
28  description = "Apple Trailers",
29  supported_media = 'video',
30  supported_keys = { 'author', 'publication-date', 'description', 'duration', 'genre', 'id', 'thumbnail', 'title', 'url', 'certificate', 'studio', 'license', 'performer', 'size' },
31  config_keys = {
32    optional = { 'definition' },
33  },
34  icon = 'resource:///org/gnome/grilo/plugins/appletrailers/trailers.svg',
35  tags = { 'country:us', 'cinema', 'net:internet', 'net:plaintext' },
36}
37
38-- Global table to store config data
39ldata = {}
40
41-- Global table to store parse results
42cached_xml = nil
43
44function grl_source_init(configs)
45  ldata.hd = (configs.definition and configs.definition == 'hd')
46  return true
47end
48
49---------------------------------
50-- Handlers of Grilo functions --
51---------------------------------
52
53APPLE_TRAILERS_CURRENT_SD = "http://trailers.apple.com/trailers/home/xml/current_480p.xml"
54APPLE_TRAILERS_CURRENT_HD = "http://trailers.apple.com/trailers/home/xml/current_720p.xml"
55
56function grl_source_browse()
57  local skip = grl.get_options("skip")
58  local count = grl.get_options("count")
59
60  -- Make sure to reset the cache when browsing again
61  if skip == 0 then
62    cached_xml = nil
63  end
64
65  if cached_xml then
66    parse_results(cached_xml)
67  else
68    local url = APPLE_TRAILERS_CURRENT_SD
69    if ldata.hd then
70      url = APPLE_TRAILERS_CURRENT_HD
71    end
72
73    grl.debug('Fetching URL: ' .. url .. ' (count: ' .. count .. ' skip: ' .. skip .. ')')
74    grl.fetch(url, fetch_results_cb)
75  end
76end
77
78---------------
79-- Utilities --
80---------------
81
82function fetch_results_cb(results)
83  if not results then
84    grl.warning('Failed to fetch XML file')
85    grl.callback()
86    return
87  end
88
89  cached_xml = grl.lua.xml.string_to_table(results)
90  parse_results(cached_xml)
91end
92
93function parse_results(results)
94  local count = grl.get_options("count")
95  local skip = grl.get_options("skip")
96
97  for i, item in pairs(results.records.movieinfo) do
98    local media = {}
99
100    media.type = 'video'
101    media.id = item.id
102    if item.cast then
103      media.performer = {}
104      for j, cast in pairs(item.cast.name) do
105        table.insert(media.performer, cast.xml)
106      end
107    end
108    if item.genre then
109      media.genre = {}
110      for j, genre in pairs(item.genre.name) do
111        table.insert(media.genre, genre.xml)
112      end
113    end
114    media.license = item.info.copyright.xml
115    media.description = item.info.description.xml
116    media.director = item.info.director.xml
117    media.publication_date = item.info.releasedate.xml
118    media.certificate = item.info.rating.xml
119    media.studio = item.info.studio.xml
120    media.title = item.info.title.xml
121    media.thumbnail = item.poster.xlarge.xml
122    media.url = item.preview.large.xml
123    media.size = tonumber(item.preview.large.filesize)
124    local mins, secs = item.info.runtime.xml:match('(%d):(%d)')
125    media.duration = tonumber(mins) * 60 + tonumber(secs)
126
127    if skip > 0 then
128      skip = skip - 1
129    else
130      count = count - 1
131      grl.callback(media, count)
132      if count == 0 then
133        return
134      end
135    end
136  end
137
138  if count ~= 0 then
139    grl.callback()
140  end
141end
142