objective-see / DoNotDisturb

Detect Evil Maid Attacks
GNU General Public License v3.0
294 stars 43 forks source link

Run python script from DND? #29

Open kodsama opened 1 year ago

kodsama commented 1 year ago

Hello everyone,

Sorry for the spam but I couldn't see where to post that.

I made a python script using Pushover with image capture to be run by DND but I get a:

2023-04-09 02:57:55 +0000: failed to execute python3 /Users/me/DoNotDisturb/dnd.py

Any idea why I can't run the script? (I tried from a terminal locally and it runs without problem, but DND can't run it for some reason)

The script:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Kodsama

import requests
import subprocess
import time
import cv2
import numpy as np
import logging

logging.basicConfig(level=logging.INFO)

#Edit these variables
appToken = ''     # Pushover app token
userToken = ''    # Pushover user token
logFile = '/Library/Objective-See/DND/DND.log'  # DND logfile path
imgFile = '/Users/me/DoNotDisturb/DND.jpg'    # Snapshot path
wait = 0.01 # Waiting time in seconds
pics = 10   # Number of snapshots to take

# Don't edit from there
logging.info('Script start')

title = time.strftime("%c")
proc = subprocess.Popen(['tail', logFile], stdout=subprocess.PIPE)
log = proc.stdout.read()

frameWidth = 640
frameHeight = 480
cap = cv2.VideoCapture(0)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10,150)
time.sleep(0.5) # Make sure everything is settled

logging.info('Camera set-up done')

success = False
try:
    if cap.isOpened():
        logging.info('Cheese :D')
        success, img = cap.read()
        for _ in range(pics):
            success, img2 = cap.read()
            img = np.vstack((img, img2))
            time.sleep(wait)
finally:
    cap.release()
logging.info(f'Snapshot done: success={success}')

if success:
    logging.info(f'Write snapshot')
    cv2.imwrite(imgFile, img)
    logging.info(f'Send Pushover with snapshot')
    r = requests.post("https://api.pushover.net/1/messages.json", data = {
    "token": appToken,
    "user": userToken,
    "message": f"Do Not Disturb alert\n{title}\n{log}"
    },
    files = {
    "attachment": ("snapshot.jpg", open(imgFile, "rb"), "image/jpeg")
    })
else:
    logging.warning(f'Send Pushover without snapshot')
    r = requests.post("https://api.pushover.net/1/messages.json", data = {
    "token": appToken,
    "user": userToken,
    "message": f"Do Not Disturb alert\n{title}\n{log}"
    })

logging.info(f'Script done!')
wiselancer commented 1 month ago

@kodsama nice idea. I'll love to modify a little bit and use it. Btw I'm having an issue "App is not available in your region" my regions is United States. Maybe you had the same issue and now workaround?