Automatically blocking Twitter spam accounts

At this point, I'm sure that everyone has been followed by one of many spam Twitter accounts.

It's pretty obvious that these are bogus, if not by the lack of actual content, then by the small number of followers compared to the massive number of friends.

So let's use the Twitter API to write a little script that detects these accounts and automatically blocks them.

The first thing to do is to get the list of followers for your Twitter account. All you have to do is send a GET request to http://twitter.com/statuses/followers.json (change the .json to .xml to get XML back instead). The request should send, via Basic Authentication, your Twitter username and password.

The above gives you a nice list of Twitter users with all sorts of information, including their Twitter user ID, number of followers, and number of friends. From there, it's simple to loop over the list and check the ratio of followers to friends.

I found

> if (**Follower** / **Friends**) \* 100 < 5

to be a nice threshold.

Now that we've identified the bogus accounts, we can use the API call to block them. In this case, we do an HTTP POST to http://twitter.com/blocks/create/id.json, passing the user ID to block as the single POST value.

At the end of this post is a complete Python script that will do all of the above. In the process of writing the script, I got to learn about parsing Python script command-line arguments using optparse.

Using optparse makes handling command-line arguments dead simple.

~~~~ {.python name="code"} parser = OptionParser() parser.add_option("-d", "--dry-run", action="store_true", dest="dryrun", help="Displays accounts that would be blocked.") (options, args) = parser.parse_args() if options.dryrun: print "--dry-run or -d was found on the command-line." else: print "No --dry-run or -d found." wzxhzdk:0