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