[Python] Send video file via python

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    I need to figure out how to make a script that scans for new files in a directory, and when there is a new one, sends the file via email.

    Someone keeps stealing bikes in my apartment building! First it was my fault (I for got to lock it), now the crook upgraded by cutting chains. I had it after the crook stole my second bike by cutting 1/2 inch airplane wire.

    Anyway, using a raspberry pi as a motion activated security camera, I want it to send me a video file as soon as the video program finishes recording it. This is incase they steal the pi.

    I am looking at these examples, but I can't figure how to make the script run continuously (every minute) or how to make it scan a folder for a new file.

    How do I send attachments using SMTP?

    OK I got it down to scanning and then trying to email. It fails when trying to attach the video file. Can you help? Here is the revised code:

    The failure is:
    msg = MIMEMultipart() TypeError: 'LazyImporter' object is not callable, line 38

    import time, glob
    import smtplib
    import email.MIMEMultipart
    from email.MIMEBase import MIMEBase
    from email.MIMEText import MIMEText
    from email.Utils import COMMASPACE, formatdate
    from email import Encoders, MIMEMultipart
    import os

    #Settings:
    fromemail= "Jose Garcia <somerandomemail@gmail.com>"
    loginname="somerandomemail@gmail.com"
    loginpassword="somerandomepassword"
    toemail= "Jose Garcia <somerandomemail@gmail.com>"
    SMTPserver='smtp.gmail.com'
    SMTPort=587
    fileslocation="/Users/someone/Desktop/Test/*.mp4"
    subject="Security Notification"

    def mainloop():
    files=glob.glob(fileslocation) #Put whatever path and file format you're using in there.
    while 1:
    new_files=glob.glob(fileslocation)
    if len(new_files)>len(files):
    for x in new_files:
    if x in files:
    print("New file detected "+x)
    print("about to call send email")
    sendMail(loginname, loginpassword, toemail, fromemail, subject, gethtmlcode(), x, SMTPserver, SMTPort)

    files=new_files
    time.sleep(1)

    def sendMail(login, password, to, frome, subject, text, filee, server, port):
    # assert type(to)==list
    # assert type(filee)==list

    msg = MIMEMultipart()
    msg['From'] = frome
    msg['To'] = COMMASPACE.join(to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    # #for filee in files:
    part = MIMEBase('application', "octet-stream")
    part.set_payload( open(filee,"rb").read() )
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition', 'attachment; filename="%s"'
    % os.path.basename(filee))
    msg.attach(part)

    smtp = smtplib.SMTP(SMTPserver, SMTPort)
    smtp.sendmail(frome, to, msg.as_string() )
    server.set_debuglevel(1)
    server.starttls()
    server.ehlo()
    server.login(login, password)
    server.sendmail(frome, to, msg)
    server.quit()

    def gethtmlcode():
    print("about to assemble html")
    html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" '
    html +='"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">'
    html +='<body style="font-size:12px;font-family:Verdana"><p>A new video file has been recorded </p>'
    html += "</body></html>"
    return(html)

    #Execute loop
    mainloop()

    Continue reading...

Compartilhe esta Página