~vanvugt/ubuntu/oneiric/mediatomb/fix-770964-784431

« back to all changes in this revision

Viewing changes to scripts/js/playlists.js

  • Committer: Bazaar Package Importer
  • Author(s): Andres Mejia
  • Date: 2008-02-02 01:42:48 UTC
  • Revision ID: james.westby@ubuntu.com-20080202014248-cjouolddb8gi2zkz
Tags: upstream-0.10.0.dfsg1
ImportĀ upstreamĀ versionĀ 0.10.0.dfsg1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Default MediaTomb playlist parsing script.
 
2
//
 
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.
 
7
 
 
8
/*MT_F*
 
9
    
 
10
    MediaTomb - http://www.mediatomb.cc/
 
11
    
 
12
    playlists.js - this file is part of MediaTomb.
 
13
    
 
14
    Copyright (C) 2006-2007 Gena Batyan <bgeradz@mediatomb.cc>,
 
15
                            Sergey 'Jin' Bostandzhyan <jin@mediatomb.cc>,
 
16
                            Leonhard Wimmer <leo@mediatomb.cc>
 
17
    
 
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.
 
21
    
 
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.
 
25
    
 
26
    $Id: playlists.js 1387 2007-07-08 23:58:04Z lww $
 
27
*/
 
28
 
 
29
var playlistOrder = 1;
 
30
 
 
31
function addPlaylistItem(location, title, playlistChain, order)
 
32
{
 
33
    if (location.match(/^.*:\/\//))
 
34
    {
 
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);
 
45
    }
 
46
    else
 
47
    {
 
48
        if (location.substr(0,1) != '/')
 
49
            location = playlistLocation + location;
 
50
        var item  = new Object();
 
51
        item.location = location;
 
52
        if (title)
 
53
            item.title = title;
 
54
        else
 
55
        {
 
56
            var locationParts = location.split('/');
 
57
            item.title = locationParts[locationParts.length - 1];
 
58
            if (! item.title)
 
59
                item.title = location;
 
60
        }
 
61
        item.objectType = OBJECT_TYPE_ITEM;
 
62
        item.playlistOrder = (order ? order : playlistOrder++);
 
63
        addCdsObject(item, playlistChain,  UPNP_CLASS_PLAYLIST_CONTAINER);
 
64
    }
 
65
}
 
66
 
 
67
print("Processing playlist: " + playlist.location);
 
68
 
 
69
var playlistLocation = playlist.location.substring(0, playlist.location.lastIndexOf('/') + 1);
 
70
 
 
71
// the function getPlaylistType is defined in common.js
 
72
var type = getPlaylistType(playlist.mimetype);
 
73
 
 
74
// the function createContainerChain is defined in common.js
 
75
var playlistChain = createContainerChain(new Array('Playlists', playlist.title));
 
76
 
 
77
if (type == '')
 
78
{
 
79
    print("Unknown playlist mimetype: '" + playlist.mimetype + "' of playlist '" + playlist.location + "'");
 
80
}
 
81
else if (type == 'm3u')
 
82
{
 
83
    var title = null;
 
84
    var line = readln();
 
85
    do
 
86
    {
 
87
        if (line.match(/^#EXTINF:(\d+),(\S.+)$/i))
 
88
        {
 
89
            // duration = RegExp.$1; // currently unused
 
90
            title = RegExp.$2;
 
91
        }
 
92
        else if (! line.match(/^(#|\s*$)/))
 
93
        {
 
94
            addPlaylistItem(line, title, playlistChain);
 
95
            title = null;
 
96
        }
 
97
        
 
98
        line = readln();
 
99
    }
 
100
    while (line);
 
101
}
 
102
else if (type == 'pls')
 
103
{
 
104
    var title = null;
 
105
    var file = null;
 
106
    var lastId = -1;
 
107
    var line = readln();
 
108
    do
 
109
    {
 
110
        if (line.match(/^\[playlist\]$/i))
 
111
        {
 
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.
 
114
        }
 
115
        else if (line.match(/^NumberOfEntries=(\d+)$/i))
 
116
        {
 
117
            // var numEntries = RegExp.$1;
 
118
        }
 
119
        else if (line.match(/^Version=(\d+)$/i))
 
120
        {
 
121
            // var plsVersion =  RegExp.$1;
 
122
        }
 
123
        else if (line.match(/^File\s*(\d+)\s*=\s*(\S.+)$/i))
 
124
        {
 
125
            var thisFile = RegExp.$2;
 
126
            var id = parseInt(RegExp.$1, 10);
 
127
            if (lastId == -1)
 
128
                lastId = id;
 
129
            if (lastId != id)
 
130
            {
 
131
                if (file)
 
132
                    addPlaylistItem(file, title, playlistChain, lastId);
 
133
                title = null;
 
134
                lastId = id;
 
135
            }
 
136
            file = thisFile
 
137
        }
 
138
        else if (line.match(/^Title\s*(\d+)\s*=\s*(\S.+)$/i))
 
139
        {
 
140
            var thisTitle = RegExp.$2;
 
141
            var id = parseInt(RegExp.$1, 10);
 
142
            if (lastId == -1)
 
143
                lastId = id;
 
144
            if (lastId != id)
 
145
            {
 
146
                if (file)
 
147
                    addPlaylistItem(file, title, playlistChain, lastId);
 
148
                file = null;
 
149
                lastId = id;
 
150
            }
 
151
            title = thisTitle;
 
152
        }
 
153
        else if (line.match(/^Length\s*(\d+)\s*=\s*(\S.+)$/i))
 
154
        {
 
155
            // currently unused
 
156
        }
 
157
        
 
158
        line = readln();
 
159
    }
 
160
    while (line);
 
161
    
 
162
    if (file)
 
163
        addPlaylistItem(file, title, playlistChain, lastId);
 
164
}