Skip to content

Instantly share code, notes, and snippets.

@jamesridgway
Last active December 1, 2023 20:22
Show Gist options
  • Save jamesridgway/3597d27eab96ff6292802aff4780d80d to your computer and use it in GitHub Desktop.
Save jamesridgway/3597d27eab96ff6292802aff4780d80d to your computer and use it in GitHub Desktop.
import asyncio
import logging
import sys
import pyinotify
# Configure the logger
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
logger.addHandler(console_handler)
class EventHandler(pyinotify.ProcessEvent):
def process_default(self, event):
if event.pathname.startswith('/dev/video'):
if event.mask & pyinotify.IN_OPEN:
logger.info("Webcam is being used!")
elif event.mask & pyinotify.IN_CLOSE_WRITE \
or event.mask & pyinotify.IN_CLOSE_NOWRITE \
or event.mask & pyinotify.IN_DELETE_SELF:
logger.info("Webcam stopped being used!")
async def main():
wm = pyinotify.WatchManager()
mask = pyinotify.IN_CLOSE_WRITE | pyinotify.IN_OPEN
handler = EventHandler()
notifier = pyinotify.Notifier(wm, handler)
wm.add_watch('/dev', mask, rec=True)
try:
logger.info("Watching for changes in /dev directory...")
notifier.loop()
except KeyboardInterrupt:
logger.info("Stopping watch.")
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment