[JAVA] Get Twitter images using twitter4j

Mostly a full copy of the reference site, but a memo

Prepare the following in the src folder

code

twitter4j.properties

#
debug=false
oauth.consumerKey= ***
oauth.consumerSecret= ***
oauth.accessToken= ***
oauth.accessTokenSecret= ***

Get the api token at https://apps.twitter.com/.

GetTwitterImage.java

/**
 *Reference: http://kikutaro777.hatenablog.com/entry/2014/01/26/110350
 *TWEET the image of Twitter ID or tag defined in Word_Save as many as NUM locally.
 */

package oppai_image_get;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

import twitter4j.*;

public class GetTwitterImage {

    //Number of acquisitions
    static final int TWEET_NUM = 20;
    
    //Image extension to be saved
    static final String TARGET_EXTENSION = ".jpg ";
    
    //Search query
    static final String Word = "aragaki_fun"; //Specify by ID or tag in Word.
    static final String MY_QUERY = "from:" + Word;
    
    public static void main( String[] args ) throws TwitterException, MalformedURLException, IOException
    {
        Twitter twitter = new TwitterFactory().getInstance();
        
        //Search execution
        Query query = new Query(MY_QUERY);
        query.setCount(TWEET_NUM);
        QueryResult result = twitter.search(query);
        
        //Check and save media entity information from search results
        for(Status sts : result.getTweets()){
            MediaEntity[] arrMedia = sts.getMediaEntities();
            for(MediaEntity media : arrMedia){
                //A filter with the number of favos looks good
                //if(sts.getFavoriteCount() > 5)
                
                //For the time being, filter by extension only
                if(media.getMediaURL().endsWith(TARGET_EXTENSION)){
                    URL website = new URL(media.getMediaURL());
                    ReadableByteChannel rbc = Channels.newChannel(website.openStream());
                    //Add the creation date of Status to the save file name
                    DateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
                    FileOutputStream fos = 
                        new FileOutputStream("ImageFromTwitter" + df.format(sts.getCreatedAt()) + TARGET_EXTENSION);
                    fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
                }
            }
        }
        System.out.println("Output complete!");
    }

}

Execution result

スクリーンショット 2018-04-16 20.37.16.png

The image of Yui Aragaki was taken safely.

reference

http://kikutaro777.hatenablog.com/entry/2014/01/26/110350 http://java-beginner.com/practice-search-twitter/ http://nekonokotatsu.hatenablog.com/entry/2015/01/27/222653 https://qiita.com/yokoh9/items/760e432ebd39040d5a0f

Recommended Posts

Get Twitter images using twitter4j
[Rails] Save images using carrierwave
Get date without using Calendar class
Get PV (views) using Impressionist ~ Rails
[Ethereum] Get block information using web3j