httpclient removed, no using better twisted.web.client
[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         remaining = int((self.playlist.getTrack(self.currentplaylistitemnumber)["duration"]/1000) - (time() - self.trackstarttime))
42         minutes = int(remaining/60)
43         seconds = int(remaining-(minutes*60))
44         def shiftchars(integer,char):
45             if integer in range(0,10):
46                 return char+str(integer)
47             else:
48                 return str(integer)
49         return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
50     
51     def play(self,tracknumber=False):
52         self.session.nav.stopService()
53             
54         if tracknumber is False:
55             self.currentplaylistitemnumber = 0 
56         else:
57             self.currentplaylistitemnumber = tracknumber
58         
59         track = self.playlist.getTrack(self.currentplaylistitemnumber)
60         
61         if track['location'] != "no location":
62             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
63             self.session.nav.stopService()
64             self.targetfile = "/tmp/lastfm.mp3"
65             system("mknod %s p" %self.targetfile)
66             url = track['location']
67             cmdstring = ""
68             if config.plugins.LastFM.useproxy.value:
69                 if os_path_isfile("/tmp/lastfmproxy"):
70                     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"]))
71                     cmdstring += ("export http_proxy=\"http://localhost:%i\"; "%config.plugins.LastFM.proxyport.value)+cmdstring
72                 else:
73                     print "lastfmproxy seem not running, not using it"
74             cmdstring += "wget  '%s' -Y on -O- > '%s'&" %(url,self.targetfile)
75             
76             system(cmdstring)
77             self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
78         self.is_playing = True
79
80     def skip(self):
81         self.stop()
82                 
83     def stop(self,text="",force=False):
84         if self.playlist is None:
85             self.is_playing = False
86             self.stateChanged(self.STATE_STOP)
87         elif force is False and self.playlist.length > 0 and (self.playlist.length-1) > self.currentplaylistitemnumber:
88             self.play(tracknumber=self.currentplaylistitemnumber+1)
89             self.stateChanged(self.STATE_PLAYINGSTARTED)
90         elif self.is_playing is True and force is True:
91             self.session.nav.stopService()
92             system("killall -9 wget")
93             system("rm %s" %self.targetfile)
94             self.session.nav.playService(self.oldService)
95             self.is_playing = False
96             self.stateChanged(self.STATE_STOP)
97             
98         else:
99             self.stateChanged(self.STATE_PLAYLISTENDS)
100             
101     def exit(self):
102         self.stop()
103     
104     def getMetadata(self,key):
105         try:
106             track = self.playlist.getTrack(self.currentplaylistitemnumber)
107             return track[key]
108         except:
109             return "N/A"