commit do server
[remotepc] / pcremote-server / players / playlist.py
diff --git a/pcremote-server/players/playlist.py b/pcremote-server/players/playlist.py
new file mode 100755 (executable)
index 0000000..31a3a61
--- /dev/null
@@ -0,0 +1,161 @@
+# -*- coding: utf-8 -*-
+
+#  ****************************************************************************
+#  Copyright (c) 2008 INdT/Fucapi.
+#  This program 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, either version 3 of the License, or
+#  (at your option) any later version.
+#
+#  This program 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 program.  If not, see <http://www.gnu.org/licenses/>.
+#
+#  ============================================================================
+#  Project Name : PC Remote
+#  Author       : Jonatas Isvi 
+#  Email        : jonatas.nona@gmail.com
+#  Reviewer     :
+#  Email        :
+#  Version      : 1.0
+#  Package      : players
+#  Description  : Playlist
+#  ============================================================================
+
+import plistparser
+import pydcop
+
+class Playlist():
+
+    """ Playlist
+    make the interpreter and manipulation
+    of the player playlist, creates a composite
+    with any player class. 
+    """
+
+    # some importants variables
+    # analyze if file is a playlist
+    def __init__(self, file):
+               if self.isPlaylist(file):
+           self.file = file
+           self.songs = self.load()
+           self.currentSong = 0
+           self.fix()
+       else:
+           raise("Argument is not a playlist file")
+
+    # analyzes the file
+    def isPlaylist(self, file):
+       if not file:
+           return False
+       else:
+           return True
+
+    # make a list of dicts songs
+    def load(self):
+       self.songs = plistparser._request(self.file)    
+       return self.songs
+
+    # get the length of the current playlist
+    def length(self):
+       return len(self.songs)
+
+    # update the current song in songs list and return a song dict
+    def update(self, track, title, artist, path, ext):
+               self.currentSong = track
+       if self.songs[self.currentSong]['title'] == 'Unknown Title':
+           self.songs[self.currentSong]['title'] = title
+       if self.songs[self.currentSong]['artist'] == 'Unknown Artist':
+           self.songs[self.currentSong]['artist'] = artist
+       self.songs[self.currentSong]['path'] = path
+       self.songs[self.currentSong]['extension'] = ext
+       print self.songs[self.currentSong]
+
+
+    # show the current song
+    def show_playing_now(self):
+               return ('TITLE: %s' % self.songs[self.currentSong]['title'],  \
+               'ARTIST: %s' % self.songs[self.currentSong]['artist'],\
+               'TRACK: %s' % self.songs[self.currentSong]['track']
+               )
+
+    # get the current song filename if index is None
+    def song_filename(self, index=None):
+       if index == None:
+           return self.songs[self.currentSong]['title'] +" - "+\
+                  self.songs[self.currentSong]['artist'] +     \
+                  self.songs[self.currentSong]['extension']
+
+       else:
+           return self.songs[index]['title'] +" - "+\
+                  self.songs[index]['artist'] +     \
+                  self.songs[index]['extension']
+       
+    # get thr current song filesize if index is None
+    def song_size(self, index=None):
+       if index == None:
+                   return int(self.songs[self.currentSong]['filesize'])
+       else:
+           return int(self.songs[index]['filesize'])
+
+    # show all songs of the playlist
+    def show(self):
+       for i in range(self.length()):
+           print self.songs[i]['track'], " - ", \
+                 self.songs[i]['title'], " | ", \
+                 self.songs[i]['artist'],       \
+                 "\n"
+
+    # fix some problems of musics tags
+    def fix(self):
+       for i in range(self.length()):
+           if self.songs[i]['title'] == None:
+              self.songs[i]['title'] = 'Unknown Title'
+           elif self.songs[i]['artist'] == None:
+               self.songs[i]['artist'] = 'Unknown Artist'
+               
+
+    # get the porperties of any song of ther playlist  
+    def song_properties(self, index=None, track=False, title=False,\
+                        artist=False, ext=False, filesize=False, \
+                       duration=False, path=False):
+       props = {}
+       if index == None:
+           if track:
+               props['track'] = self.songs[self.currentSong]['track']
+           if title:
+               props['title'] = self.songs[self.currentSong]['title']
+           if artist:
+               props['artist'] = self.songs[self.currentSong]['artist']
+           if ext:
+               props['ext'] = self.songs[self.currentSong]['extension']
+           if filesize:
+               props['filesize'] = self.songs[self.currentSong]['filesize']
+           if duration:
+               props['duration'] = self.songs[self.currentSong]['duration']
+           if path:
+               props['path'] = self.songs[self.currentSong]['path']
+                       
+           return props
+       else:
+           if track:
+               props['track'] = self.songs[index]['track']
+           if title:
+               props['title'] = self.songs[index]['title']
+           if artist:
+               props['artist'] = self.songs[index]['artist']
+           if ext:
+               props['ext'] = self.songs[index]['extension']
+           if filesize:
+               props['filesize'] = self.songs[index]['filesize']
+           if duration:
+               props['duration'] = self.songs[index]['duration']
+           if path:
+               props['path'] = self.songs[index]['path']
+
+            return props
+