Convert Python 2.5 Scripts to 3?

Posted on
Wed Nov 08, 2023 1:14 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Convert Python 2.5 Scripts to 3?

Getting an error running these scripts in 2022.2
Guessing they need to be updated to Python 3?
Code: Select all
#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

import simplejson
import urllib2

DTVIP = "192.168.1.53"
url = "http://%s:8080/tv/getTuned?" % DTVIP
f = urllib2.urlopen(url)
simplejson_string = f.read()
f.close()
parsed_simplejson = simplejson.loads(simplejson_string)

try:
   programTitle = parsed_simplejson['title']
   indigo.variable.updateValue(1708209973, programTitle)
except:
   indigo.variable.updateValue(1708209973, " ")
try:
   majorCh = str(parsed_simplejson['major'])
   indigo.variable.updateValue(564695813, majorCh)
except:
   indigo.variable.updateValue(564695813, " ")


Code: Select all
theText ="Ch " + indigo.variables["DTVchannelNumber"].value + " - " +  indigo.variables["DTVprogramTitle"].value
indigo.variable.updateValue("DTVall", theText)
indigo.variable.updateValue("Message_Upper", theText)


Appreciate any help!

Thanks,

Carl

Posted on
Wed Nov 08, 2023 2:51 pm
CraigM offline
Posts: 589
Joined: Oct 28, 2007

Re: Convert Python 2.5 Scripts to 3?

I would be curious to see how accurate this online converter did.
Code: Select all
#! /usr/bin/env python2.5
# -*- coding: utf-8 -*-

import simplejson
import urllib.request, urllib.error, urllib.parse

DTVIP = "192.168.1.53"
url = "http://%s:8080/tv/getTuned?" % DTVIP
f = urllib.request.urlopen(url)
simplejson_string = f.read()
f.close()
parsed_simplejson = simplejson.loads(simplejson_string)

try:
   programTitle = parsed_simplejson['title']
   indigo.variable.updateValue(1708209973, programTitle)
except:
   indigo.variable.updateValue(1708209973, " ")
try:
   majorCh = str(parsed_simplejson['major'])
   indigo.variable.updateValue(564695813, majorCh)
except:
   indigo.variable.updateValue(564695813, " ")

Posted on
Wed Nov 08, 2023 3:41 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Python 2.5 Scripts to 3?

Still no go:

Script Error embedded script error:
Script Error No module named 'simplejson'
Script Error Exception Traceback (most recent call shown last):

embedded script, line 4, at top level
ModuleNotFoundError: No module named 'simplejson'

Thanks,

Carl

Posted on
Wed Nov 08, 2023 4:15 pm
FlyingDiver offline
User avatar
Posts: 7222
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Convert Python 2.5 Scripts to 3?

Code: Select all
pip3 install simplejson

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Wed Nov 08, 2023 4:50 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Python 2.5 Scripts to 3?

Change:

Code: Select all
import simplejson


to:

Code: Select all
import json as simplejson


That should fix it (I don't believe the ancient simplejson module works in python3 since python 2.7 and newer included json).

Honestly, the better (but slightly more pervasive change) would be to just substitute json for simplejson throughout the script.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Nov 08, 2023 5:10 pm
jay (support) offline
Site Admin
User avatar
Posts: 18224
Joined: Mar 19, 2008
Location: Austin, Texas

Re: Convert Python 2.5 Scripts to 3?

Let's use this script as a learning opportunity since you posted the entire script. Here's how I would update it:

Code: Select all
import requests  # Indigo includes this and it's easier to use for making HTTP requests

# Constants - change as needed for your specific environment
DTVIP = "192.168.1.53"
PROGRAM_TITLE_VAR_ID = 1708209973
MAJOR_CHANNEL_VAR_ID = 564695813

# The rest shouldn't need to change unless you want to add more values to new variables
url = f"http://{DTVIP}:8080/tv/getTuned?"

# Get the JSON message from the device
reply = requests.get(url)

# Get a python dictionary from the JSON in the reply
reply_dict = reply.json()

# Set your Indigo variable values
try:
   programTitle = reply_dict['title']
   indigo.variable.updateValue(PROGRAM_TITLE_VAR_ID, programTitle)
except:
   indigo.variable.updateValue(PROGRAM_TITLE_VAR_ID, " ")
try:
   majorCh = str(reply_dict['major'])
   indigo.variable.updateValue(MAJOR_CHANNEL_VAR_ID, majorCh)
except:
   indigo.variable.updateValue(MAJOR_CHANNEL_VAR_ID, " ")


Untested, but it should be close.

Jay (Indigo Support)
Twitter | Facebook | LinkedIn

Posted on
Wed Nov 08, 2023 7:17 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Convert Python 2.5 Scripts to 3?

Works great.
Overall the upgrade from 7.2 to 2022.2 has gone amazingly well!

Many thanks!

Carl

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 11 guests