Skip to content

Commit

Permalink
Fix code for running it in python3
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgecis committed Feb 21, 2019
1 parent 46193dd commit 3757fec
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@

#Do the login and get the token
token = client.login(args.username, args.password)
print "API Token", token
print ("API Token", token)

#Get the list of the controls
lst_ctl = client.list_controls()
for ctl in lst_ctl:
print "id %s Name %s" % (ctl["_id"], ctl['name'])
print ("id %s Name %s" % (ctl["_id"], ctl['name']))

#get the list of the available buttons for the first control
lst_bto = client.list_buttons(lst_ctl[1]["_id"])
for bto in lst_bto:
if bto["key"] == "POWER OFF": #Get the Id of the POWER OFF
print "id %s Key %s" % (bto["_id"], bto["key"])
print ("id %s Key %s" % (bto["_id"], bto["key"]))

#Do the infrared blast using the IDDEV of the first control
#and the id of the POWER OFF
Expand Down
16 changes: 10 additions & 6 deletions remotsylib/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
""" Remotsy library for python
Copyright 2018 Jorge Cisneros [email protected]
"""
from json import dumps, loads
from urllib2 import Request, urlopen, HTTPError, URLError
import sys
from json import dumps, loads
try:
# For Python 3.0 and later
from urllib.request import urlopen, Request, HTTPError, URLError
except ImportError:
# Fall back to Python 2's urllib2
from urllib2 import Request, urlopen, HTTPError, URLError


class API(object):
Expand All @@ -19,12 +24,12 @@ def post(self, url, data):
if self.auth_key is not None:
data["auth_key"] = self.auth_key
try:
resp = urlopen(req, dumps(data))
resp = urlopen(req, dumps(data).encode("utf-8"))
except HTTPError as errobj:
print errobj
print (errobj)
sys.exit(-1)
except URLError as errobj:
print errobj
print (errobj)
sys.exit(-2)
else:
body = loads(resp.read())
Expand Down Expand Up @@ -81,4 +86,3 @@ def update_firmware(self, iddev):
""" Function to update Remotsy's firmware"""
ret = self.post("devices/updatefirmware", {"id_dev": iddev})
return ret["status"] == "success"

0 comments on commit 3757fec

Please sign in to comment.