Use "Execute Script", not "Open File".
https://wiki.indigodomo.com/doku.php?id ... er_actions
FlyingDiver wrote:Use "Execute Script", not "Open File".
import requests
from requests.auth import HTTPDigestAuth
try:
r = requests.get('http://192.168.1.141:8141/cgi-bin/snapshot.cgi?', auth=HTTPDigestAuth('username', 'password'), timeout = 2.0)
r.raise_for_status()
except:
indigo.server.log("Can't get camera image", isError = True)
try:
with open('/Users/williammoore/Desktop/IPCamRotate/Testing.jpg', 'wb') as image_file:
image_file.write(r.content)
except:
indigo.server.log(“Can’t write the rotation image”, isError = True)
indigo.server.log(“Can’t write the rotation image”, isError = True)
FlyingDiver wrote:Highly recommend BBEdit. There's a free mode which is all you really need for basic stuff.
FlyingDiver wrote:This line has fancy quotes, which Python doesn't like:
ThisScript = 'Python Script With Error Handling'
import requests
from requests.auth import HTTPDigestAuth
try:
r = requests.get('http://192.168.1.141:8141/cgi-bin/snapshot.cgi?', auth=HTTPDigestAuth('username', 'password'), timeout = 2.0)
r.raise_for_status()
try:
with open('/Users/williammoore/Desktop/IPCamRotate/Testing.jpg', 'wb') as image_file:
image_file.write(r.content)
except:
indigo.server.log(ThisScript + ' :Rotation image error', isError = True)
except:
indigo.server.log(ThisScript + ' :No camera image available', isError = True)
ThisScript = 'Python Script With Error Handling'
import requests
from requests.auth import HTTPDigestAuth
try:
r = requests.get('http://192.168.1.141:8141/cgi-bin/snapshot.cgi?', auth=HTTPDigestAuth('username', 'password'), timeout = 2.0)
r.raise_for_status()
except:
indigo.server.log(ThisScript + ' :No camera image available', isError = True)
else:
try:
with open('/Users/williammoore/Desktop/IPCamRotate/Testing.jpg', 'wb') as image_file:
image_file.write(r.content)
except:
indigo.server.log(ThisScript + ' :Rotation image error', isError = True)
FlyingDiver wrote:This construct is a little easier to follow....
import requests
THIS_SCRIPT = 'Python Script With Error Handling: {}'
IMAGE_PATH = '/Users/williammoore/Desktop/IPCamRotate/Testing.jpg'
try:
# Get the image
reply = requests.get(
'http://192.168.1.141:8141/cgi-bin/snapshot.cgi?',
auth=requests.auth.HTTPDigestAuth('username', 'password'),
timeout = 2.0
)
# Raise an exception if the HTTP GET failed
reply.raise_for_status()
# Open the image file and write the image data
with open(IMAGE_PATH, 'wb') as image_file:
image_file.write(reply.content)
except requests.HTTPError:
# The request to get the image failed
indigo.server.log(
THIS_SCRIPT.format('No camera image available'),
isError=True
)
except:
# Something else went wrong, probably writing to the file
indigo.server.log(
THIS_SCRIPT.format('Rotation image error'),
isError=True
)
FlyingDiver wrote:I know PEP8 has that line length thing. Foo on them. I've got a huge monitor and I'm going to use it.
FlyingDiver wrote:Is that really in PEP8?
foo['bar'] = self.pluginPrefs['key']
var = indigo.variables[12345678].value
sandwich = "ham"
ice_cream_flavor = "Blackberry Swirl"
foo = "bar"
foo['bar'] = self.pluginPrefs['key']
var = indigo.variables[12345678].value
sandwich = "ham"
ice_cream_flavor = "Blackberry Swirl!"
foo = "bar"
DaveL17 wrote:PEP8 is a beautiful thing. Taken in moderation.
DaveL17 wrote:One spot where I disagree with PEP8 (that hasn't already been mentioned) is that I think this:
- Code: Select all
foo['bar'] = self.pluginPrefs['key']
var = indigo.variables[12345678].value
sandwich = "ham"
ice_cream_flavor = "Blackberry Swirl"
foo = "bar"
is much less readable than this:
- Code: Select all
foo['bar'] = self.pluginPrefs['key']
var = indigo.variables[12345678].value
sandwich = "ham"
ice_cream_flavor = "Blackberry Swirl!"
foo = "bar"
DaveL17 wrote:I think the line length limit of 79 originated because people used to print their code out on letter width paper--but I defer to those with degrees in computer science.
PEP8 wrote:Limiting the required editor window width makes it possible to have several files open side-by-side, and works well when using code review tools that present the two versions in adjacent columns.
I know PEP8 has that line length thing. Foo on them. I've got a huge monitor and I'm going to use it
I personally don't like it - all the extra white space sometimes requires me have to select the whole line to make sure I'm seeing the right assignments. To each his own...
Also, personal preference, but I wouldn't lump two wildly different operations (get and image write) into the same try block. But that's just me. Is that really in PEP8? I guess I should go look.
Users browsing this forum: No registered users and 0 guests