Example usage for twitter4j TwitterObjectFactory createStatus

List of usage examples for twitter4j TwitterObjectFactory createStatus

Introduction

In this page you can find the example usage for twitter4j TwitterObjectFactory createStatus.

Prototype

public static Status createStatus(String rawJSON) throws TwitterException 

Source Link

Document

Constructs a Status object from rawJSON string.

Usage

From source file:at.illecker.storm.commons.util.io.JsonUtils.java

License:Apache License

public static List<Status> readTweets(InputStream tweetsFile, String filterLanguage) {
    List<Status> tweets = new ArrayList<Status>();
    InputStreamReader isr = null;
    BufferedReader br = null;//from  www .j a va  2  s .c  o m
    try {
        isr = new InputStreamReader(tweetsFile, "UTF-8");
        br = new BufferedReader(isr);
        String rawJSON = "";
        while ((rawJSON = br.readLine()) != null) {
            // rawJSON may include multiple status objects within one line
            String regex = "\"created_at\":";
            String[] rawJSONTweets = rawJSON.split("\\}\\{" + regex);

            if (rawJSONTweets.length == 0) { // only one object
                try {
                    Status status = TwitterObjectFactory.createStatus(rawJSON);
                    tweets.add(status);
                    // LOG.info("@" + status.getUser().getScreenName() + " - "
                    // + status.getText());
                } catch (TwitterException twe) {
                    LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                }

            } else { // read multiple objects
                for (int j = 0; j < rawJSONTweets.length; j++) {
                    if (j == 0) {
                        rawJSONTweets[j] = rawJSONTweets[j] + "}";
                    } else if (j == rawJSONTweets.length) {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j];
                    } else {
                        rawJSONTweets[j] = "{" + regex + rawJSONTweets[j] + "}";
                    }
                    try {
                        Status status = TwitterObjectFactory.createStatus(rawJSONTweets[j]);
                        if (filterLanguage != null) {
                            if (status.getLang().equals(filterLanguage)) {
                                tweets.add(status);
                            }
                        } else {
                            tweets.add(status);
                        }
                        // LOG.info("@" + status.getUser().getScreenName() + " - "
                        // + status.getText());
                    } catch (TwitterException twe) {
                        LOG.error("Mailformed JSON Tweet: " + twe.getMessage());
                    }
                }
            }
        }

    } catch (IOException e) {
        LOG.error("IOException: " + e.getMessage());
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException ignore) {
            }
        }
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ignore) {
            }
        }
    }
    LOG.info("Loaded " + tweets.size() + " tweets");
    return tweets;
}

From source file:cc.twittertools.corpus.data.Bz2JsonStatusBlockReader.java

License:Apache License

/**
 * Returns the next status, or <code>null</code> if no more statuses.
 *//* w ww  . j a va 2 s. c o m*/
public Status next() throws IOException {
    Status nxt = null;
    String raw = null;

    while (nxt == null) {
        raw = br.readLine();

        // Check to see if we've reached end of file.
        if (raw == null) {
            return null;
        }

        try {
            nxt = TwitterObjectFactory.createStatus(raw);
        } catch (TwitterException e) {
        }
    }
    return nxt;
}

From source file:cc.twittertools.corpus.data.JsonStatusBlockReader.java

License:Apache License

/**
 * Returns the next status, or <code>null</code> if no more statuses.
 *///ww w. j  av  a2s .  co m
public Status next() throws IOException {
    Status nxt = null;

    while (nxt == null) {
        String raw = br.readLine();

        // Check to see if we've reached end of file.
        if (raw == null) {
            return null;
        }

        try {
            nxt = TwitterObjectFactory.createStatus(raw);
        } catch (TwitterException e) {
        }
    }
    return nxt;
}

From source file:com.marklogic.tweetdeck.LoadRawJSON.java

License:Apache License

/**
 * Usage: java twitter4j.examples.json.LoadRawJSON
 *
 * @param args String[]// w w  w .  j  a  v a 2s .c  om
 */
public static void main(String[] args) {
    try {
        File[] files = new File("statuses").listFiles(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".json");
            }
        });
        for (File file : files) {
            String rawJSON = readFirstLine(file);
            Status status = TwitterObjectFactory.createStatus(rawJSON);
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        }
        System.exit(0);
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.out.println("Failed to store tweets: " + ioe.getMessage());
    } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:com.raythos.sentilexo.files.twitter.TwitterJSONLoader.java

