Thursday, January 17, 2019

Watcher on Python ( with voice )

In Today`s world we are currently getting engaged in lost of automation and AI environment whether it is an mobile app or workstations. So in the same context i am sharing Watcher method based on python lang. which can help you get voice notification with email on a change/updation of data with background activity.

Watcher is term used in guard level for keeping eye on some movement and here it doing same thing on folders /share drive/website or any thing that you relate with your creative mind.

Let`s jump on the uses/advantages:

  • we can align and run the script in background with almost 0 % CPU utilisation which can allow you work on different application without any hanging issue.
  • it will keep an eye on located section so whenever data is created or modified you will get to know by notification.
  • It uses email and voice method to inform the user.
  • You can still write code and run as this script will be running in background which make it more powerful as compared to VBA/Excel/Access macro. 


Code:



import time
import pythoncom
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import win32com.client as win32
import win32com.client
speaker = win32com.client.Dispatch("SAPI.SpVoice")
pythoncom.CoInitialize()
mail = outlook.CreateItem(0)


class Watcher:
    DIRECTORY_TO_WATCH = "folder /share point path "

    def __init__(self):
        self.observer = Observer()

    def run(self):
        event_handler = Handler()
        self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
        self.observer.start()
        try:
            while True:
                time.sleep(5)
        except:
            self.observer.stop()
            print ("Error")

        self.observer.join()


class Handler(FileSystemEventHandler):

    @staticmethod
    def on_any_event(event):
        if event.is_directory:
            return None

        elif event.event_type == 'created':
            # Take any action here when a file is first created.
            print ("Received created event - %s." % event.src_path)
            pythoncom.CoInitialize()
            speaker.Speak("Hello, it works!")
            outlook = win32.Dispatch('outlook.application')
            mail = outlook.CreateItem(0)
            mail.To = 'your email id '
            mail.Subject = "New file updated" + event.src_path
            mail.Body = "text you may like to add"
            mail.Send()
                 


        elif event.event_type == 'modified':
            # Taken any action here when a file is modified.
            print ("Received modified event - %s." % event.src_path)


if __name__ == '__main__':
    w = Watcher()
    w.run()

How to use:


  1.  copy and paste the code on notepad and save as "program_name.py"
  2. create a batch script to run this application on dos directly
  3. save this batch file in you computer startup folder.



Thanks,

Keep Learning


Rohit
Believe in work not in words

No comments:

Post a Comment

JOB in 2019

Automated - Digital One o One Meetings- An Idea

                                            Automated - Digital One o One Discussion                                                       ...