1
// Default MediaTomb playlist parsing script.
3
// Note: This script currently only handles .m3u and .pls playlists, but it can
4
// easily be extended. It doesn't use the "correct" way to parse the playlist,
5
// but a more fault-tolerant way, so it will try to do it's best and won't
6
// complain even if the playlist is completely messed up.
10
MediaTomb - http://www.mediatomb.cc/
12
playlists.js - this file is part of MediaTomb.
14
Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
15
Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
16
Leonhard Wimmer <leo@mediatomb.cc>
18
This file is free software; the copyright owners give unlimited permission
19
to copy and/or redistribute it; with or without modifications, as long as
20
this notice is preserved.
22
This file is distributed in the hope that it will be useful,
23
but WITHOUT ANY WARRANTY; without even the implied warranty of
24
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
26
$Id: playlists.js 1387 2007-07-08 23:58:04Z lww $
29
var playlistOrder = 1;
31
function addPlaylistItem(location, title, playlistChain, order)
33
if (location.match(/^.*:\/\//))
35
var exturl = new Object();
36
exturl.mimetype = 'audio/mpeg';
37
exturl.objectType = OBJECT_TYPE_ITEM_EXTERNAL_URL;
38
exturl.location = location;
39
exturl.title = (title ? title : location);
40
exturl.protocol = 'http-get';
41
exturl.upnpclass = UPNP_CLASS_ITEM_MUSIC_TRACK;
42
exturl.description = "Song from " + playlist.title;
43
exturl.playlistOrder = (order ? order : playlistOrder++);
44
addCdsObject(exturl, playlistChain, UPNP_CLASS_PLAYLIST_CONTAINER);
48
if (location.substr(0,1) != '/')
49
location = playlistLocation + location;
50
var item = new Object();
51
item.location = location;
56
var locationParts = location.split('/');
57
item.title = locationParts[locationParts.length - 1];
59
item.title = location;
61
item.objectType = OBJECT_TYPE_ITEM;
62
item.playlistOrder = (order ? order : playlistOrder++);
63
addCdsObject(item, playlistChain, UPNP_CLASS_PLAYLIST_CONTAINER);
67
print("Processing playlist: " + playlist.location);
69
var playlistLocation = playlist.location.substring(0, playlist.location.lastIndexOf('/') + 1);
71
// the function getPlaylistType is defined in common.js
72
var type = getPlaylistType(playlist.mimetype);
74
// the function createContainerChain is defined in common.js
75
var playlistChain = createContainerChain(new Array('Playlists', playlist.title));
79
print("Unknown playlist mimetype: '" + playlist.mimetype + "' of playlist '" + playlist.location + "'");
81
else if (type == 'm3u')
87
if (line.match(/^#EXTINF:(\d+),(\S.+)$/i))
89
// duration = RegExp.$1; // currently unused
92
else if (! line.match(/^(#|\s*$)/))
94
addPlaylistItem(line, title, playlistChain);
102
else if (type == 'pls')
110
if (line.match(/^\[playlist\]$/i))
112
// It seems to be a correct playlist, but we will try to parse it
113
// anyway even if this header is missing, so do nothing.
115
else if (line.match(/^NumberOfEntries=(\d+)$/i))
117
// var numEntries = RegExp.$1;
119
else if (line.match(/^Version=(\d+)$/i))
121
// var plsVersion = RegExp.$1;
123
else if (line.match(/^File\s*(\d+)\s*=\s*(\S.+)$/i))
125
var thisFile = RegExp.$2;
126
var id = parseInt(RegExp.$1, 10);
132
addPlaylistItem(file, title, playlistChain, lastId);
138
else if (line.match(/^Title\s*(\d+)\s*=\s*(\S.+)$/i))
140
var thisTitle = RegExp.$2;
141
var id = parseInt(RegExp.$1, 10);
147
addPlaylistItem(file, title, playlistChain, lastId);
153
else if (line.match(/^Length\s*(\d+)\s*=\s*(\S.+)$/i))
163
addPlaylistItem(file, title, playlistChain, lastId);