Itchy
Posts: 27
Joined: Tue Feb 25, 2014 9:55 pm

How do I always select the latest photo?

Thu Mar 20, 2014 9:03 pm

Hi,

I'm using motion, a webcam and a simple python script to create a security system that will email me the latest photo when motion is detected. Main problem is I can't get the program to send me the latest photo (it's sending me the same photo every time). I have to set the 2 parameters which are text for the body of email and file to attach as per below. What can I add to my script to dispense with the need for adding a second parameter so it will always just email the latest jpeg.

python sendemailattach.py 'body of text', '/home/pi/webcam/photo.jpg'

Code: Select all

#!/usr/bin/env python
"""
Home Automation: sends an e-mail with a photograph attachment
For the Raspberry Pi
"""

import sys
import smtplib
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart

def emailphoto(msgtext, afilename):
        #enter the e-mail account username between the quotes
        smtp_user = "[email protected]"
        
        #enter the e-mail account password between the quotes
        smtp_pass = "sunders2006" 
        msg = MIMEMultipart() 
        
        #enter the target e-mail address between the quotes
        msg['To']   = "[email protected]" 
        
        #enter the e-mail account username between the quotes
        msg['From'] = "Valdo" 
        
        #enter the message subject between the quotes
        msg['Subject'] = "Hello" 
        
        #That is what you see if don't have an e-mail reader:     
        msg.preamble = 'Multipart message.\n'  
        
        #sys.argv[1] is the 1st parameter that is passed to this 
        #and it contains the text for the body of the e-mail
        part = MIMEText(msgtext) 
        msg.attach(part)
        
        
        #The next 3 lines attach the photo using the filename 
        #passed in as the second parameter to this program
        part = MIMEApplication(open(afilename,"rb").read())
        part.add_header('Content-Disposition', 'attachment', filename=afilename)
        msg.attach(part)
        
        #enter the SMPT server URL or IP Address between the quotes
        s = smtplib.SMTP_SSL("smtp.gmail.com", 465) 
        s.login(smtp_user,smtp_pass)
        s.sendmail(msg['From'], msg['To'], msg.as_string())
        s.quit()

def main():
    emailphoto(sys.argv[1], sys.argv[2])

if __name__ == "__main__":
        main()

wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: How do I always select the latest photo?

Fri Mar 21, 2014 2:48 am

I don't know my way around the python. What you need to do is:
1) search for all the files matching some criteria (eg. in a particular directory, matching a particular filename pattern
2) put them into date order
3) pick out the last one (or, if you put them into newest first, then the first one).

You can do this on the command line with:

Code: Select all

cd /home/pi/webcam; python sendemailattach.py 'body of text', `ls -t *.jpg | head -1`
The ls command only returns the filename, not the full pathname, so it'll end up passing photo.jpg, rather than /home/pi/webcam/photo.jpg. There are ways around this (specify the directory in the ls command *and* then add it as a prefix to the returned filename), but I've taken the shorter approach of simply changing directory before running the command. This could mean that you need to specify the path to the python script instead.

Itchy
Posts: 27
Joined: Tue Feb 25, 2014 9:55 pm

Re: How do I always select the latest photo?

Fri Mar 21, 2014 8:57 pm

Nope. That didn't work. In fact it seems that nothing really happens. All i get is the greater than sign >

Any other ideas? This is really stressing me out as the entire project is almost there but I can't quite get this last bit right.

User avatar
redhawk
Posts: 3465
Joined: Sun Mar 04, 2012 2:13 pm
Location: ::1

Re: How do I always select the latest photo?

Fri Mar 21, 2014 9:03 pm

Why don't you create your filenames appended with an incremental counter i.e. photo_0001.jpg, photo_0002.jpg
Then when you need to select the latest photo just scan all files from the storage area and find the file with the highest appended value (or something like that).

Richard S.

hk1963
Posts: 25
Joined: Fri Dec 06, 2013 6:06 pm

Re: How do I always select the latest photo?

Fri Mar 21, 2014 9:53 pm

With regards to the problem of keeping track of some "latest" photo:

I have some application which takes photos at different times and stores them in files having a timestamp in the filename, like
pic-20140321-184512.jpg
To keep accessing the "latest" pic simple, I just keep a symbolic link with a name like "latest.jpg" to the latest file I created.

ln -s -f pic-20140321-184512.jpg latest.jpg

If next time a new file is created with name e.g. pic-20140321-192551.jpg, calling

ln -s -f pic-20140321-192551.jpg latest.jpg

will now let point "latest.jpg" to the new file.
This way, I can always access my latest file simply by referring to "latest.jpg".

Itchy
Posts: 27
Joined: Tue Feb 25, 2014 9:55 pm

Re: How do I always select the latest photo?

Sat Mar 22, 2014 12:13 am

Ok that sounds really promising as I can create the photos with a date and time stamp. I really am a newbie though. Sorry. I don't know how to create that symbolic link. Where would I type that code in my python script? What would I type exactly if my files follow the same naming convention?

wimble
Posts: 34
Joined: Tue Feb 05, 2013 9:52 am

Re: How do I always select the latest photo?

Sat Mar 22, 2014 12:55 am

Itchy wrote:Nope. That didn't work. In fact it seems that nothing really happens. All i get is the greater than sign >

Any other ideas? This is really stressing me out as the entire project is almost there but I can't quite get this last bit right.
Did you cut and paste exactly what I'd put into the post?

Code: Select all

cd /home/pi/webcam; python sendemailattach.py 'body of text', `ls -t *.jpg | head -1`
Those are back quotes around the ls... head-1 command. What this does is run that command (which lists all the jpg files, in time order, and then return the first line of the list), and the backquotes then put the results into the command itself. If I leave off the last backquote, then I get the same > prompt that you mention, where the shell is asking me to continue the string and then close the quotes.

hk1963
Posts: 25
Joined: Fri Dec 06, 2013 6:06 pm

Re: How do I always select the latest photo?

Sat Mar 22, 2014 6:31 am

Itchy wrote:Ok that sounds really promising as I can create the photos with a date and time stamp. I really am a newbie though. Sorry. I don't know how to create that symbolic link. Where would I type that code in my python script? What would I type exactly if my files follow the same naming convention?
Sorry, this here is just the command I use in a shell script. I haven't done any Phython programming yet so can't tell you, how to do it in Python. But there surely is a way to do it. Googling for "Python create symbolic link" (without the quotes) says that in Python you have a os.symlink(). I don't know how equivalent it is to ln -s -f, especially the -f option as this deals with the situation when the symbolic link already exists. Without the -f you get an error, with -f it simply changes the link to the new file. Without -f you would first have to remove the old link. To avoid another possible error situation, it should also be put into account that no symbolic link is existing the first time.

User avatar
DougieLawson
Posts: 35602
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: How do I always select the latest photo?

Sat Mar 22, 2014 8:18 am

In python you can use os.symlink(source, link_name)
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

Itchy
Posts: 27
Joined: Tue Feb 25, 2014 9:55 pm

Re: How do I always select the latest photo?

Sat Mar 22, 2014 5:43 pm

I think symlink is definitely what I need (the other method doesn't appear to work). Sorry for such newbie question but what code would i need to type either at command line on in my python script to use symlink?

I'm using motion and there is a comment in the .conf. file which says: 'Note: A symbolic link called lastsnap.jpg created in the target_dir will always point to the latest snapshot, unless snapshot_filename is exactly 'lastsnap'

How do i create that symbolic link in the target directory?

Return to “Troubleshooting”