From 8c505285f956510a17c4456830edecd3b3e9d137 Mon Sep 17 00:00:00 2001 From: James West Date: Sat, 2 Jul 2016 11:42:57 +0100 Subject: [PATCH 1/3] Added papirus-twitter.py --- bin/papirus-twitter.py | 141 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 bin/papirus-twitter.py diff --git a/bin/papirus-twitter.py b/bin/papirus-twitter.py new file mode 100644 index 0000000..8eb490a --- /dev/null +++ b/bin/papirus-twitter.py @@ -0,0 +1,141 @@ +# !/usr/bin/env python +# coding: utf-8 +# ------------------------------------------------------ +# Filename twitterbuttons.py +# ------------------------------------------------------ +# Displays Tweets on PaPiRus Zero display +# +# v1.0 by James West June 2016 +# +# I used the Twython tutorials at tecoed.co.uk +# to get me started +# ------------------------------------------------------ + +# import libraries to make it work +import os +import re +import time +import RPi.GPIO as GPIO +from twython import Twython +from papirus import Papirus +from papirus import PapirusText + +# set up PaPiRus +screen = Papirus() +text = PapirusText() + +#Twitter authorisation keys - add your own here +CONSUMER_KEY = 'YOUR CONSUMER KEY HERE' +CONSUMER_SECRET = 'YOUR CONSUMER SECRET HERE' +ACCESS_KEY = 'YOUR ACCESS KEY HERE' +ACCESS_SECRET = 'YOUR ACCESS SECRET HERE' + +api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) + +# Identifies the GPIOs that the buttons operate on +SW1 = 26 +SW2 = 19 +SW3 = 20 +SW4 = 16 +SW5 = 21 + +def main(): + GPIO.setmode(GPIO.BCM) + + GPIO.setup(SW1, GPIO.IN) + GPIO.setup(SW2, GPIO.IN) + GPIO.setup(SW3, GPIO.IN) + GPIO.setup(SW4, GPIO.IN) + GPIO.setup(SW5, GPIO.IN) + +# Writes the menu to the PaPiRus - 14 is the font size + text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n5 = Off', 14) + while True: + if GPIO.input(SW1) == False: + +# Put the Twitter usernames of the news organisations you want to read here + twits = ["BBCNews", "GranadaReports", "SkyNews", "itvnews", "MENnewsdesk"] + for index in range (len(twits)): + +# The count parameter defines how many tweets from each account you'll read + tweets = api.get_user_timeline(screen_name=twits[index], count=4) + for tweet in tweets: + clean_tweet = '%s: %s' % ( tweet['user']['screen_name'].encode('utf-8'), + tweet['text'].encode('utf-8')) + +# These lines clear URLs from the tweets and tidies up other elements the display +# doesn't seem to like + clean_tweet = re.sub(r"(https?\://|http?\://|https?\:)\S+", "", clean_tweet) + clean_tweet = re.sub(r"&", "&", clean_tweet) + clean_tweet = re.sub(r"&horbar|&hyphen|&mdash|&ndash", "-", clean_tweet) + clean_tweet = re.sub(r"&apos|&rsquo|&rsquor|&prime", "'", clean_tweet) + clean_tweet = re.sub(r"£", "", clean_tweet) + + text.write(clean_tweet, 14) + +# Sets how many seconds each tweet is on screen for + time.sleep(5) + + text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n5 = Off', 14) + + if GPIO.input(SW2) == False: +# usernames of the weather accounts you'll be using + twitweather = ["mcrweather", "Manches_Weather"] + for index in range (len(twitweather)): + tweets = api.get_user_timeline(screen_name=twitweather[index], count=1) + for tweet in tweets: + clean_tweet = '%s: %s' % ( tweet['user']['screen_name'].encode('utf-8'), + tweet['text'].encode('utf-8')) + + clean_tweet = re.sub(r"(https?\://|http?\://)\S+", "", clean_tweet) + clean_tweet = re.sub(r"&", "&", clean_tweet) + clean_tweet = re.sub(r"&horbar|&hyphen|&mdash|&ndash", "-", clean_tweet) + clean_tweet = re.sub(r"&apos|&rsquo|&rsquor|&prime", "'", clean_tweet) + clean_tweet = re.sub(r"£", "", clean_tweet) + text.write(clean_tweet, 14) + time.sleep(8) + + text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n5 = Off', 14) + + if GPIO.input(SW3) == False: +# Gets your home timeline + tweets = api.get_home_timeline(screen_name='jameswest', count=20) + for tweet in tweets: + clean_tweet = '%s: %s' % ( tweet['user']['screen_name'].encode('utf-8'), + tweet['text'].encode('utf-8')) + + clean_tweet = re.sub(r"(https?\://|http?\://)\S+", "", clean_tweet) + clean_tweet = re.sub(r"&", "&", clean_tweet) + clean_tweet = re.sub(r"&horbar|&hyphen|&mdash|&ndash", "-", clean_tweet) + clean_tweey = re.sub(r"&apos|&rsquo|&rsquor|&prime", "'", clean_tweet) + clean_tweet = re.sub(r"£", "", clean_tweet) + text.write(clean_tweet, 14) + time.sleep(5) + + text.write('1 = News\n2 = Weather\n3 = My timeline\n4 = My mentions\n5 = Off', 14) + + if GPIO.input(SW4) == False: +# gets your mentions + tweets = api.get_mentions_timeline(screen_name='jameswest', count=5) + for tweet in tweets: + clean_tweet = '%s: %s' % ( tweet['user']['screen_name'].encode('utf-8'), + tweet['text'].encode('utf-8')) + + clean_tweet = re.sub(r"(https?\://|http?\://)\S+", "", clean_tweet) + clean_tweet = re.sub(r"&", "&", clean_tweet) + clean_tweet = re.sub(r"&horbar|&hyphen|&mdash|&ndash", "-", clean_tweet) + clean_tweey = re.sub(r"&apos|&rsquo|&rsquor|&prime", "'", clean_tweet) + clean_tweet = re.sub(r"£", "", clean_tweet) + text.write(clean_tweet, 14) + time.sleep(5) + + text.write('1 = News\n2 Weather\n3 = My timeline\n4 = My mentions\n5 = Off', 14) + + if GPIO.input(SW5) == False: +# Says goodbye, clears the screen and shuts the Pi down + text.write('Goodbye...') + text.write(' ') + os.system("sudo shutdown -h now") + +if __name__ == '__main__': + main() From 4f9abae0abff6704ab407d335928e36855233953 Mon Sep 17 00:00:00 2001 From: James West Date: Tue, 5 Jul 2016 09:12:30 +0100 Subject: [PATCH 2/3] Update papirus-twitter.py --- bin/papirus-twitter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/papirus-twitter.py b/bin/papirus-twitter.py index 8eb490a..3ba9f19 100644 --- a/bin/papirus-twitter.py +++ b/bin/papirus-twitter.py @@ -1,7 +1,7 @@ # !/usr/bin/env python # coding: utf-8 # ------------------------------------------------------ -# Filename twitterbuttons.py +# Filename papirus-twitter.py # ------------------------------------------------------ # Displays Tweets on PaPiRus Zero display # From c514c80dff0329ed4d0ba81cc21d296702a9cb54 Mon Sep 17 00:00:00 2001 From: James West Date: Tue, 5 Jul 2016 09:13:52 +0100 Subject: [PATCH 3/3] Update papirus-twitter.py --- bin/papirus-twitter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/papirus-twitter.py b/bin/papirus-twitter.py index 3ba9f19..b527345 100644 --- a/bin/papirus-twitter.py +++ b/bin/papirus-twitter.py @@ -24,7 +24,7 @@ screen = Papirus() text = PapirusText() -#Twitter authorisation keys - add your own here +# Twitter authorisation keys - add your own here CONSUMER_KEY = 'YOUR CONSUMER KEY HERE' CONSUMER_SECRET = 'YOUR CONSUMER SECRET HERE' ACCESS_KEY = 'YOUR ACCESS KEY HERE'