Skip to content

Instantly share code, notes, and snippets.

@zone13
Created March 27, 2017 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save zone13/48a4a918331eda4be50afed6eb5f4f2c to your computer and use it in GitHub Desktop.
Save zone13/48a4a918331eda4be50afed6eb5f4f2c to your computer and use it in GitHub Desktop.
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