adding gst-plugin-neonhttpsrc to Depends
[enigma2-plugins.git] / lastfm / src / StreamPlayer.py
1 from enigma import eServiceReference
2 from enigma import iPlayableService
3 from time import time
4 from urllib import quote_plus
5 from Components.config import config
6 from Components.ServiceEventTracker import ServiceEventTracker
7
8 class StreamPlayer:
9     STATE_PLAYINGSTARTED = 0
10     STATE_STOP = 1
11     STATE_PLAYLISTENDS = 2
12     is_playing = False
13     trackstarttime = 0
14     currentplaylistitemnumber = 0
15     playlist = None
16     targetfile = ""
17     onClose = []
18     def __init__(self,session, args = 0):
19         self.session = session
20         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
21         self.onStateChanged = []
22         self.__event_tracker = ServiceEventTracker(screen=self,eventmap=
23             {
24                 iPlayableService.evStart: self.__onStart,
25                 iPlayableService.evEOF: self.__onStop,
26             })
27     
28     def __onStart(self):
29         print "START"*20
30         self.trackstarttime = time()
31     
32     def __onStop(self):
33         print "STOP"*20
34         self.stop("got EVENT 6, GST stopped")
35         
36     def setSession(self,session):
37         self.session = session
38         
39     def setPlaylist(self,playlist):
40         if self.playlist is not None:
41             self.currentplaylistitemnumber = 0 
42         self.playlist = playlist
43         
44     def stateChanged(self,reason):
45         for i in self.onStateChanged:
46             i(reason)   
47
48     def getRemaining(self):
49         track = self.playlist.getTrack(self.currentplaylistitemnumber)
50         if track is False:
51             return "N/A"
52         else:
53             remaining = int((track["duration"]/1000) - (time() - self.trackstarttime))
54             minutes = int(remaining/60)
55             seconds = int(remaining-(minutes*60))
56             def shiftchars(integer,char):
57                 if integer in range(0,10):
58                     return char+str(integer)
59                 else:
60                     return str(integer)
61             return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
62     
63     def play(self,tracknumber=False):
64         self.session.nav.stopService()
65             
66         if tracknumber is False:
67             self.currentplaylistitemnumber = 0 
68         else:
69             self.currentplaylistitemnumber = tracknumber
70         
71         track = self.playlist.getTrack(self.currentplaylistitemnumber)
72         if track is False:
73             print "no track to play"
74         elif track['location'] != "no location":
75             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
76             self.session.nav.stopService()
77             sref = eServiceReference(4097,0,track['location'])
78             self.session.nav.playService(sref)
79         self.is_playing = True
80
81     def skip(self):
82         self.stop()
83                 
84     def stop(self,text="",force=False):
85         if self.playlist is None:
86             self.is_playing = False
87             self.stateChanged(self.STATE_STOP)
88         elif force is False and self.playlist.length > 0 and (self.playlist.length-1) > self.currentplaylistitemnumber:
89             self.play(tracknumber=self.currentplaylistitemnumber+1)
90             self.stateChanged(self.STATE_PLAYINGSTARTED)
91         elif self.is_playing is True and force is True:
92             self.session.nav.stopService()
93             self.session.nav.playService(self.oldService)
94             self.is_playing = False
95             self.stateChanged(self.STATE_STOP)
96             
97         else:
98             self.stateChanged(self.STATE_PLAYLISTENDS)
99             
100     def exit(self):
101         for x in self.onClose:
102             x()
103         self.stop()
104     
105     def getMetadata(self,key):
106         try:
107             track = self.playlist.getTrack(self.currentplaylistitemnumber)
108             return track[key]
109         except:
110             return "N/A"