Learning Python Network Programming

(Sean Pound) #1
Chapter 3

This will call get_mentions() every 60 seconds and then process any new tweets
that have been downloaded. If we hit any HTTP errors, then it will retry the process
15 times before exiting the program.


Now if we run our program, then it will run continuously, replying to tweets that
mention the world clock app account. Give it a try, run the program, and then send
some tweets from your test account. After a minute, you will see some replies to
your notifications.


Taking it further


Now that we've written a basic functional Twitter API client, there are certainly some
things that we could improve upon. Although we don't have space in this chapter to
explore enhancements in detail, it's worth mentioning a few to inform future projects
you may want to undertake.


Polling and the Twitter streaming APIs


You may have already spotted a problem that our client will only pull a maximum
of 200 tweets per poll. In each poll, Twitter provides the most recent tweets first. This
means that if we get more than 200 tweets in 60 seconds, then we will permanently
lose the tweets that come in first. In fact, there is no complete solution for this using
the statuses/mentions_timeline.json endpoint.


Twitter's solution for this problem is to provide an alternative type of API, which
is called a streaming API. When connecting to these APIs, the HTTP response
connection is actually left open and the incoming tweets are continuously streamed
through it. The Requests package provides neat functionality for handling this.
The Requests response objects have an iter_lines() method, which runs
indefinitely. It is capable of outputting a line of data whenever the server sends one,
which can then be processed by us. If you do find that you need this, then there's
an example that will help you in getting started in the Requests documentation,
and it can be found at http://docs.python-requests.org/en/latest/user/
advanced/#streaming-requests.


Alternative oAuth flows


Our setup for having our app operate against our main account and having a second
account for sending the test tweets is a little clunky, especially so if you use your
app account for regular tweeting. Wouldn't it be better to have a separate account
dedicated to handling the world clock tweets?

Free download pdf