clPlayer.py

00001 #!/usr/bin/env python
00002 # -*- coding: utf-8 -*-
00003 
00004 # ----------------------------------------------------------------------------
00005 # pyjama - python jamendo audioplayer
00006 # Copyright (c) 2009 Daniel Nögel
00007 #
00008 # This program is free software: you can redistribute it and/or modify
00009 # it under the terms of the GNU General Public License as published by
00010 # the Free Software Foundation, either version 3 of the License, or
00011 # (at your option) any later version.
00012 #
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # You should have received a copy of the GNU General Public License
00018 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00019 # ----------------------------------------------------------------------------
00020 
00021 ######################################################################
00022 #                                                                    #
00023 #                THIS INTERFACE IS DEPRECATED                        #
00024 #                                                                    #
00025 ######################################################################
00026 
00027 ######################################################################
00028 #
00029 # This is pyjama's old MOC interface. It was just used for testing 
00030 # resons while i've been looking for a proper replacement which
00031 # has been found in gstreamer0.10
00032 #
00033 
00034 import os#, sys
00035 
00036 # Time formatting
00037 from time import strftime, gmtime, time
00038 
00039 # Pipes
00040 import subprocess
00041 
00042 from constants import *
00043 import functions
00044 
00045 # Gettext - Übersetzung
00046 functions.translation_gettext()
00047 #def notrans((string):
00048 #    return string
00049 
00050 def notrans(txt):
00051     return txt
00052 
00053 ###################################################################
00054 #
00055 # converts seconds into a more readable string
00056 # RETURNS: string
00057 #
00058 def sec2time(seconds):
00059     time_string = strftime("%M:%S", gmtime(seconds))
00060     return time_string
00061 
00062 class Player:
00063     def __init_notrans((self, parent):
00064         self.main = parent
00065         self.cur_playing = None
00066         self.last_played = None
00067         self.connection_tries = 0
00068 
00069         self.artist_counter = {}
00070         self.album_counter = {}
00071         self.track_counter = {}
00072 
00073         self.playlist = {}
00074         self.playlist_track_uids = []
00075         
00076         self.percentage = None
00077         self.text = None
00078         self.status = None 
00079         self.cursec = None
00080         self.duration = None
00081         self.togo = None
00082         self.artist_name  = None
00083         self.track_name = None
00084         self.numalbum = None
00085 
00086     ###################################################################
00087     #
00088     # adds a track-object to the playlist
00089     # RETURNS: n/A
00090     #
00091     def add2playlist(self, track):
00092         count = len(self.playlist)
00093         self.playlist[count] = track
00094         self.playlist_track_uids.append(track['uid'])
00095 
00096     ###################################################################
00097     #
00098     # guess what: clear playlist 
00099     # RETURNS: n/A
00100     #
00101     def clearplaylist(self):
00102         self.cur_playing = None
00103         self.last_played = None
00104         self.playlist = {}
00105         self.playlist_track_uids = []
00106 
00107     ###################################################################
00108     #
00109     # checks status of mocp
00110     # RETURNS: None if not playing, otherwise mocp status-infos
00111     #
00112     def check_status(self):
00113         if self.status == "End":
00114             return None
00115         if self.cur_playing == None: 
00116             self.status =  "End"
00117         else:
00118             ret = self.__pipe("mocp -i")
00119             if self.main.debug_extreme:
00120                 print ret
00121             if ret == None: #Fehler
00122                 self.status = "Error"
00123             elif ret[0].strip() == "State: STOP":
00124                 self.connection_tries +=1
00125                 if self.connection_tries >= CONNECTION_TRIES:
00126                     # SINCE JAMENDO DOES NOT HAVE
00127                     # SONGLENGTHS FOR EVERY TRACK
00128                     # WE JUST PLAY THE NEXT TRACK
00129                     # AFTER 5 RETRIES
00130                     self.next()
00131                     #self.cur_playing = None
00132                     #self.main.stop_timer()
00133                     #self.status = "Stop"
00134                 else:
00135                     #self.main.start_timer()
00136                     self.status = "Buffering %s" % (self.connection_tries*".")
00137             else:
00138                 self.status = "Playing"
00139                 self.cursec =  int(ret[7].replace("CurrentSec: ", "").strip())
00140                 self.duration = int(self.cur_playing['duration'])
00141                 self.percentage =  (1.0 / self.duration) * self.cursec
00142                 self.togo = self.cursec - self.duration
00143                 self.artist_name = self.cur_playing['artist_name']
00144                 self.track_name = self.cur_playing['name']
00145                 self.numalbum = self.cur_playing['numalbum']
00146                 self.text = "%s:%s: %s / %s - %s" % (self.cur_playing['artist_name'] ,self.cur_playing['name'], sec2time(self.cursec), sec2time(self.duration), sec2time(self.togo*-1))
00147                 if self.togo == -1:
00148                     uid = self.cur_playing['uid']
00149                     if uid in self.playlist_track_uids:
00150                         pos = self.playlist_track_uids.index(uid)
00151                         if len(self.playlist)> pos+1:
00152                             self.play(self.playlist[pos+1], pos+1)
00153 
00154     ###################################################################
00155     #
00156     # a tiny little pipe
00157     # RETURNS: string
00158     #
00159     def __pipe(self, command):
00160         p = subprocess.Popen(command, bufsize=0, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
00161         return p.stdout.readlines()
00162 
00163     ###################################################################
00164     #
00165     # set volume via mocp
00166     # RETURNS: n/A
00167     #
00168     def set_volume(self, volume):
00169         mocp = "mocp -v %s" % volume
00170         os.system(mocp)
00171 
00172     ###################################################################
00173     #
00174     # remove a track from playlist
00175     # RETURNS: n/A
00176     #
00177     def remove(self, items):
00178         for item in items:
00179             del self.playlist[item]
00180             self.playlist_track_uids = []
00181         pl = {}
00182         counter = 0
00183         for x in self.playlist:
00184             pl[counter] = self.playlist[x]
00185             self.playlist_track_uids.append(self.playlist[x]['uid'])
00186             counter += 1
00187         self.playlist = pl
00188             
00189         
00190     ###################################################################
00191     #
00192     # play next track in playlist
00193     # RETURNS: n/A
00194     #
00195     def next(self):
00196         uid = self.cur_playing['uid']
00197         if uid in self.playlist_track_uids:
00198             pos = self.playlist_track_uids.index(uid)
00199             if len(self.playlist)> pos+1:
00200                 self.play(self.playlist[pos+1], pos+1)
00201             else:
00202                 self.status = "End"
00203 
00204     ###################################################################
00205     #
00206     # play previous track in playlist
00207     # RETURNS: n/A
00208     #                
00209     def prev(self):
00210         uid = self.cur_playing['uid']
00211         if uid in self.playlist_track_uids:
00212             pos = self.playlist_track_uids.index(uid)
00213             if pos-1 >= 0:
00214                 self.play(self.playlist[pos-1], pos-1)
00215 
00216     ###################################################################
00217     #
00218     # ???
00219     # RETURNS: n/A
00220     #
00221     def test(self, array, key):
00222         if array.has_key(key):
00223             array[key] += 1
00224         else:
00225             array[key] = 1
00226 #        print array[key]
00227 
00228     ###################################################################
00229     #
00230     # play a track
00231     # RETURNS: n/A
00232     #            
00233     def play(self, track, activate_item=None):
00234         self.test(self.artist_counter, track['artist_id'])
00235         self.test(self.album_counter, track['album_id'])
00236         self.test(self.track_counter, track['id'])
00237         if track['stream'] == "query":
00238             track['stream'] = self.main.jamendo.stream(track['id'])
00239         if track['duration'] == 0:
00240             #GET FROM JAMENDO
00241             track['duration'] = -1
00242         x = "mocp -l " +  track['stream']
00243         ret = os.system(x)
00244         if ret <> 0:
00245             print notrans(("Error running moc")
00246             print notrans(("Sometimes deleting '~/.moc' helpes")
00247         self.connection_tries = 0
00248         self.last_played = self.cur_playing
00249         self.cur_playing = track
00250         self.status = "Starting"
00251         #self.main.start_timer()
00252         img = self.main.get_album_image(self.cur_playing['album_id'])
00253         self.main.notification(notrans(("Now playing"), "<b><i>%s</i></b>:\n%s" % (track['artist_name'], track['name']), icon = img, size = NOTIFICATION_COVER_SIZE)
00254         if activate_item != None:
00255             self.main.setplaylist(activate_item)
00256 
00257     ###################################################################
00258     #
00259     # stop playing
00260     # RETURNS: n/A
00261     #        
00262     def stop(self):
00263         self.percentage = None
00264         self.text = None
00265         self.cursec = None
00266         self.duration = None
00267         self.togo = None
00268         self.artist_name  = None
00269         self.track_name = None
00270         self.numalbum = None
00271         self.status = "End"
00272         x = "mocp -s"
00273         os.system(x)

Generated on Thu Jun 4 19:08:24 2009 for Pyjama by  doxygen 1.5.8