Camera Snapshot -> Copy image -> send one via email / text

Posted on
Tue May 21, 2019 3:15 pm
whmoorejr offline
User avatar
Posts: 523
Joined: Jan 15, 2013
Location: Houston, TX

Camera Snapshot -> Copy image -> send one via email / text

The bulk of my applescripts have something to do with images. And they are all embedded in actions. This one does a few things....
1) takes a snapshot from a camera (Curl) and saves it in a folder "IPCamRotate/R.jpg". <-- I have several control pages that look at that image... so when something changes that one image address can change from any camera or any other image. (The key to this is that the "R.jpg" is continually overwritten with updated images based on different events)

2) Uses some script to create a unique filename "set filename to "Backyard_" & the file & ".jpg as string"

3) Copies R.jpg to new jpg with the unique filename to be stored in a log folder on the server computer.

4) Sends the new logged jpg image file as an attachment. (in this script, I send the image via iMessage). I may have to modify to send via email to text or via pushover.

Code: Select all
do shell script "curl -f 'http://10.0.6.185:8085/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=whmoorejr&pwd=donkey' -o ~/Desktop/IPCamRotate/R.jpg"

set TheYear to year of (current date) as string
set TheMonth to month of (current date) as integer
set TheDay to day of (current date) as string
set Today to TheYear & TheMonth & TheDay as string

set Myhour to get the (hours of (current date)) as string
set Myminutes to get the (minutes of (current date)) as string
set Myseconds to get the (seconds of (current date)) as string
set MyTime to Myhour & "-" & Myminutes & "-" & Myseconds

set thefile to Today & "_" & MyTime as string

--set filedate to (current date) as number
set filename to "BackYard_" & thefile & ".jpg" as string

set sourceFile to POSIX file "/Users/williammoore/Desktop/IPCamRotate/R.jpg"
set destFolder to POSIX file "/Users/williammoore/Desktop/IPCamLog"

tell application "Finder"
   set dupeFile to duplicate sourceFile to destFolder
   set dupeFile's name to filename
end tell

tell application "Finder"
   
   set folderPath to folder "MacMini:Users:williammoore:Desktop:IPCamLog"
   set thefile to file filename in folderPath as alias
   set filename to name of thefile
   
end tell
set theAttachment1 to thefile

tell application "Messages"
   send theAttachment1 to buddy "bill.moore.jr@gmail.com" of (service 1 whose service type is iMessage)
   --   send theAttachment1 to buddy "msvikki04@gmail.com" of (service 1 whose service type is iMessage)
end tell


So far (from this part of the forum), I've got...

Code: Select all
url = 'http://10.0.6.185:8085/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=whmoorejr&pwd=donkey'

# Make the request and get the reply
reply = requests.get(url)

# This will raise an exception if the request returned an error
reply.raise_for_status()

# Save the content to a file
with open('/Users/williammoore/Desktop/IPCamRotate/R.jpg', 'wb') as image_file:
    image_file.write(reply.content)
But the above died on the 2nd line "embedded script: global name 'requests' is not defined"

I also tried
Code: Select all
import os
os.system("/usr/bin/curl -f 'http://10.0.6.185:8085/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=whmoorejr&pwd=donkey -o ~/Desktop/IPCamRotate/R.jpg")
No error... but didn't do anything either

Bill
My Plugin: My People

Posted on
Tue May 21, 2019 10:44 pm
RogueProeliator offline
User avatar
Posts: 2437
Joined: Nov 13, 2012
Location: Baton Rouge, LA

Re: Camera Snapshot -> Copy image -> send one via email / te

But the above died on the 2nd line "embedded script: global name 'requests' is not defined

At the top of your script, you need to tell it you are using requests:
Code: Select all
import requests

Adam

Posted on
Wed May 22, 2019 4:56 pm
whmoorejr offline
User avatar
Posts: 523
Joined: Jan 15, 2013
Location: Houston, TX

Re: Camera Snapshot -> Copy image -> send one via email / te

