What is it?This simple guide will get you started with Twitter and the Raspberry Pi. You will be able to send a Tweet, stream your timeline and pull down your mentions, all from the terminal window in your Raspberry Pi.
Getting Started?Firstly pop over to the Twitter API site and register your details. This is required to create the Authentication Token and Access Token.
These KEYS will be unique to you, keep them secure as they will be used in the Python code to stream the Twitter feed.
Ensure that the under the application settings the Application Access Type is set to read and write, this will allow posting and reading timelines.
Once you have created your own account and you have the authorisation codes and tokens the program can be created.
First Update the Raspberry Pi sudo apt-get update Upgrade your Pi sudo apt-get upgrade Next install the python setup tools and pip, use this to install the Twython module that will allow the communication between the twitter API and Python. sudo apt-get install python-setuptools sudo easy_install twython or sudo pip install twython |
Using TwitterThere are three basic commands that are used to stream your tweets, your timeline and mentions.
Sending a tweet: api.update_status(status=say) Your timeline: api.get_home_timeline(screen_name='your_screen_name') Your mentions: api.get_mentions_timeline() Sending a Tweet###From @TeCoEd###
#!/usr/bin/env python import sys from twython import Twython CONSUMER_KEY = 'replace with yours' CONSUMER_SECRET = 'replace with yours' ACCESS_KEY = 'replace with yours' ACCESS_SECRET = 'replace with yours' api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) say = raw_input("What is your tweet? ") api.update_status(status=say) #sends the tweet Save the python program, for example tweet.py and launch the LX terminal, run the code using sudo python tweet.py Streaming your Timeline###From @TeCoEd###
#!/usr/bin/env python import sys from twython import Twython CONSUMER_KEY = 'replace with yours' CONSUMER_SECRET = 'replace with yours' ACCESS_KEY = 'replace with yours' ACCESS_SECRET = 'replace with yours' api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) tweets = api.get_home_timeline(screen_name='your_screen_name') while True: for tweet in tweets: print(tweet['text']) time.sleep(5) Save the python program, for example timeline.py and launch the LX terminal, run the code using sudo python timeline.py Getting your Mentions###From @TeCoEd###
#!/usr/bin/env python import sys from twython import Twython CONSUMER_KEY = 'replace with yours' CONSUMER_SECRET = 'replace with yours' ACCESS_KEY = 'replace with yours' ACCESS_SECRET = 'replace with yours' api = Twython(CONSUMER_KEY,CONSUMER_SECRET,ACCESS_KEY,ACCESS_SECRET) tweets = api.get_mentions_timeline() while True: for tweet in tweets: print(tweet['text']) time.sleep(2) Save the python program, for example mentions.py and launch the LX terminal, run the code using sudo python mention.py |
- Home
- Python
- Ras Pi
-
Pi Hardware
- Pi-Hacks
-
Pi-Hacks 2
- The Joker
- Hologram Machine
- Google Vision: Camera Tell
- Yoda Tweets
- Pi Phone
- Darth Beats
- Twitter Keyword Finder
- Crimbo Lights Hack
- Xmas Elf
- Halloween 2016
- Halloween Hack 2015
- Socrative Zombie
- Voice Translation
- The Blue-Who Finder
- GPIO, Twitter
- Pi Chat Bot >
- PiGlow & Email
- Pibrella Alarm System
- SMS with Python >
- Pi-Hacks 3
- Minecraft
- Computing
- Contact Me
- Random Hacks