2014-09-04 111 views
-3

我需要弄清楚如何創建一個腳本來掃描目錄中的新文件,當有新文件時,通過電子郵件發送文件。通過python發送視頻文件

有人在我的公寓樓裏偷自行車!首先,這是我的錯(我要鎖定它),現在是通過切割鏈升級的騙子。在騙子偷走我的第二輛自行車時,我用1/2英寸的飛機電線割下了它。

無論如何,使用樹莓派作爲運動激活的安全攝像頭,我希望它在視頻節目完成錄製後立即向我發送視頻文件。這是他們偷了pi。

我正在看這些例子,但我不知道如何使腳本連續運行(每分鐘)或如何使它掃描一個新文件的文件夾。

How do I send attachments using SMTP?

OK 我得到它下降到掃描,然後試圖電子郵件。嘗試附加視頻文件時失敗。你能幫我嗎?下面是修改後的代碼:

失敗是:
味精= MimeMultipart的() 類型錯誤: 'LazyImporter' 對象不是可調用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 <[email protected]>" 
loginname="[email protected]" 
loginpassword="somerandomepassword" 
toemail= "Jose Garcia <[email protected]>" 
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() 
+0

更好的,但到底是什麼「它失敗「是什麼意思?它發送[Rick Astley]的視頻(http://en.wikipedia.org/wiki/Rickrolling)?它會引發錯誤?什麼是錯誤信息? – 2014-09-04 17:00:57

+0

我再次修改它,減少混亂。代碼仍然失敗,「失敗是: msg = MIMEMultipart()TypeError:'LazyImporter'對象不可調用,第38行」 – 2014-09-04 17:44:54

+0

請告訴我們什麼'LazyImporter'和「行38」是。 – 2014-09-04 20:53:36

回答

0

只要把你的腳本一個循環,讓它睡60秒。您可以使用glob獲取目錄中的文件列表。 in對於查看列表中的內容(即目錄中的文件列表)非常有用。

import time, glob 

files=glob.glob("/home/me/Desktop/*.mp4") #Put whatever path and file format you're using in there. 

while 1: 
    new_files=glob.glob("/home/me/Desktop/*.mp4") 
    if len(new_files)>len(files): 
     for x in new_files: 
      if x not in files: 
       print("New file: "+x) #This is where you would email it. Let me know if you need help figuring that out. 
    files=new_files 
    time.sleep(60) 
+0

做比60秒更頻繁的時間間隔不會受傷。它根本不應該消耗你的處理器。一秒鐘就會好過。 – Shule 2014-09-04 06:05:56

+0

我把它歸結爲掃描並執行電子郵件發件人,但未能加載有效負載。你能看到缺少的東西嗎? – 2014-09-04 16:02:11

1

我終於搞定了。我不得不使用python 2.5來擺脫LazyImporter錯誤。每次將新文件添加到安全文件夾時,它都會通過電子郵件發送給我。日誌記錄已損壞。

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


#Settings: 
fromemail= 'RaspberryPI Security Camera <someone>' 
loginname='[email protected]' 
loginpassword='something' 
toemail= 'Jose Garcia < [email protected]>' 
SMTPserver='smtp.gmail.com' 
SMTPort=587 
fileslocation='/Users/someone/Desktop/Test/*.*' 
subject="Security Notification" 
log='logfile.txt' 

def mainloop(): 
     files=glob.glob(fileslocation) #Put whatever path and file format you're using in there. 
     while 1: 
      f = open(log, 'w') 
      sys.stdout = Tee(sys.stdout, f) 
      new_files=glob.glob(fileslocation) 
      if len(new_files)>len(files): 
       for x in new_files: 
        if x in files: 
         print(str(datetime.datetime.now()) + "New file detected "+x) 
         sendMail(loginname, loginpassword, toemail, fromemail, subject, gettext(), x, SMTPserver, SMTPort) 
      files=new_files 
      f.close() 
      time.sleep(1) 

def sendMail(login, password, send_to, send_from, subject, text, send_file, server, port): 

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

    msg.attach(MIMEText(text)) 

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

    smtp = smtplib.SMTP(SMTPserver, SMTPort) 
    smtp.set_debuglevel(1) 
    smtp.ehlo() 
    smtp.starttls() 
    smtp.login(login, password) 
    smtp.sendmail(send_from, send_to, msg.as_string()) 
    smtp.close() 


def gettext(): 
    text = "A new file has been added to the security footage folder. \nTime Stamp: "+ str(datetime.datetime.now()) 
    return(text) 

class Tee(object): 
    def __init__(self, *files): 
     self.files = files 
    def write(self, obj): 
     for f in self.files: 
      f.write(obj) 


#Execute loop 
mainloop()