replace urllib.urlopen with twisted.web.client.getPage
[enigma2-plugins.git] / netcaster / src / bin / StreamPlayer.py
1 from enigma import eServiceReference
2
3 class StreamPlayer:
4         is_playing = False
5
6         def __init__(self, session, args=0):
7             print "[NETcaster.StreamPlayer] init StreamPlayer"
8             self.is_playing = False
9             self.session = session
10             self.oldService = self.session.nav.getCurrentlyPlayingServiceReference()
11             self.session.nav.event.append(self.__event)
12         
13         def __event(self, ev):
14             print "[NETcaster.StreamPlayer] EVENT ==>", ev
15         
16         def play(self, stream):
17             if self.is_playing:
18                 self.stop()
19             stream.getURL(self._playURL)
20         
21         def _playURL(self, url=None):
22                 if not url:
23                         print "no URL provided for play"
24                         return
25                 print "[NETcaster.StreamPlayer] playing stream", url
26                 
27                 esref = eServiceReference("4097:0:0:0:0:0:0:0:0:0:%s" % url.replace(':', '%3a'))
28                 
29                 try:
30                         self.session.nav.playService(esref)
31                         self.is_playing = True
32                 except:
33                         print "[NETcaster.StreamPlayer] Could not play %s" % esref
34         
35         def stop(self, text=""):
36             if self.is_playing:
37                 print "[NETcaster.StreamPlayer] stop streaming", text
38                 try:
39                     self.is_playing = False
40                     self.session.nav.stopService()
41                     self.session.nav.playService(self.oldService)
42                 except TypeError, e:
43                     print " ERROR Z", e
44                     self.exit()
45
46         def exit(self):
47             self.stop()
48