Created
July 15, 2013 14:19
-
-
Save dankrause/6000292 to your computer and use it in GitHub Desktop.
Tiny Roku remote library. Requires requests and ssdp.py from here: https://gist.github.com/dankrause/6000248
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ssdp | |
import requests | |
from xml.etree import ElementTree | |
class RokuApp(object): | |
def __init__(self, app_id, name, version, remote=None): | |
self.id = app_id | |
self.name = name | |
self.version = version | |
self._remote = remote | |
def __repr__(self): | |
cls = self.__class__.__name__ | |
values = {k: repr(v) for k, v in self.__dict__.iteritems()} | |
return "{0}({id}, {name}, {version})".format(cls, **values) | |
def launch(self): | |
self._remote.launch(self.id) | |
class RokuRemote(object): | |
def __init__(self, location): | |
self.location = location | |
def __repr__(self): | |
return "{0}({1})".format(self.__class__.__name__, repr(self.location)) | |
def _call(self, method, path, params=None): | |
url = "".join([self.location, path]) | |
return requests.request(method, url, params=params).text | |
def keypress(self, key): | |
self._call("post", "/keypress/{0}".format(key)) | |
def keydown(self, key): | |
self._call("post", "/keydown/{0}".format(key)) | |
def keyup(self, key): | |
self._call("post", "/keyup/{0}".format(key)) | |
def query_apps(self): | |
apps_xml = ElementTree.fromstring(self._call("get", "/query/apps")) | |
apps = {} | |
for app in apps_xml.getchildren(): | |
a = app.attrib | |
apps[a["id"]] = RokuApp(a["id"], app.text, a["version"], self) | |
return apps | |
def launch(self, app, params=None): | |
self._call("post", "/launch/{0}".format(app), params=params) | |
def send_input(self, params): | |
self._call("post", "/input", params=params) | |
def type_string(self, string): | |
for letter in string: | |
self.keypress("Lit_{0}".format(letter)) | |
def get_remotes(): | |
return {s.usn: RokuRemote(s.location) for s in ssdp.discover("roku:ecp")} | |
# Example: | |
# import roku | |
# remote = roku.get_remotes().values()[0] | |
# remote.keypress("home") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment