Use the Eerce Luke

It's pretty complicated to write a program that sends messages to a service like Twitter. Fortunately, other people have already tackled problems like this and posted their code on the Web. Here's a Python function (found on the Web) that is very similar to what you need:

This is -the te*t oi -the message "that be sent

Twitter

Put youv

Twitter passwot-d def send_to_twitter():

msg = "I am a message that will be sent to Twitter" password_manager = urllib.request.HTTPPasswordMgr() password_manager.add_password("Twitter API",

"http://twitter.com/statuses", http_handler = urllib.request.HTTPBasicAuthHandler(password_manager) page_opener = urllib.request.build_opener(http_handler) urllib.request.install_opener(page_opener) params = urllib.parse.urlencode( {'status': msg} )

resp = urllib.request.urlopen("http://twitter.com/statuses/update.json", params) resp.read()

This code looks complex but, for now, all you need to know is that it sends a message to the Twitter service. An advantage of using functions (which is illustrated here) is that they allow you to understand a program at a high level without having to initially understand all the details. This is known as working at a higher level of abstraction.

This code looks like it could be useful. But is there a problem?

Why can't you just replace the print() calls in our existing program with calls to this function?

Do this!

To use the code you will first need to sign up for a free Twitter account. To register, go to:

https://twitter.com/signup

the message stays the same

Was this article helpful?

0 0

Post a comment