A Script to Download Images From Twitter

Recently, I decided to pull images from Twitter to use in my Deep Neural Network project. Apparently, it is not a good idea because of the noisy data which might be the topic of another post. In this post, I will share a simple Java Code that allow you to download images from Twitter.

The Code is available in Github.

To run the code, you need to have a Twitter Application Account so that you can get appropriate authentication token for the Twitter’s API.

I want to explain the code little. The code is using Twitter’s Streaming API with Twitter’s Java Client.

Once you get the authentication token, you just need to decide which terms will be tracked. And this is all. The Code will check if the tweet has appropriate image and if there is, it will download the image in a separate thread that you can specify the maximum number of threads. It will also take care of duplicate urls which is important because I saw in my own tests, there are lots of tweets with same image url.

If you want to specify other constraints like location, it is fairly simple and can be found in Twitter’s Streaming API documentation.

It is very naive approach to get images and not tested very well. I am sharing the code because It worked for me and it might be a starting point for your project!