コード例 #1
0
ファイル: send_email.py プロジェクト: victorvarza/hermes
    def sendMessage(self, fpath, folder):
        try:
            fname = ntpath.basename(fpath)

            Logs.Print("Sending email for " + folder + " and file:    " +
                       str(fname))

            msg = MIMEMultipart()
            msg['Subject'] = "ALARM DETECTED ON " + folder
            msg['From'] = "ALERT! " + "<" + self.mail_from + ">"
            msg['To'] = self.mail_to

            part = MIMEBase('application', "octet-stream")
            part.set_payload(open(fpath, "rb").read())
            encoders.encode_base64(part)
            part.add_header('Content-Disposition',
                            'attachment; filename="' + fname + '"')
            msg.attach(part)

            server = SMTP(self.smtp_server, timeout=10)
            server.set_debuglevel(0)

            #server.login(self.username, self.password)
            server.sendmail(msg['From'], msg['To'], msg.as_string())
            Logs.Print('Message sent')

        except Exception as e:
            Logs.Print("Exception: " + str(e))

        finally:
            if server:
                server.quit()
コード例 #2
0
    def app(self):

        Logs.Print("Starting Hermes App..")

        Logs.Print("Paths to monitor:")
        for monitor_path in self.conf_data['monitor_paths']:
            Logs.Print("{0}: {1}".format(monitor_path['name'],
                                         monitor_path['path']))

        Logs.Print("Archive path: {0}".format(self.conf_data['archive_path']))

        while 1:
            try:
                for monitor_path in self.conf_data['monitor_paths']:

                    # Check if there are any files in these paths
                    files = self.file_system.list_files(monitor_path['path'])

                    # check if the files are ok to be processed
                    for file in files:
                        if os.path.getsize(
                                file) < self.conf_data['file_size_min']:
                            current_time = time.time()
                            creation_time = os.path.getctime(file)
                            if (
                                    current_time - creation_time
                            ) < 60:  # older than 1 minutes are ok to be processed
                                Logs.Print(
                                    "File: {0} is too small to be processed.".
                                    format(file))
                                files.remove(file)

                    # wait 2 seconds
                    time.sleep(2)

                    if files:
                        time_now = datetime.datetime.utcnow()
                        print("\n\n")
                        Logs.Print(
                            "======================================================"
                        )
                        Logs.Print(
                            str(time_now) + ":    Found alarm on " +
                            monitor_path['name'])

                        # Create google_drive object
                        google_drive = MyGoogleDrive(
                            self.conf_data['gdrive_cred_path'])

                        # Upload images into google drive
                        for file in files:
                            google_drive.push(file,
                                              monitor_path['gdrive_folder_id'])

                        self.mail.sendMessage(file, monitor_path['name'])
                        self.file_system.archive_files(files,
                                                       monitor_path['name'])

            except Exception as e:
                Logs.Print("Exception: " + str(e))
コード例 #3
0
ファイル: google_drive.py プロジェクト: victorvarza/hermes
 def cleanup(self, days, gdrive_folder_ids):
     try:
         date_N_days_ago = datetime.now() - timedelta(days=days)
         files = self.drive.ListFile({
             "q":
             "trashed = false and modifiedDate < '" +
             str(date_N_days_ago).split(' ')[0] + "'",
             "maxResults":
             1000
         }).GetList()
         for file in files:
             if file['id'] not in gdrive_folder_ids:
                 Logs.Print(file['title'] + " will be deleted")
                 file.Delete()
     except Exception as e:
         Logs.Print("Exception: " + str(e))
コード例 #4
0
ファイル: file_system.py プロジェクト: victorvarza/hermes
    def archive_files(self, files, folder):
        try:
            Logs.Print("Archiving files to " + folder)

            for file in files:
                oldfile = file
                file_date = str(self.get_creation_date(file))
                newfile = self.archive + "/" + str(file_date).replace(
                    ' ', '_').replace('-', '_').replace(
                        ':', '_') + "_" + folder + ".jpg"

                Logs.Print("Archiving file: " + oldfile + " to " + newfile)
                os.rename(oldfile, newfile)

        except Exception as e:
            Logs.Print("Exception: " + str(e))
コード例 #5
0
    def __lock_pid(self):
        self.pid = str(os.getpid())
        if os.path.isfile(self.pidfile):
            Logs.Print("%s already exists, exiting" % self.pidfile)
            sys.exit()

        with open(self.pidfile, 'w') as fwrie:
            fwrie.write(self.pid)
コード例 #6
0
ファイル: google_drive.py プロジェクト: victorvarza/hermes
 def push(self, file_path, folder_id):
     try:
         filename = os.path.basename(file_path)
         Logs.Print('Uploading file: ' + str(filename) +
                    " on gdrive folder: " + str(folder_id))
         textfile = self.drive.CreateFile({
             'title':
             filename,
             'mimeType':
             'image/jpg',
             "parents": [{
                 "kind": "drive#fileLink",
                 "id": folder_id
             }]
         })
         textfile.SetContentFile(file_path)
         textfile.Upload()
     except Exception as e:
         Logs.Print("Exception: " + str(e))
コード例 #7
0
    def cleanup(self):

        Logs.Print("Starting Hermes Cleanup..")

        gdrive_folder_ids = []
        retention = self.conf_data['retention']
        for folder_id in self.conf_data['monitor_paths']:
            gdrive_folder_ids.append(folder_id)

        while 1:
            try:
                Logs.Print("Start cleaning process.")

                self.file_system.cleanup_archive(retention)
                google_drive = MyGoogleDrive(
                    self.conf_data['gdrive_cred_path'])
                google_drive.cleanup(retention, gdrive_folder_ids)

                Logs.Print("Stop cleaning process.")

                time.sleep(86400)
            except Exception as e:
                Logs.Print("Exception: " + str(e))