Skip to content

Commit 19cccb4

Browse files
authored
Merge pull request #38 from soberstadt/channels-new
add new channel loading
2 parents a3cd097 + 551735c commit 19cccb4

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

roku/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from roku.core import Roku, Application, RokuException, __version__
1+
from roku.core import Roku, Application, Channel, RokuException, __version__

roku/core.py

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from six.moves.urllib_parse import urlparse
66

77
from . import discovery
8-
from .util import deserialize_apps
8+
from .util import deserialize_apps, deserialize_channels
99

1010
try:
1111
from urllib.parse import quote_plus
@@ -103,6 +103,27 @@ def store(self):
103103
self.roku.store(self)
104104

105105

106+
class Channel(object):
107+
108+
def __init__(self, number, name, roku=None):
109+
self.number = str(number)
110+
self.name = name
111+
self.roku = roku
112+
113+
def __eq__(self, other):
114+
return isinstance(other, Channel) and \
115+
(self.number, self.name) == (other.number, other.name)
116+
117+
def __repr__(self):
118+
return ('<Channel: [%s] %s>' %
119+
(self.number, self.name))
120+
121+
def launch(self):
122+
if self.roku:
123+
tv_app = Application(id= 'tvinput.dtv', version=None, name='TV', roku=self.roku)
124+
self.roku.launch(tv_app, {'ch': self.number})
125+
126+
106127
class DeviceInfo(object):
107128

108129
def __init__(self, model_name, model_num, software_version, serial_num, user_device_name, roku_type):
@@ -226,6 +247,14 @@ def active_app(self):
226247
else:
227248
return None
228249

250+
@property
251+
def tv_channels(self):
252+
resp = self._get('/query/tv-channels')
253+
channels = deserialize_channels(resp)
254+
for c in channels:
255+
c.roku = self
256+
return channels
257+
229258
@property
230259
def device_info(self):
231260
resp = self._get('/query/device-info')
@@ -269,10 +298,11 @@ def power_state(self):
269298
def icon(self, app):
270299
return self._get('/query/icon/%s' % app.id)
271300

272-
def launch(self, app):
301+
def launch(self, app, params={}):
273302
if app.roku and app.roku != self:
274303
raise RokuException('this app belongs to another Roku')
275-
return self._post('/launch/%s' % app.id, params={'contentID': app.id})
304+
params['contentID'] = app.id
305+
return self._post('/launch/%s' % app.id, params=params)
276306

277307
def store(self, app):
278308
return self._post('/launch/11', params={'contentID': app.id})

roku/util.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ def serialize_apps(apps):
3232
content = bffr.getvalue()
3333

3434
return content
35+
36+
def deserialize_channels(doc, roku=None):
37+
38+
from .core import Channel
39+
40+
channels = []
41+
root = ET.fromstring(doc)
42+
43+
for elem in root:
44+
channel = Channel(
45+
number=elem.find('number').text,
46+
name=elem.find('name').text,
47+
roku=roku,
48+
)
49+
channels.append(channel)
50+
return channels

0 commit comments

Comments
 (0)