media/myspass.lua: Rewrite ident function
[libquvi-scripts.git] / share / media / myspass.lua
blob2b52981fe3f50a7569c46e40243791becb1bf69d
1 -- libquvi-scripts
2 -- Copyright (C) 2013 Toni Gundogdu <legatvs@gmail.com>
3 -- Copyright (C) 2012 Guido Leisker <guido@guido-leisker.de>
4 --
5 -- This file is part of libquvi-scripts <http://quvi.sourceforge.net/>.
6 --
7 -- This program is free software: you can redistribute it and/or
8 -- modify it under the terms of the GNU Affero General Public
9 -- License as published by the Free Software Foundation, either
10 -- version 3 of the License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU Affero General Public License for more details.
17 -- You should have received a copy of the GNU Affero General
18 -- Public License along with this program. If not, see
19 -- <http://www.gnu.org/licenses/>.
22 local MySpass = {} -- Utility functions unique to this script
24 -- Identify the media script.
25 function ident(qargs)
26 return {
27 can_parse_url = MySpass.can_parse_url(qargs),
28 domains = table.concat({'myspass.de'}, ',')
30 end
32 -- Query available formats.
33 function query_formats(self)
34 self.formats = 'default'
35 return self
36 end
38 -- Parse media URL.
39 function parse(self)
40 self.host_id = "myspass"
42 self.id = self.page_url:match("(%d+)/?$")
43 or error("no match: media ID")
45 local format = MySpass.getMetadataValue(self, 'format')
46 local title = MySpass.getMetadataValue(self, 'title')
47 local season = MySpass.getMetadataValue(self, 'season')
48 local episode = MySpass.getMetadataValue(self, 'episode')
49 self.thumbnail_url = MySpass.getMetadataValue(self, 'imagePreview') or ''
51 self.title = string.format("%s %03d %03d %s", format, season,
52 episode, title)
54 self.url = {MySpass.getMetadataValue(self, 'url_flv')}
56 return self
57 end
60 -- Utility functions
63 function MySpass.can_parse_url(qargs)
64 local U = require 'socket.url'
65 local t = U.parse(qargs.input_url)
66 if t and t.scheme and t.scheme:lower():match('^http$')
67 and t.host and t.host:lower():match('myspass%.de$')
68 -- Expect all URLs ending with digits to be videos.
69 and t.path and t.path:lower():match('^/myspass/.-/%d+/?$')
70 then
71 return true
72 else
73 return false
74 end
75 end
77 function MySpass.getMetadataValue(self, key)
78 if self.metadata == nil then
79 self.metadata = quvi.fetch(
80 'http://www.myspass.de/myspass/'
81 .. 'includes/apps/video/getvideometadataxml.php?id='
82 .. self.id ) or error("cannot fetch meta data xml file")
83 end
84 local p = string.format("<%s>(.-)</%s>", key, key)
85 local temp = self.metadata:match(p) or error("meta data: no match: " .. key)
86 local value = temp:match('<!%[CDATA%[(.+)]]>') or temp
87 return value
88 end
90 -- vim: set ts=2 sw=2 tw=72 expandtab: