Porting python 3 to groovy

Although i am not completely blind when trying to code stuff it usually takes a while for me to get my stuff to work, I have never programmed in groovy before so i thought i would seek some help. i have a python script i made that refreshes the connection between my cnct intelliplugs and amazon alexa. i have it set as a scheduled task on my pc to run every 12 hours, but i think it would work better if i could get smartthings to send this info for me so i could also add a virtual device to refresh the connection on demand if i need.

the code i use is here

import requests
import urllib
url = 'https://www.ikonkek2.com/KOAuthDemeter/Alley/authorize3'
login = "username@email.com here"
password = "password here"
client_id = 'client id here'
response_type = 'code'
values = {'client_id': client_id, 'user': login + "cnct", 'pwd': password, 'response_type': response_type}
r = requests.post(url, json=values)

data = r.json()

state = 'put state here'
scope = 'discoverOrControl'
uri = 'https://pitangui.amazon.com/api/skill/link/XXXXXXXXXXXX'

redir = uri + "?code=" + data['code'][0:32] + "&state=" + state + "&scope=" +scope

import urllib.request
with urllib.request.urlopen(redir) as response:
   html = response.read()

any help getting this process started would be greatly appreciated

Are you trying to launch something every 12h that your Alexa can activate?
if so, why not creating an Alexa routine directly with Alexa app and setup the recurrence inside?

Python 3 is not complete rewrite of Python; it still pretty much the good old Python you all love. But I am not going to lie. There are many changes in Python 3; many that will cause pain when you will port your code; and so many that I won’t be able to cover them all in this talk. That is why I will focus only on the changes that will need to know to port your code. If you want to learn about all new and shiny features, you will need to visit the python org’s website and the online documentation of Python 3.

Check this blog may be its could be useful to you :https://peadrop.com/blog/2009/04/05/porting-your-code-to-python-3/

You would need to make a smartapp to do what you are doing - look the Amazon dash button app for an example of handling http request/response. Not quite so straight forward and a lot of complexity for something that doesn’t look like has anything to do with ST.

TBH if you are doing this because ST hub box is always on and sitting on your LAN, and you don’t want to run your PC 24x7 to run a simple python script, then there are better and simpler approaches. @Philippe_Portes suggestion is the most logical one, or you could get $5 Raspberry Pi W and run your python code via a crown job on it and let it run 24x7 headless - just google how you do it headless and you won’t have to buy any accessories with it beyond a microsd card and microuab power dongle

1 Like