sábado, 10 de marzo de 2012

Internet of things: Sending data to Twitter using Python - part I

Abstract


Continuing with the goal of sending data using different means to connect a microcontroller to the outside world, this time we are going to describe how to interact with twitter as a way to show messages triggered by conditions defined and showing values of any kind of sources.

This article is divided in 2 parts. The first one is intended to show you how to send data from our computer to twitter. The second one is going to be dedicated to explains the procedure that have to be followed to send data produced by different sources to Twitter.

See the video:





Sending data to Twitter - part I

What we want to do is obtain data from a source (i.e. a sensor), then analyze it to trigger messages depending on specific conditions. With this goal in mind the first step is to know how the messaging system of twitter actually works.

In order to send messages  to twitter from command line it is not enough to use curl or whatever command line tool to send messages over internet. Instead, Twitter clients will need to use more secure authentication based on OAuth. OAuth is an authentication protocol that allows users to approve application to act on their behalf without sharing their password.

So this first part is going to help people to know what is the lifecycle to get ready for sending messages.

(1) Download Tweepy

To interact easily with Twitter from Python, is imperative to get in hand an API that can be used to get an abstraction, this way is easier to program what we need. It is worth to say that Tweepy is a very well documented Twitter library for Python. This is the python API that we will use to authenticate against twitter and send messages.

(2) Create the application on  https://dev.twitter.com/apps

Here you can create as much as applications as you need. This step will provide you with two authentication pieces required for the next step.


CONSUMER_KEY
CONSUMER_SECRET

It is important to remember to configure the application to Read, Write and Access direct messages located on settings tab.


(3) Authenticate against the application created in the above step.

This step engage with the authentication procedure of twitter and will provide you with another two pieces required to send the messages.


ACCESS_KEY
ACCESS_SECRET

The following script do this engage, just replace key and secret.


#!/usr/bin/python


import tweepy
CONSUMER_KEY = 'put here the consumer key'
CONSUMER_SECRET = 'put here de consumer secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth_url = auth.get_authorization_url()
print 'Use this URL to get the PIN: ' + auth_url
PIN = raw_input('PIN?: ').strip()
auth.get_access_token(PIN)
print "ACCESS_KEY = '%s'" % auth.access_token.key
print "ACCESS_SECRET = '%s'" % auth.access_token.secret


Execute the script, copy and paste the url on your favorite browser and get the PIN. Introduce this PIN number and the script will print the access_key and access_secret required for the next step.

(4) Sending messages to Twitter

With the information gathered before and a script created using the Tweepy API it is very easy to send messages. The following code is depict the simple procedure of getting the message using a command line parameter and then send the message to twitter.


#!/usr/bin/python
#sendMessage2Twitter.py
import sys
import tweepy
CONSUMER_KEY = 'put here the consumer key'
CONSUMER_SECRET = 'put here the consumer secret'
ACCESS_KEY = 'put here the access key'
ACCESS_SECRET = 'put here the access secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(sys.argv[1])

So, the last line get its argument from command line and update the status of our twitter, just by doing this.

./sendMessage2Twitter.py "This is a test of sending messages to twitter from command line using #teewpy and #python"

You can extend the script to gather any kind of information and send it to twitter. The next article will be dealing with getting info from a microcontroller and send it to twitter using this procedure.

VIDEO





Enjoy!






No hay comentarios:

Publicar un comentario