initial version
[enigma2-plugins.git] / vlcplayer / src / VlcControlHttp.py
1 from urllib import urlencode\r
2 from urllib2 import urlopen\r
3 from xml.dom.minidom import parse\r
4 from Components.config import config\r
5 import socket\r
6 \r
7 def getText(nodelist):\r
8     rc = ""\r
9     for node in nodelist:\r
10         if node.nodeType == node.TEXT_NODE:\r
11             rc = rc + node.data\r
12     return rc\r
13 \r
14 class VlcControlHttp:\r
15         defaultStreamName = "dreambox"\r
16         \r
17         def __init__(self, servernum):\r
18                 cfg = config.plugins.vlcplayer.servers[servernum]\r
19                 self.servercfg = cfg\r
20                 self.host = cfg.host.value + ":" + str(cfg.httpport.value)\r
21                 self.lastError = None\r
22                 try:\r
23                         self.defaultStreamName = "dream" + socket.gethostbyname(socket.gethostname()).split('.')[3]\r
24                 except Exception, e:\r
25                         pass\r
26 \r
27         def connect(self):\r
28                 pass\r
29 \r
30         def close(self):\r
31                 pass\r
32         \r
33         def xmlRequest(self, request, params):\r
34                 uri = "/requests/"+request+".xml"\r
35                 if params is not None: uri = uri + "?" + urlencode(params)\r
36                 #print "[VLC] SEND:", self.host+uri\r
37                 resp = urlopen("http://"+self.host+uri)\r
38                 return parse(resp)\r
39 \r
40         def playfile(self, filename, output):\r
41                 filename = filename.replace("\\", "\\\\")\r
42                 filename = filename.replace("'", "\\'")\r
43                 input = filename\r
44                 if output is not None:\r
45                         input += " :sout=" + output;\r
46                 print "[VLC] playfile", input\r
47                 xml = self.xmlRequest("status", {"command": "in_play", "input": input})\r
48                 error = xml.getElementsByTagName("error")\r
49                 if error is not None and len(error) > 0:\r
50                         self.lastError = getText(error[0].childNodes).strip()\r
51                         if len(self.lastError) == 0:\r
52                                 self.lastError = None\r
53                         else:\r
54                                 print "[VLC] VlcControl error:", self.lastError\r
55                 else:\r
56                         self.lastError = None\r
57                 return self.lastError\r
58 \r
59         def status(self):\r
60                 xml = self.xmlRequest("status", None)\r
61                 stats = {}\r
62                 for e in xml.documentElement.childNodes:\r
63                         if e.nodeType == e.ELEMENT_NODE:\r
64                                 if e.firstChild is None:\r
65                                         stats[e.nodeName.encode("latin_1", "replace")] = None\r
66                                 else:\r
67                                         stats[e.nodeName.encode("latin_1", "replace")] = e.firstChild.nodeValue.encode("latin_1", "replace")\r
68                 return stats\r
69 \r
70         def get_playlist(self):\r
71                 xml = self.xmlRequest("playlist", None)\r
72                 flist = []\r
73                 for e in xml.getElementsByTagName("leaf"):\r
74                         fentry = {}\r
75                         fentry["id"] = int(e.getAttribute("id"))\r
76                         fentry["current"] = e.hasAttribute("current")\r
77                         if e.hasAttribute("uri") is not None:\r
78                                 fentry["name"] = e.getAttribute("uri").encode("latin_1", "replace")\r
79                         flist.append(fentry)\r
80                 return flist\r
81         \r
82         def get_current_id(self):\r
83                 flist = self.get_playlist()\r
84                 for f in flist:\r
85                         if f["current"]:\r
86                                 return f["id"]\r
87                 return -1\r
88 \r
89         def play(self, listid=None):\r
90                 if listid is None:\r
91                         self.xmlRequest("status", {"command": "pl_pause"})\r
92                 else:\r
93                         self.xmlRequest("status", {"command": "pl_play", "id": str(listid)})\r
94                 \r
95         def stop(self):\r
96                 self.xmlRequest("status", {"command": "pl_stop"})\r
97 \r
98         def pause(self):\r
99                 self.xmlRequest("status", {"command": "pl_pause"})\r
100                 \r
101         def delete(self, listid=None):\r
102                 if listid is None:\r
103                         listid = self.get_current_id()\r
104                 self.xmlRequest("status", {"command": "pl_delete", "id": str(listid)})\r
105                 \r
106         def seek(self, value):\r
107                 """  Allowed values are of the form:\r
108   [+ or -][<int><H or h>:][<int><M or m or '>:][<int><nothing or S or s or ">]\r
109   or [+ or -]<int>%\r
110   (value between [ ] are optional, value between < > are mandatory)\r
111 examples:\r
112   1000 -> seek to the 1000th second\r
113   +1H:2M -> seek 1 hour and 2 minutes forward\r
114   -10% -> seek 10% back"""\r
115                 self.xmlRequest("status", {"command": "seek", "val": str(value)})\r
116                 \r