clPlayer.py
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 import os
00035
00036
00037 from time import strftime, gmtime, time
00038
00039
00040 import subprocess
00041
00042 from constants import *
00043 import functions
00044
00045
00046 functions.translation_gettext()
00047
00048
00049
00050 def notrans(txt):
00051 return txt
00052
00053
00054
00055
00056
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
00089
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
00099
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
00110
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:
00122 self.status = "Error"
00123 elif ret[0].strip() == "State: STOP":
00124 self.connection_tries +=1
00125 if self.connection_tries >= CONNECTION_TRIES:
00126
00127
00128
00129
00130 self.next()
00131
00132
00133
00134 else:
00135
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
00157
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
00166
00167
00168 def set_volume(self, volume):
00169 mocp = "mocp -v %s" % volume
00170 os.system(mocp)
00171
00172
00173
00174
00175
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
00193
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
00207
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
00220
00221 def test(self, array, key):
00222 if array.has_key(key):
00223 array[key] += 1
00224 else:
00225 array[key] = 1
00226
00227
00228
00229
00230
00231
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
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
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
00260
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)