URL Camera Script with if/else?

Posted on
Mon Jul 22, 2019 11:57 pm
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

URL Camera Script with if/else?

I have a script that works great when everything is working. Just found out that if the camera goes offline, then the script is a failure.

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)

Bill
My Plugin: My People

Posted on
Tue Jul 23, 2019 7:23 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: URL Camera Script with if/else?

Something like this should work. Untested. I also cleaned up a bunch of redundant or unnecessary code.

Code: Select all
import requests
import datetime

# Step 1 - Camera Snapshot to File

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....")

try:
    reply = requests.get(url, timeout=1.0)       

except:
    data = {
        "token": "mytoken",
        "user": "myusernumber",
        "message": "Bill's Office Opened: Camera Offline"
    }
    r = requests.post("https://api.pushover.net/1/messages.json", data=data)
    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


    Time = "{date:%Y%m%d_%H-%M-%S}".format(date=datetime.datetime.now())
    Title = "Bill_Office.jpg"

    FileName = u"/Users/williammoore/Desktop/IPCamLog/{}_{}".format(Time, Title)

    # Step 3 - Save same image data to Log Image

   with open(FileName, 'wb') as image_file:
       image_file.write(reply.content)

    # Step 4 - Send Image via pushover

    data = {
        "token": "mytoken",
        "user": "myuser",
        "message": FileName
    }
    files = {
        "attachment": ("image.jpg", open(theFile, "rb"), "image/jpg")
    }
    r = requests.post("https://api.pushover.net/1/messages.json", data=data, files=files)
    print(r.text)

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

Posted on
Tue Jul 23, 2019 8:40 am
whmoorejr offline
User avatar
Posts: 762
Joined: Jan 15, 2013
Location: Houston, TX

Re: URL Camera Script with if/else?

FlyingDiver wrote:
Something like this should work. Untested. I also cleaned up a bunch of redundant or unnecessary code.


Worked great with a couple minor tweaks.

Python and it's indentions.... somewhere an indent was off, so I re-indented everything. After cleaning up the copy file part, I had to change the variable in the last pushover part to attach FileName instead of theFile.

To test functionality, I called the image from a working camera then switched to the non-working camera.

Now I just wait for my new Amcrest camera to arrive to replace the Foscam C2 with the dead wifi.

Bill
My Plugin: My People

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 3 guests