I'm trying to fix it so I get a camera image, but if for any reason it can't get the image after (whatever number of tries), then skip the image and send text or a blank static image (local URL).
- Code: Select all
# Step 1 - Camera Snapshot to File
import requests
url = 'http://IPADDRESS/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=me&pwd=donkey'
# Make the request and get the reply. (This is where the code get hung up when the camera is offline, "Max retries exceeded with url....")
reply = requests.get(url)
reply.raise_for_status()
#Trying to add an if/else.... but the code is getting hung up already and don't know if the below will even work
if reply is None or reply =="":
import requests
r = requests.post("https://api.pushover.net/1/messages.json", data = {
"token": "mytoken",
"user": "myusernumber",
"message": "Bill's Office Opened: Camera Offline"
})
print(r.text)
else:
# Save the content to a file
with open('/Users/williammoore/Desktop/IPCamRotate/R.jpg', 'wb') as image_file:
image_file.write(reply.content)
# Step 2 - Create Unique filename
import datetime
Time = "{date:%Y%m%d_%H-%M-%S}".format(date=datetime.datetime.now())
Title = "Bill_Office.jpg"
FileName = u"{}_{}".format(Time, Title)
# Step 3 - Copy Rotation Image to Log Image
from shutil import copyfile
copyfile('/Users/williammoore/Desktop/IPCamRotate/R.jpg','/Users/williammoore/Desktop/IPCamLog/'+ FileName)
# Step 4 - Send Image via pushover
theFile = "/Users/williammoore/Desktop/IPCamLog/" + FileName
import requests
r = requests.post("https://api.pushover.net/1/messages.json", data = {
"token": "mytoken",
"user": "myuser",
"message": FileName
},
files = {
"attachment": ("image.jpg", open(theFile, "rb"), "image/jpg")
})
print(r.text)