Twitter

Twitter is probably the second biggest social network on the planet. Some are hating it, others are loving it and the rest are having no idea about it! Despite that, I think that Twitter is a very good tool/service to gather information from people and organisations that you want.

And like all the internet services out there that they want to grow and succeed (in this highly competitive environment) they producing and realising API (Application Programming Interface) in order for the developers to create Apps and tools.

Twitter is no exception to that! They have made their developers page and release their REST API (currently at version 1.1)

We are going to use this API together with Python and a library called Twython (github) with our Raspberry Pi (here are the twython documentation as well). Doing so we will be able to send/delete tweets and direct messages, read timelines, gather trends and in general anything that is defined on the Twitter API!

 
 

Before we start

In ordet to start we must have a Twitter account. Then to interact with it we will need to register our application. This is accomplished from here: https://dev.twitter.com

Press the Sign In button and ender your Twitter credentials (username and password). Once you have signed in click your user icon (on the same position as the “Sign In” button) and then select My Applications. On the next screen click the Create a new application and you will have something like that:

Fill out everything that is mandatory. For website just use http://www.google.com or if you have your own website then put it here. Pay attention that the form needs the http://www in front of the url! Once you have complete this step you will presented with the following:

Select the second tab with the name Settings:

Here, in the Application Type you will choose the access level of the application to be Read, Write and Access direct messages and for the Callback URL use the one you have used before. At the bottom of the screen hit the Update this Twitter application settings.

Move again at the first tab named Detail and then at the bottom press the Create my access token:

That will send the details to Twitter and create for you the necessary keys/passwords in order to access your application. It needs a little bit of time to do that (from a few seconds to couple of minutes). Refresh your browser and when everything are ready you are going to see the following on the bottom of the same screen:

Those alphanumeric stings together with the OAuth settings will be used at our Raspberry Pi Python code that you are going to write in a minute.

 
 

Install Required Packages

Now it’s time to power your Pi and load a terminal session. For start type the usual but if you are feeling comfortable that you are running the latest OS version then you can skip this step:

sudo apt-get update
sudo apt-get upgrade

Then you have to instal the Twython:

sudo apt-get install python-pip
sudo pip install twython

After the installation finishes you are ready for the first application.

 
 

Tweet from the Pi

And now time for a little bit of code using Python. We are going to create a simple blank .py file with nano:

sudo nano SendTweet.py

And inside you have to type:

from twython import Twython, TwythonError

#Your Twitter Application keys
APP_KEY = '5q03N0REfdvpOZUXl1VIhw'
APP_SECRET = 'iHdcLT4S4QttkZGBUdJg1WYy2X8VuEOqtlWJca9T38'
OAUTH_TOKEN = '44181831-dHtTMGUSUrnFyidRYOoAGYLtQSqkDuAzf5Ssl8Vgv'
OAUTH_TOKEN_SECRET = '86IuN8zOPiCrV4LsEdAc5vIsmRMc9ZmqlRYQeIvDTahLS'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
twitter.update_status(status='Hello World!')
except TwythonError as e:
print e

Where:

APP_KEY = Your Consumer Key
APP_SECRET = Your Consumer Secret
OAUTH_TOKEN = Your Access Token
OAUTH_TOKEN_SECRET = Your Access Token Secret

Include your keys/passwords like above, check for any spelling mistakes and hit Ctrl-X from your keyboard. It will ask you if you want to save the changes. Press Y and then enter.

Now it is time to run the Python code:

sudo python SendTweet.py

If everything is OK then at your Twitter feed you are going to see something like this:

If something is wrong, for example a spelling mistake at your keys you are going to receive an error message at your terminal:

If you have this response at your terminal window then of back again by typing

sudo nano SendTweet.py

Check all the code from top to bottom for any mistakes. Correct them and follow the previous procedure!

 
 

Tweet for the terminal

Under Construction

 
 

Get your Timeline

Lets create a new Python file in order to retrieve the user timeline. Again as before:

sudo nano GetUserTimeline.py

The nano editor will open and inside you will type (you can copy paste it as well) the following:

from twython import Twython, TwythonError

#Your Twitter Application keys
APP_KEY = '5q03N0REfdvpOZUXl1VIhw'
APP_SECRET = 'iHdcLT4S4QttkZGBUdJg1WYy2X8VuEOqtlWJca9T38'
OAUTH_TOKEN = '44181831-dHtTMGUSUrnFyidRYOoAGYLtQSqkDuAzf5Ssl8Vgv'
OAUTH_TOKEN_SECRET = '86IuN8zOPiCrV4LsEdAc5vIsmRMc9ZmqlRYQeIvDTahLS'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
user_timeline = twitter.get_user_timeline(screen_name="Ioannis_Kedros")
except TwythonError as e:
print e

for tweet in user_timeline:
print(tweet['text'])

Where screen name is your Twitter name. Press Ctrl-X, then Y and enter. And now run the code:

sudo python GetUserTimeline.py

Voila! The terminal will have a few of your previous tweets for you! The number of those tweets are defined inside the get_user_timeline function. For more info and configuration have a look at the API

 
 

Send Direct Message

The final example is a direct message one. Make a new file:

sudo python SendDirectMessage.py

The type the code:

from twython import Twython, TwythonError

#Your Twitter Application keys
APP_KEY = '5q03N0REfdvpOZUXl1VIhw'
APP_SECRET = 'iHdcLT4S4QttkZGBUdJg1WYy2X8VuEOqtlWJca9T38'
OAUTH_TOKEN = '44181831-dHtTMGUSUrnFyidRYOoAGYLtQSqkDuAzf5Ssl8Vgv'
OAUTH_TOKEN_SECRET = '86IuN8zOPiCrV4LsEdAc5vIsmRMc9ZmqlRYQeIvDTahLS'

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

try:
twitter.send_direct_message(user_id='123456789', text='Hello There!!!')
except TwythonError as e:
print e

Now here is the trick. inside the send_direct_message function you must define the user_id, the one that will receive the private message. The user id is not the indicated Twitter user name but a constant number. In order to have this number out of the Twitter name you can use this web page: Twitter user ID
Type at the text box the user name and you will have the user id! Simple!

As usual Ctrl-X, then Y and finally enter.

Run the code:

sudo python SendDirectMessage.py

One thought on “Twitter

  1. Rhys says:

    fantastic submit, very informative. I ponnder why the other
    experts of this sector don’t realize this. You must
    continue your writing. I am confident, you’ve a huge readers’ base already!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: