diff --git a/roku/__init__.py b/roku/__init__.py index a62e7d6..997081e 100644 --- a/roku/__init__.py +++ b/roku/__init__.py @@ -1 +1 @@ -from roku.core import Roku, Application, RokuException, __version__ +from roku.core import Roku, Application, Channel, RokuException, __version__ diff --git a/roku/core.py b/roku/core.py index 05fd66a..f846dc4 100644 --- a/roku/core.py +++ b/roku/core.py @@ -5,7 +5,7 @@ from six.moves.urllib_parse import urlparse from . import discovery -from .util import deserialize_apps +from .util import deserialize_apps, deserialize_channels try: from urllib.parse import quote_plus @@ -102,6 +102,27 @@ def store(self): self.roku.store(self) +class Channel(object): + + def __init__(self, number, name, roku=None): + self.number = str(number) + self.name = name + self.roku = roku + + def __eq__(self, other): + return isinstance(other, Channel) and \ + (self.number, self.name) == (other.number, other.name) + + def __repr__(self): + return ('' % + (self.number, self.name)) + + def launch(self): + if self.roku: + tv_app = Application(id= 'tvinput.dtv', version=None, name='TV', roku=self.roku) + self.roku.launch(tv_app, {'ch': self.number}) + + class DeviceInfo(object): def __init__(self, model_name, model_num, software_version, serial_num, user_device_name): @@ -223,6 +244,14 @@ def active_app(self): else: return None + @property + def tv_channels(self): + resp = self._get('/query/tv-channels') + channels = deserialize_channels(resp) + for c in channels: + c.roku = self + return channels + @property def device_info(self): resp = self._get('/query/device-info') @@ -248,10 +277,11 @@ def commands(self): def icon(self, app): return self._get('/query/icon/%s' % app.id) - def launch(self, app): + def launch(self, app, params={}): if app.roku and app.roku != self: raise RokuException('this app belongs to another Roku') - return self._post('/launch/%s' % app.id, params={'contentID': app.id}) + params['contentID'] = app.id + return self._post('/launch/%s' % app.id, params=params) def store(self, app): return self._post('/launch/11', params={'contentID': app.id}) diff --git a/roku/util.py b/roku/util.py index 5872e2e..bd42850 100644 --- a/roku/util.py +++ b/roku/util.py @@ -32,3 +32,19 @@ def serialize_apps(apps): content = bffr.getvalue() return content + +def deserialize_channels(doc, roku=None): + + from .core import Channel + + channels = [] + root = ET.fromstring(doc) + + for elem in root: + channel = Channel( + number=elem.find('number').text, + name=elem.find('name').text, + roku=roku, + ) + channels.append(channel) + return channels