Okay. I'm 75% there. (Steps 1-3 are good). Stuck on #4. I'm not picky on how I send an image.. but I would like to figure out a way to script it so it's in one place... versus running the script then moving to another action that uses a plugin to send the file (iMessage, Pushover, Better Email, etc.).

So this is what I have... On #4, no error, but nothing happens. If I take out the variable theFile and replace with "hello world", it works. But I want a picture.

Code: Select all
# Step 1 - Camera Snapshot to File

import requests
url = 'http://10.0.6.185:8085/cgi-bin/CGIProxy.fcgi?cmd=snapPicture2&usr=whmoorejr&pwd=donkey'

reply = requests.get(url)

reply.raise_for_status()

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 = "Backyard.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 ???

theFile = ‘/Users/williammoore/Desktop/IPCamLog/‘ + FileName

import os
cmd = """osascript<<END
tell application "Messages"
send theFile to buddy "bill.moore.jr@gmail.com" of (service 1 whose service type is iMessage)
end tell
END"""
def send_Message():
     os.system(cmd)
send_Message()


I'm guessing I need to tell the Python genie that theFile is an image and guessing that the way I'm adding a variable into the send line is probably wrong. If it's easier to script a plugin to accomplish this, I'm game.

Bill
My Plugin: My People

Posted on
Wed May 22, 2019 5:18 pm
whmoorejr offline
User avatar
Posts: 523
Joined: Jan 15, 2013
Location: Houston, TX

Re: Camera Snapshot -> Copy image -> send one via email / te

Kinda answering myself....

send image via pushover... (as a fun added bit... I created a new token with the application name "Motion" to separate these pushover messages from my indigo plugin ones)

Code: Select all
import requests
r = requests.post("https://api.pushover.net/1/messages.json", data = {
  "token": "my token number",
  "user": "my user number",
  "message": "hello world"
},
files = {
  "attachment": ("image.jpg", open("/Users/williammoore/Desktop/IPCamRotate/R.jpg", "rb"), "image/jpg")
})
print(r.text)


I'd still appreciate alternate ways... (my wife hates Pushover). Would prefer iMessage or even email to text with image.

Bill
My Plugin: My People

Posted on
Sat Jan 04, 2020 11:14 am
ryanbuckner offline
Posts: 458
Joined: Oct 08, 2011

Re: Camera Snapshot -> Copy image -> send one via email / te

did you figure out the sending image via iMessage part? I'm trying to do it too. I had it working with AppleScript

Posted on
Sat Jan 04, 2020 6:00 pm
ryanbuckner offline
Posts: 458
Joined: Oct 08, 2011

Re: Camera Snapshot -> Copy image -> send one via email / te

I wound up using python to call an action group that executes an AppleScript file:

Call the action group
Code: Select all
if (boolSuccess):
    indigo.actionGroup.execute(1116335381)


Applescript
Code: Select all
tell application "Messages"
   set targetBuddy to "ryanbuckner@gmail.com"
   set targetService to id of 1st service whose service type = iMessage
   set textMessage to POSIX file "/Users/ryanbuckner/Desktop/staffit.png"
   set theBuddy to buddy targetBuddy of service id targetService
   send textMessage to theBuddy
end tell


It's not great but it only runs once a week

Posted on
Sun Jan 05, 2020 9:35 am
whmoorejr offline
User avatar
Posts: 523
Joined: Jan 15, 2013
Location: Houston, TX

Re: Camera Snapshot -> Copy image -> send one via email / te

ryanbuckner wrote:
did you figure out the sending image via iMessage part? I'm trying to do it too. I had it working with AppleScript


working with Python.... No. Working without AppleScript, yes.

Like you, I can use a python script in my above code to call an action. Instead of having an AppleScript action, you can try the iMessage plugin to create a "Send iMsg Picture/File (iMessagePlugin Action)" to send the picture you want.

Bill
My Plugin: My People

Posted on
Tue Jan 07, 2020 3:18 pm
ryanbuckner offline
Posts: 458
Joined: Oct 08, 2011

Re: Camera Snapshot -> Copy image -> send one via email / te

Ahh, Thx. I removed the iMessage plugin in favor of the Messages plugin after getting lots of errors. I'll try again.

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests