1 |
apollock |
8 |
#!/usr/bin/python |
2 |
|
|
|
3 |
|
|
import socket |
4 |
|
|
import datetime |
5 |
|
|
|
6 |
|
|
class Frontend: |
7 |
|
|
"""Class for operating on a MythTV frontend""" |
8 |
|
|
|
9 |
|
|
def __init__(self, host, port=6546): |
10 |
|
|
self.host = host |
11 |
|
|
self.port = port |
12 |
|
|
self.ready = False |
13 |
|
|
self.socket = None |
14 |
|
|
|
15 |
|
|
|
16 |
|
|
def _connect(self): |
17 |
|
|
"""Create a connection to the frontend""" |
18 |
|
|
|
19 |
|
|
self.socket = socket.socket() |
20 |
|
|
self.socket.connect((self.host, self.port)) |
21 |
|
|
if self.socket.recv(4096).splitlines()[-1] == '# ': |
22 |
|
|
self.ready = True |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
def _sendcmd(self, cmd): |
26 |
|
|
"""Send a command to the frontend""" |
27 |
|
|
|
28 |
|
|
#TODO(apollock): raise an exception if not ready |
29 |
|
|
if self.ready: |
30 |
|
|
self.socket.send("%s\r\n" % cmd) |
31 |
|
|
return self.socket.recv(4096).splitlines() |
32 |
|
|
|
33 |
|
|
|
34 |
|
|
def location(self): |
35 |
|
|
"""Return what location the front end is currently in""" |
36 |
|
|
|
37 |
|
|
location = {} |
38 |
|
|
self._connect() |
39 |
|
|
result = self._sendcmd("query location")[0].split(" ") |
40 |
|
|
#TODO(apollock): convert date and time to something other |
41 |
|
|
# than a string |
42 |
|
|
# Maybe return a location object instead? |
43 |
|
|
|
44 |
|
|
location['where'] = result[0] |
45 |
|
|
|
46 |
|
|
if location['where'] == 'Playback': |
47 |
|
|
location['what'] = result[1] |
48 |
|
|
location['position'] = result[2] |
49 |
|
|
location['length'] = result[4] |
50 |
|
|
location['speed'] = result[5] |
51 |
|
|
location['chanid'] = result[6] |
52 |
|
|
recdate = result[7].split("T")[0].split("-") |
53 |
|
|
location['recdate'] = datetime.date(int(recdate[0]), int(recdate[1]), int(recdate[2])) |
54 |
|
|
rectime = result[7].split("T")[1].split(":") |
55 |
|
|
location['rectime'] = datetime.time(int(rectime[0]), int(rectime[1]), int(rectime[2])) |
56 |
|
|
if location['what'] == 'Recorded': |
57 |
|
|
location['title'] = self.recording(location['chanid'], location['recdate'].isoformat(), location['rectime'].isoformat()) |
58 |
|
|
elif location['where'] == 'PlaybackBox': |
59 |
|
|
pass |
60 |
|
|
elif location['where'] == 'MainMenu': |
61 |
|
|
pass |
62 |
|
|
|
63 |
|
|
return location |
64 |
|
|
|
65 |
|
|
|
66 |
|
|
def recording(self, chanid, recdate, rectime): |
67 |
|
|
"""Returns recording information for a specified recording""" |
68 |
|
|
|
69 |
|
|
self._connect() |
70 |
|
|
result = self._sendcmd("query recording %s %sT%s" % |
71 |
|
|
(chanid, recdate, rectime))[0].split(" ") |
72 |
|
|
|
73 |
|
|
return " ".join(result[2:]) |
74 |
|
|
|
75 |
|
|
|
76 |
|
|
def pause(self): |
77 |
|
|
"""Pauses the playback""" |
78 |
|
|
|
79 |
|
|
self._connect() |
80 |
|
|
result = self._sendcmd("play speed pause")[0] |
81 |
|
|
if result != "OK": |
82 |
|
|
#TODO(apollock): raise some sort of exception |
83 |
|
|
pass |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
def play(self): |
87 |
|
|
"""Resumes playback""" |
88 |
|
|
self._connect() |
89 |
|
|
result = self._sendcmd("play speed normal")[0] |
90 |
|
|
if result != "OK": |
91 |
|
|
#TODO(apollock): raise some sort of exception |
92 |
|
|
pass |