fix bluescreen with getTrack of Playlist
[enigma2-plugins.git] / lastfm / src / StreamPlayer.py
1 from enigma import eServiceReference
2 from os import system
3 from os.path import isfile as os_path_isfile
4 from math import ceil
5 from time import time
6 from urllib import quote_plus
7 from Components.config import config
8
9 class StreamPlayer:
10     STATE_PLAYINGSTARTED = 0
11     STATE_STOP = 1
12     STATE_PLAYLISTENDS = 2
13     is_playing = False
14     trackstarttime = 0
15     currentplaylistitemnumber = 0
16     playlist = None
17     
18     def __init__(self,session, args = 0):
19         self.session = session
20         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
21         self.session.nav.event.append(self.__event)
22         self.onStateChanged = []
23     
24     def setPlaylist(self,playlist):
25         if self.playlist is not None:
26             self.currentplaylistitemnumber = 0 
27         self.playlist = playlist
28         
29     def stateChanged(self,reason):
30         for i in self.onStateChanged:
31             i(reason)   
32  
33     def __event(self, ev):
34         print "EVENT ==>",ev
35         if ev == 6:
36             self.stop("got EVENT 6, GST stopped")
37         if ev == 4:
38             self.trackstarttime = time()
39
40     def getRemaining(self):
41         track = self.playlist.getTrack(self.currentplaylistitemnumber)
42         if track is False:
43             return "N/A"
44         else:
45             remaining = int((track["duration"]/1000) - (time() - self.trackstarttime))
46             minutes = int(remaining/60)
47             seconds = int(remaining-(minutes*60))
48             def shiftchars(integer,char):
49                 if integer in range(0,10):
50                     return char+str(integer)
51                 else:
52                     return str(integer)
53             return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
54     
55     def play(self,tracknumber=False):
56         self.session.nav.stopService()
57             
58         if tracknumber is False:
59             self.currentplaylistitemnumber = 0 
60         else:
61             self.currentplaylistitemnumber = tracknumber
62         
63         track = self.playlist.getTrack(self.currentplaylistitemnumber)
64         if track is False:
65             print "no track to play"
66         elif track['location'] != "no location":
67             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
68             self.session.nav.stopService()
69             self.targetfile = "/tmp/lastfm.mp3"
70             system("mknod %s p" %self.targetfile)
71             url = track['location']
72             cmdstring = ""
73             if config.plugins.LastFM.useproxy.value:
74                 if os_path_isfile("/tmp/lastfmproxy"):
75                     url += "?savetrack=true&artist=%s&album=%s&title=%s&image=%s"%(quote_plus(track["creator"]),quote_plus(track["album"]),quote_plus(track["title"]),quote_plus(track["image"]))
76                     cmdstring += ("export http_proxy=\"http://localhost:%i\"; "%config.plugins.LastFM.proxyport.value)+cmdstring
77                 else:
78                     print "lastfmproxy seem not running, not using it"
79             cmdstring += "wget  '%s' -Y on -O- > '%s'&" %(url,self.targetfile)
80             
81             system(cmdstring)
82             self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
83         self.is_playing = True
84
85     def skip(self):
86         self.stop()
87                 
88     def stop(self,text="",force=False):
89         if self.playlist is None:
90             self.is_playing = False
91             self.stateChanged(self.STATE_STOP)
92         elif force is False and self.playlist.length > 0 and (self.playlist.length-1) > self.currentplaylistitemnumber:
93             self.play(tracknumber=self.currentplaylistitemnumber+1)
94             self.stateChanged(self.STATE_PLAYINGSTARTED)
95         elif self.is_playing is True and force is True:
96             self.session.nav.stopService()
97             system("killall -9 wget")
98             system("rm %s" %self.targetfile)
99             self.session.nav.playService(self.oldService)
100             self.is_playing = False
101             self.stateChanged(self.STATE_STOP)
102             
103         else:
104             self.stateChanged(self.STATE_PLAYLISTENDS)
105             
106     def exit(self):
107         self.stop()
108     
109     def getMetadata(self,key):
110         try:
111             track = self.playlist.getTrack(self.currentplaylistitemnumber)
112             return track[key]
113         except:
114             return "N/A"