Example usage for twitter4j Twitter uploadMedia

List of usage examples for twitter4j Twitter uploadMedia

Introduction

In this page you can find the example usage for twitter4j Twitter uploadMedia.

Prototype

UploadedMedia uploadMedia(String fileName, InputStream media) throws TwitterException;

Source Link

Document

Uploads media image to be attached via #updateStatus(twitter4j.StatusUpdate) <br>This method calls https://api.twitter.com/1.1/media/upload.json

Usage

From source file:kerguelenpetrel.UpdateStatusServlet.java

License:Apache License

public static long[] addFlickrImg(Twitter twit, HttpServletResponse resp) throws IOException, TwitterException {
    long[] mediaID = new long[1];
    StringBuilder imageFeedUrl = new StringBuilder();
    String tag;//from w ww  . j  a v  a  2s .  co m

    imageFeedUrl.append("http://api.flickr.com/services/feeds/photos_public.gne");

    try {
        Properties p = new Properties();
        InputStream in = UpdateStatusServlet.class.getResourceAsStream("wordnik.properties");
        p.load(in);
        System.setProperty("WORDNIK_API_KEY", p.getProperty("WORDNIK_API_KEY"));

        boolean hasDictionaryDef = false;
        EnumSet<PartOfSpeech> includePartOfSpeech = null;
        EnumSet<PartOfSpeech> excludePartOfSpeech = null;
        int minCorpusCount = 0;
        int maxCorpusCount = 0;
        int minDictionaryCount = 0;
        int maxDictionaryCount = 0;
        int minLength = 3;
        int maxLength = 6;

        Word result = WordsApi.randomWord(hasDictionaryDef, includePartOfSpeech, excludePartOfSpeech,
                minCorpusCount, maxCorpusCount, minDictionaryCount, maxDictionaryCount, minLength, maxLength);
        tag = result.getWord();
        imageFeedUrl.append("?tag=").append(tag);
    }

    catch (FileNotFoundException e) {
        resp.getWriter().println("Wordnik property file not found \n");
        e.printStackTrace(resp.getWriter());
    } catch (KnickerException e) {
        resp.getWriter().println("Problem with Wordnik \n");
        e.printStackTrace(resp.getWriter());
    }

    try {
        XmlReader reader = new XmlReader(new URL(imageFeedUrl.toString()));
        SyndFeed feed = new SyndFeedInput().build(reader);

        int ind = r.nextInt(feed.getEntries().size());
        SyndEntry entry = (SyndEntry) feed.getEntries().get(ind); //Get a random entry

        //Get our links from the feed
        List<SyndLink> links = entry.getLinks();

        //get the URL for the image
        String mediaURL = links.get(1).getHref();
        InputStream imageStream = new URL(mediaURL).openConnection().getInputStream();
        mediaID[0] = twit.uploadMedia(mediaURL, imageStream).getMediaId();
    } catch (FeedException e) {
        resp.getWriter().println("Problem with RSS Feed \n");
        e.printStackTrace(resp.getWriter());
    }
    return mediaID;
}