Skip to content

Commit

Permalink
Merge pull request #38 from soberstadt/channels-new
Browse files Browse the repository at this point in the history
add new channel loading
  • Loading branch information
iantrich authored Jul 22, 2019
2 parents a3cd097 + 551735c commit 19cccb4
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
2 changes: 1 addition & 1 deletion roku/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from roku.core import Roku, Application, RokuException, __version__
from roku.core import Roku, Application, Channel, RokuException, __version__
36 changes: 33 additions & 3 deletions roku/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -103,6 +103,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 ('<Channel: [%s] %s>' %
(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, roku_type):
Expand Down Expand Up @@ -226,6 +247,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')
Expand Down Expand Up @@ -269,10 +298,11 @@ def power_state(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})
Expand Down
16 changes: 16 additions & 0 deletions roku/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 19cccb4

Please sign in to comment.