protected void handleRawJsonLine(String rawJSONLine) {
    int lineNo = this.getLinesRead();
    Status status;/*  w w w.  jav  a 2  s . co  m*/
    try {
        log.trace("processing File + " + filename + "line #" + lineNo);
        if (rawJSONLine.startsWith("{\"created_at")) {
            status = TwitterObjectFactory.createStatus(rawJSONLine);
            handleStatusObject(lineNo, status, rawJSONLine);
            log.trace("processing File + " + filename + "line #" + lineNo + "containes twitter status");
            statusesRead++;

        } else
            log.warn("File " + filename + " line " + lineNo + " has no twitter status JSON text");
    } catch (Exception ex) {
        log.error("Exception was raised: " + ex);
    }
}

From source file:com.raythos.sentilexo.trident.twitter.TextLineToAvroKafkaFunction.java

License:Apache License

@Override
public void execute(TridentTuple tuple, TridentCollector tc) {
    String owner = tuple.getString(0);
    String queryName = tuple.getString(1);
    String queryTerms = tuple.getString(2);
    String jsonText = tuple.getString(3);
    Status status = null;/*  www .j av  a 2 s.c  o  m*/
    try {
        log.trace("processing json " + jsonText);
        if (jsonText.startsWith("{\"created_at")) {
            status = TwitterObjectFactory.createStatus(jsonText);
            log.trace("Status object was created from " + jsonText);
            TwitterQueryResultItemAvro avroObject = QueryResultItemMapper.mapItem(owner, queryName, queryTerms,
                    status);
            /* todod 
                Post test and avro to kafka queue
            */
        } else {
            log.warn("No twitter status found in: " + jsonText);
        }
    } catch (Exception ex) {
        log.error("Exception was raised: " + ex);
    }
}

From source file:com.raythos.sentilexo.trident.twitter.TextLineToResultItemFunction.java

License:Apache License

@Override
public void execute(TridentTuple tuple, TridentCollector tc) {
    String owner = tuple.getString(0);
    String queryName = tuple.getString(1);
    String queryTerms = tuple.getString(2);
    String jsonText = tuple.getString(3);
    Status status = null;//from w  w w .  ja va 2s  .  co  m
    try {
        log.trace("processing json " + jsonText);
        if (jsonText.startsWith("{\"created_at")) {
            status = TwitterObjectFactory.createStatus(jsonText);
            log.trace("Status object was created from " + jsonText);

            ResultJson jsonDataItem = new ResultJson(status.getId(), jsonText);
            jsonDataItem.save();
            log.trace("JsonText was saved in jsonLog for " + status.getId());

            TwitterQueryResultItemAvro avroObject = QueryResultItemMapper.mapItem(owner, queryName, queryTerms,
                    status);
            log.trace("Text was deserialised into Avro object with status id = " + avroObject.getStatusId());
            ResultItem dataItem = new ResultItem(avroObject);
            dataItem.save();
            log.info("Result Item StatusId = " + avroObject.getStatusId() + " written to Cassandra");
            tc.emit(new Values(dataItem));
            log.info("Result Item was emmited for " + status.getId());
        } else {
            log.warn("No twitter status found in: " + jsonText);
        }
    } catch (Exception ex) {
        log.error("Exception was raised: " + ex);
    }
}

From source file:edu.cmu.cs.lti.discoursedb.io.twitter.converter.TwitterConverter.java

License:Open Source License

/**
 * Parses a MongoDB Document that represents a tweet into a Twitter4J Status
 * object//from   ww w  .j  a va 2  s  .c  om
 * 
 * @param tweetDocument
 *            a MongoDB document representing a tweet
 * @return a Twitter4J Status object representing the tweet
 */
private Status document2Tweet(Document tweetDocument) {
    Assert.notNull(tweetDocument, "The mongodb document representing the tweet to be parsed cannot be null.");

    Status stat = null; // if parsing fails, null will be returns and mapper
    // will skip the tweet
    try {
        stat = TwitterObjectFactory.createStatus(tweetDocument.toJson());
    } catch (TwitterException e) {
        log.warn("Could not parse tweet from document", e);
    }
    return stat;
}

From source file:net.lacolaco.smileessence.util.TwitterMock.java

License:Open Source License

public Status getStatusMock() throws IOException, TwitterException {
    return TwitterObjectFactory.createStatus(getJson("status.json"));
}

From source file:net.lacolaco.smileessence.util.TwitterMock.java

License:Open Source License

public Status getReplyMock() throws IOException, TwitterException {
    return TwitterObjectFactory.createStatus(getJson("reply.json"));
}