Initial sketch

There is still a way to go but so far in my attempt to write a twitter monitoring program I have managed to get a program to listen to the twitter feed for a list of keywords, and summarise them after the program exits.

So lets walk through this section by section (my apologies for the lack of comments in the code, but its just a hacky test so far) so to begin with we include a few libraries: time, to get the current time stamp; datetime, to help turn the time stamps into something readable; Twython is the python twitter API, allowing us to get access the tweets as they come in.

Next up we define a few variables, first up is our search term list, then the keys that twitter need in order to access the API. APP_KEY and APP_SECRET are used in order to identify the program that you are using to access the twitter feed, we could probably get away with just this as we are not sending tweets but OAUTH_TOKEN and OAUTH_TOKEN_SECRET would allow me in theory to get the program to tweet its results from my account. I will write up how to get these keys for you another time. Next in our variable list the program stores when it was started, and the number of seconds it is to run for, in this case it is set to a 24 hours.

So now we get onto the parts which in fact talk to twitter, so we create the class MyStreamer, which as a child class of TwythonStreamer. We then create two functions in this class, on_success and on_error.

on_success first checks that we are still within the time frame we wanted to use, and if not disconnecting the program from the twitter stream. after this it checks that the tweet actually has some text, and prints out the tweet to the screen. We then capture the data we wanted from the tweet, such as the users screen name, the text of the tweet, and when the tweet was sent and save the data into the array tweets. In order to make the timestamp more user friendly we use datetime.fromtimestamp() to interpret twitters time stamp, remembering that twitter stores its timestamps in ms rather than s.

on_error is the code that is run when there is a problem with the twitter feed, such as non internet connection. This is set up to simply print the error code out to the screen and then disconnect the twitter stream.

Next up we define our own function finishsup(). This code is designed to process the contents of tweets and display them to the user, eventually via email but for now simply on the screen.

The first output from finishup() is to display the contents of tweets in the order in which they were captured in a human friendly way. We then run through and display for each search term, which of the tweets match, counting the number as we go, summarising the results at the end.

The final output is to look for tweets which use more than one of our search terms. To do this, we first create a loop over all terms, and inside this loop, we make another loop over all terms. We then check that both values of the loop are not the same, then search over all stored tweets to see if the contain both search terms, and if both are present we display the tweet.

This is a very early stage project, but bellow is the python code that i have running so far. Let me know if spot anything that can be done better, or something silly I have overlooked.

Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *