Skip to content

Instantly share code, notes, and snippets.

@stetro
Created October 28, 2012 11:51
Show Gist options
  • Save stetro/3968412 to your computer and use it in GitHub Desktop.
Save stetro/3968412 to your computer and use it in GitHub Desktop.
Raspberry PI GPIO Twitter and Mail Notifier
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Status
from threading import Thread
import RPi.GPIO as GPIO
import feedparser
import time
import commands
# READ LAST TWEET ID FORM FILE
def read_last_tweet_id():
tweet_id = 0
fobj = open(".lasttweetid", "r")
for line in fobj:
tweet_id = int(line)
fobj.close()
return tweet_id
# SAVE LAST TWEET ID TO FILE
def save_last_tweet_id(last_tweet_id):
fobj = open(".lasttweetid", "w")
fobj.write(str(last_tweet_id))
fobj.close()
# AUTHENTICATE WITH TWITTER API
def tweepy_auth():
consumer_key="..."
consumer_secret="..."
access_token="..."
access_token_secret="..."
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
return auth;
# LOAD NEWEST STATES FROM PERSONAL TIMELINE
def load_newest_states(auth,last_tweet_id):
api = API(auth)
if(last_tweet_id==0):
states = api.home_timeline()
else:
states = api.home_timeline(last_tweet_id)
return states
# CHECK FOR NEW TWEETS BY THE LAST TWEET ID AND GIVES NUMBER OF NEW TWEETS
def check_for_new_tweets():
last_tweet_id = read_last_tweet_id()
auth = tweepy_auth()
states = load_newest_states(auth,last_tweet_id)
for status in states:
if(last_tweet_id<status.id):
last_tweet_id=status.id
save_last_tweet_id(last_tweet_id)
return str(len(states))
# CHECK FOR NEW MAILS WITH ATOM FEED AND GIVES NUMBER OF NEW TWEETS
def check_for_new_mails():
fobj = open(".mailpassword", "r")
for line in fobj:
PASSWORD = str(line).rstrip()
fobj.close()
USERNAME = "" #Account hier einfuegen
feed = feedparser.parse("https://" + USERNAME + ":" + PASSWORD + "@mail.google.com/mail/feed/atom")
newmails = int(feed['feed']['fullcount'])
return newmails
if __name__ == '__main__':
GPIO.setmode(GPIO.BCM)
GPIO.setup(0, GPIO.OUT)
GPIO.setup(1, GPIO.OUT)
MAIL_LED = 0
TWEET_LED = 1
tweets = 0
while True:
tweets = check_for_new_tweets()
mails = check_for_new_mails()
print str(tweets) + " new tweets"
print str(mails) + " new mails"
if(int(tweets) == 0):
GPIO.output(TWEET_LED, GPIO.HIGH)
else:
GPIO.output(TWEET_LED, GPIO.LOW)
if(int(mails) == 0):
GPIO.output(MAIL_LED, GPIO.HIGH)
else:
GPIO.output(MAIL_LED, GPIO.LOW)
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment