Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to post an alert tweet with latest captured image when motion is detected by MotionEyeOS
#!/usr/bin/env python
##########################################################################
### Python Script to post a tweet when motion is detected by MotionEyeOS
### This script also attaches the latest image captured.
### IMPORTANT: Ensure that tweets are protected in the dummy account.
### run 'sudo pip install python-twitter' before execution
### Author: Sid
### https://zone13.io
### Version 1.0
##########################################################################
import twitter, time, os, glob
# Populate the Twitter API keys below
consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''
api = twitter.Api(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token_key=access_token_key,
access_token_secret=access_token_secret)
# Get current time
t = time.strftime("%d-%m-%Y %H:%M:%S", time.gmtime())
status = "Alert !! Motion detected at " + t
# Get the latest folder
folder = "/var/lib/motioneye/Camera1/" + time.strftime("%Y-%m-%d", time.gmtime())
latest_capture = max(glob.iglob(os.path.join(folder,'*.jpg')), key=os.path.getctime)
# Wait 5 seconds until the images are created in the output folder by MotionEyeOS
time.sleep(5)
# Tweet motion detection alert along with latest captured image
status = api.PostUpdate(status,media=latest_capture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.