adding possibilty to use a proxy
[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 import time 
6 from httpclient 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         print " init StreamPlayer"
20         self.session = session
21         self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
22         self.session.nav.event.append(self.__event)
23         self.onStateChanged = []
24     
25     def setPlaylist(self,playlist):
26         if self.playlist is not None:
27             self.currentplaylistitemnumber = 0 
28         self.playlist = playlist
29         
30     def stateChanged(self,reason):
31         for i in self.onStateChanged:
32             i(reason)   
33  
34     def __event(self, ev):
35         print "EVENT ==>",ev
36         if ev == 6:
37             self.stop("got EVENT 6, GST stopped")
38         if ev == 4:
39             self.trackstarttime = time.time()
40
41     def getRemaining(self):
42         remaining = int((self.playlist.getTrack(self.currentplaylistitemnumber)["duration"]/1000) - (time.time() - self.trackstarttime))
43         minutes = int(remaining/60)
44         seconds = int(remaining-(minutes*60))
45         def shiftchars(integer,char):
46             if integer in range(0,10):
47                 return char+str(integer)
48             else:
49                 return str(integer)
50         return "-%s:%s"%(shiftchars(minutes," "), shiftchars(seconds,"0"))
51     
52     def play(self,tracknumber=False):
53         self.session.nav.stopService()
54             
55         if tracknumber is False:
56             self.currentplaylistitemnumber = 0 
57         else:
58             self.currentplaylistitemnumber = tracknumber
59         
60         track = self.playlist.getTrack(self.currentplaylistitemnumber)
61         
62         if track['location'] != "no location":
63             print "playing item "+str(self.currentplaylistitemnumber) +"/"+str(self.playlist.length)+" with url ",track['location']
64             self.session.nav.stopService()
65             self.targetfile = "/tmp/lastfm.mp3"
66             system("mknod %s p" %self.targetfile)
67             url = track['location']
68             cmdstring = ""
69             if config.plugins.LastFM.useproxy.value:
70                 if os_path_isfile("/tmp/lastfmproxy"):
71                     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"]))
72                     cmdstring += ("export http_proxy=\"http://localhost:%i\"; "%config.plugins.LastFM.proxyport.value)+cmdstring
73                 else:
74                     print "lastfmproxy seem not running, not using it"
75             cmdstring += "wget  '%s' -Y on -O- > '%s'&" %(url,self.targetfile)
76             
77             system(cmdstring)
78             self.session.nav.playService(eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s"%self.targetfile))
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             system("killall -9 wget")
94             system("rm %s" %self.targetfile)
95             self.session.nav.playService(self.oldService)
96             self.is_playing = False
97             self.stateChanged(self.STATE_STOP)
98             
99         else:
100             self.stateChanged(self.STATE_PLAYLISTENDS)
101             
102     def exit(self):
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"