Example usage for twitter4j Status getCreatedAt

List of usage examples for twitter4j Status getCreatedAt

Introduction

In this page you can find the example usage for twitter4j Status getCreatedAt.

Prototype

Date getCreatedAt();

Source Link

Document

Return the created_at

Usage

From source file:uk.co.cathtanconsulting.twitter.TwitterSource.java

License:Apache License

/**
 * @param status/*from  ww w.j  a  v a  2 s .c  om*/
 * 
 * Generate a TweetRecord object and submit to 
 * 
 */
private void extractRecord(Status status) {
    TweetRecord record = new TweetRecord();

    //Basic attributes
    record.setId(status.getId());
    //Using SimpleDateFormat "yyyy-MM-dd'T'HH:mm:ss'Z'"
    record.setCreatedAtStr(formatterTo.format(status.getCreatedAt()));
    //Use millis since epoch since Avro doesn't do dates
    record.setCreatedAtLong(status.getCreatedAt().getTime());
    record.setTweet(status.getText());

    //User based attributes - denormalized to keep the Source stateless
    //but also so that we can see user attributes changing over time.
    //N.B. we could of course fork this off as a separate stream of user info
    User user = status.getUser();
    record.setUserId(user.getId());
    record.setUserScreenName(user.getScreenName());
    record.setUserFollowersCount(user.getFollowersCount());
    record.setUserFriendsCount(user.getFriendsCount());
    record.setUserLocation(user.getLocation());

    //If it is zero then leave the value null
    if (status.getInReplyToStatusId() != 0) {
        record.setInReplyToStatusId(status.getInReplyToStatusId());
    }

    //If it is zero then leave the value null
    if (status.getInReplyToUserId() != 0) {
        record.setInReplyToUserId(status.getInReplyToUserId());
    }

    //Do geo. N.B. Twitter4J doesn't give use the geo type
    GeoLocation geo = status.getGeoLocation();
    if (geo != null) {
        record.setGeoLat(geo.getLatitude());
        record.setGeoLong(geo.getLongitude());
    }

    //If a status is a retweet then the original tweet gets bundled in
    //Because we can't guarantee that we'll have the original we can
    //extract the original tweet and process it as we have done this time
    //using recursion. Note: we will end up with dupes.
    Status retweetedStatus = status.getRetweetedStatus();
    if (retweetedStatus != null) {
        record.setRetweetedStatusId(retweetedStatus.getId());
        record.setRetweetedUserId(retweetedStatus.getUser().getId());
        extractRecord(retweetedStatus);
    }

    //Submit the populated record onto the channel
    processRecord(record);
}

From source file:uk.co.flax.ukmp.twitter.TweetUpdateThread.java

License:Apache License

private Tweet buildTweetFromStatus(Status status) {
    String text = status.getText();

    Tweet tweet = new Tweet();
    tweet.setId("" + status.getId());
    tweet.setText(text);// w w w  .  j av  a  2  s .c  om
    tweet.setUserScreenName(status.getUser().getScreenName());
    tweet.setUserName(status.getUser().getName());
    tweet.setCreated(status.getCreatedAt());
    if (status.getPlace() != null) {
        tweet.setPlaceName(status.getPlace().getFullName());
        tweet.setCountry(status.getPlace().getCountry());
    }
    tweet.setRetweetCount(status.getRetweetCount());
    tweet.setFavouriteCount(status.getFavoriteCount());
    tweet.setParty(partyListIds.get(status.getUser().getId()));

    if (status.getUserMentionEntities() != null) {
        List<String> screenNames = new ArrayList<>(status.getUserMentionEntities().length);
        List<String> fullNames = new ArrayList<>(status.getUserMentionEntities().length);

        for (UserMentionEntity ent : status.getUserMentionEntities()) {
            screenNames.add(ent.getScreenName());
            if (StringUtils.isNotBlank(ent.getName())) {
                fullNames.add(ent.getName());
            }
        }
        tweet.setMentionScreenNames(screenNames);
        tweet.setMentionFullNames(fullNames);
    }

    if (status.getHashtagEntities().length > 0) {
        List<String> hashtags = new ArrayList<>(status.getHashtagEntities().length);
        for (HashtagEntity ht : status.getHashtagEntities()) {
            hashtags.add(ht.getText());
        }
        tweet.setHashtags(hashtags);
    }

    // Call the entity extraction service
    Map<String, List<String>> entities = entityExtraction.getEntities(text);
    if (entities != null && !entities.isEmpty()) {
        Map<String, Object> tweetEntities = new HashMap<String, Object>();
        entities.keySet().forEach(type -> tweetEntities.put(type, entities.get(type)));
        tweet.setEntities(tweetEntities);
    }

    return tweet;
}

From source file:uniandes.cupi2.tweetSpy.mundo.TweeSpy.java

License:Academic Free License

/**
 * Recupera la informacion de  las horas en las que se ha twitteado.
 *///  w ww .java 2  s.  c  o m
@SuppressWarnings("deprecation")
public void cargarFranjas() {
    ResponseList<Status> timeline = darTimeLine();

    //Creo la franjas y su contenedor para contarlas.
    Palabra madrugada = new Palabra("Madrugador");
    Palabra dia = new Palabra("Maanero");
    Palabra tarde = new Palabra("Poco productivo");
    Palabra noche = new Palabra("Trasnochador");

    for (int i = 0; i < timeline.size(); i++) {
        Status estadoCheck = timeline.get(i);
        int hora = estadoCheck.getCreatedAt().getHours();

        //iniciarcontadores
        try {
            arbolFranjas.insertar(madrugada);
            arbolFranjas.insertar(dia);
            arbolFranjas.insertar(noche);
            arbolFranjas.insertar(tarde);
        } catch (Exception e) {

        }

        if (hora < 5) {
            try {
                arbolFranjas.insertar(madrugada);
            } catch (YaExisteException e) {
                Palabra madruga = arbolFranjas.buscar(madrugada);
                madruga.aumentarContador();
            }
        } else if (hora >= 5 && hora < 12) {
            try {
                arbolFranjas.insertar(dia);
            } catch (YaExisteException e) {
                Palabra day = arbolFranjas.buscar(dia);
                day.aumentarContador();
            }
        } else if (hora >= 12 && hora < 19) {
            try {
                arbolFranjas.insertar(tarde);
            } catch (YaExisteException e) {
                Palabra tard = arbolFranjas.buscar(tarde);
                tard.aumentarContador();
            }
        } else {
            try {
                arbolFranjas.insertar(noche);
            } catch (YaExisteException e) {
                Palabra nit = arbolFranjas.buscar(noche);
                nit.aumentarContador();
            }
        }

    }
}

From source file:uta.ak.CollectTweets.java

public void collectTweetsByFileList(String sinceDate, String untilDate, String tag) {
    try {//ww  w . j  a v a  2s .  c  o m
        // The factory instance is re-useable and thread safe.
        System.out.println("Start to collect tweets from :");
        Set<String> mediaList = new HashSet<String>();
        Resource res = new ClassPathResource("new-social-meida-list.txt");
        //                File stopwords=res.getFile();
        //      File stopwords=new File("/Users/zhangcong/dev/corpus/StopWordTable2.txt");
        InputStreamReader isr = new InputStreamReader(res.getInputStream());
        //            File medias=new File(path);
        //            BufferedReader mdsreader = new BufferedReader(new FileReader(medias));
        BufferedReader mdsreader = new BufferedReader(isr);
        String tempString = mdsreader.readLine();
        while ((tempString = mdsreader.readLine()) != null) {
            System.out.println(tempString.toLowerCase());
            mediaList.add(tempString.toLowerCase());
        }

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("LuhVZOucqdHX6x0lcVgJO6QK3")
                .setOAuthConsumerSecret("6S7zbGLvHMXDMgRXq7jRIA6QmMpdI8i5IJNpnjlB55vpHpFMpj")
                .setOAuthAccessToken("861637891-kLunD37VRY8ipAK3TVOA0YKOKxeidliTqMtNb7wf")
                .setOAuthAccessTokenSecret("vcKDxs6qHnEE8fhIJr5ktDcTbPGql5o3cNtZuztZwPYl4");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        /*
        Connection con = null; //MYSQL
        Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL
        con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/USTTMP", "root", "root.123"); //MYSQL
        System.out.println("connection yes");
                
        String insertSQL="INSERT INTO c_rawtext(mme_lastupdate, mme_updater, title, text, tag, text_createdate) "
                        + "VALUES (NOW(), \"AK\", ?, ?, ?, ?)";
        PreparedStatement insertPS = con.prepareStatement(insertSQL);
        */

        //?usttmp??
        String restUrl = "http://192.168.0.103:8991/usttmp_textreceiver/rest/addText";

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 1);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        for (String mediastr : mediaList) {
            Query query = new Query("from:" + mediastr);
            query.setSince(sinceDate);
            query.setUntil(untilDate);
            query.setCount(100);
            query.setLang("en");

            QueryResult result = twitter.search(query);
            for (Status status : result.getTweets()) {
                System.out.println("@" + status.getUser().getScreenName() + " | "
                        + status.getCreatedAt().toString() + ":" + status.getText());
                System.out.println("Inserting the record into the table...");

                String formattedDate = format1.format(status.getCreatedAt());
                /*
                insertPS.setString (1, status.getUser().getScreenName());
                insertPS.setString (2, status.getText());
                insertPS.setString (3, tag);
                insertPS.setString (4, formattedDate);
                insertPS.addBatch();*/

                //                    if(null!=status.getText()){
                //                        break;
                //                    }

                String interfaceMsg = "<message> " + "    <title> "
                        + ((null != status.getUser().getScreenName()) ? status.getUser().getScreenName()
                                : "NO TITLE")
                        + "    </title> " + "    <text> " + StringEscapeUtils.escapeXml10(status.getText())
                        + "    </text> " + "    <textCreatetime> " + formattedDate + "    </textCreatetime> "
                        + "    <tag> " + tag + "    </tag> " + "</message>";

                //                    String restUrl="http://127.0.0.1:8991/usttmp_textreceiver/rest/addText";

                RestTemplate restTemplate = new RestTemplate();

                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.TEXT_XML);
                headers.setAccept(Arrays.asList(MediaType.TEXT_XML));
                //            headers.setContentLength();
                HttpEntity<String> entity = new HttpEntity<String>(interfaceMsg, headers);

                ResponseEntity<String> resresult = restTemplate.exchange(restUrl, HttpMethod.POST, entity,
                        String.class);

                System.out.println(resresult.getBody());
                if (resresult.getBody().contains("<result>failed</result>")) {
                    throw new RuntimeException("response message error");
                }

            }
        }

        //            System.out.println("Start to insert records...");
        //            insertPS.clearParameters();
        //            int[] results = insertPS.executeBatch();

    } catch (Exception te) {
        te.printStackTrace();
        System.out.println("Failed: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:uta.ak.CollectTweets.java

public void collectTweetsByKeyWords(String keyWords, String sinceDate, String untilDate, String tag) {
    try {//from  w w w  . j  av a 2 s .com

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true).setOAuthConsumerKey("LuhVZOucqdHX6x0lcVgJO6QK3")
                .setOAuthConsumerSecret("6S7zbGLvHMXDMgRXq7jRIA6QmMpdI8i5IJNpnjlB55vpHpFMpj")
                .setOAuthAccessToken("861637891-kLunD37VRY8ipAK3TVOA0YKOKxeidliTqMtNb7wf")
                .setOAuthAccessTokenSecret("vcKDxs6qHnEE8fhIJr5ktDcTbPGql5o3cNtZuztZwPYl4");
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        Connection con = null; //MYSQL
        Class.forName("com.mysql.jdbc.Driver").newInstance(); //MYSQL
        con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/USTTMP", "root", "root.123"); //MYSQL
        System.out.println("connection yes");

        String insertSQL = "INSERT INTO c_rawtext(mme_lastupdate, mme_updater, title, text, tag, text_createdate) VALUES (NOW(), \"AK\", ?, ?, ?, ?)";
        PreparedStatement insertPS = con.prepareStatement(insertSQL);

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DATE, 1);
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");

        Query query = new Query(keyWords);
        query.setSince(sinceDate);
        query.setUntil(untilDate);
        query.setCount(100);
        query.setLang("en");

        QueryResult result = twitter.search(query);
        for (Status status : result.getTweets()) {
            //                    System.out.println("@" + status.getUser().getScreenName() +
            //                                       " | " + status.getCreatedAt().toString() +
            //                                       ":" + status.getText());
            //                    System.out.println("Inserting the record into the table...");

            String formattedDate = format1.format(status.getCreatedAt());

            insertPS.setString(1, status.getUser().getScreenName());
            insertPS.setString(2, status.getText());
            insertPS.setString(3, tag);
            insertPS.setString(4, formattedDate);
            insertPS.addBatch();
        }

        System.out.println("Start to insert records...");
        insertPS.clearParameters();
        int[] results = insertPS.executeBatch();

    } catch (Exception te) {
        te.printStackTrace();
        System.out.println("Failed: " + te.getMessage());
        System.exit(-1);
    }
}

From source file:utils.GetTwitters.java

License:Open Source License

public Iterable<MessageTwitter> getMessages() {
    List<MessageTwitter> messages = new ArrayList<MessageTwitter>(20);
    if (LastUpdate.getInstance(compte).isUpdate()) {
        LOGGER.fine("Les messages twitter sont  jour, envoie du contenu de la base de donne");
        messages.addAll(MessageTwitter.findByCompte(compte));
    } else {//w  w  w.  jav a 2s. co  m
        LOGGER.fine("Les messages twitter ne sont pas  jour, rcupration du contenu de twiter");
        Twitter twitter = getFactory().getInstance();
        twitter.setOAuthConsumer("9Jsib4k1uEMCWZqEHy1t1Q", "vLQQaog60gYRrPCC2bHeEZdod3JDSkTRI9W7r2cZIZ8");
        twitter.setOAuthAccessToken(new AccessToken("225864007-Y11ZtDLq2LVZwMR3anKxPW9nR6dIGkLyFlOhdAMx",
                "GQ16L9QMhhzSiRT4xRia7B25011BoNsXUEgUyp0vKI"));
        ResponseList<Status> listeStatus;
        try {
            listeStatus = twitter.getUserTimeline("@" + compte);
        } catch (TwitterException e) {
            LOGGER.log(Level.SEVERE, "Erreur lors de l'accs  twitter", e);
            return MessageTwitter.findByCompte(compte);
        }
        for (Status status : listeStatus) {
            messages.add(new MessageTwitter(status.getCreatedAt(), status.getText(), compte));
        }
        MessageTwitter.deleteAll();
        for (MessageTwitter message : messages) {
            message.save();
        }
    }
    Collections.sort(messages, new Comparator<MessageTwitter>() {
        public int compare(MessageTwitter o1, MessageTwitter o2) {
            return o2.getDateCreation().compareTo(o1.getDateCreation());
        }
    });
    return messages;
}

From source file:ws.project.languagebasedlexiconanalisys.TwitterStreamAnalizer.java

void parseStream() throws IOException {
    //Inizializza file JSON
    FileWriter file = new FileWriter("data.json");
    JSONObject obj = new JSONObject();
    final JSONArray data = new JSONArray();
    obj.put("data", data);
    file.write(obj.toJSONString());/*from   w  ww .  ja  v a2 s . co m*/
    file.flush();
    file.close();

    SimpleDateFormat currentDate = new SimpleDateFormat();
    currentDate.applyPattern("dd-MM-yyyy");
    final String currentDateStr = currentDate.format(new Date());

    ConfigurationBuilder cfg = new ConfigurationBuilder();
    cfg.setOAuthAccessToken("3065669171-9Hp3VZbz7f0BCsvWWFfgywgqimSIp1AlT98745S");
    cfg.setOAuthAccessTokenSecret("AUmg0AdhHzMXisnP1WV7Wnsw5amWFQPyIojI5aBG5qV4A");
    cfg.setOAuthConsumerKey("arieQRhL2WwgRFfXFLAJp5Hkw");
    cfg.setOAuthConsumerSecret("NvmWqgN1UKKPUWoh9d9Z2PuQobOah8IR5faqX2WjDGBL053sWE");

    StatusListener listener;
    listener = new StatusListener() {
        @Override
        public void onStatus(Status status) {
            SimpleDateFormat tweetDate = new SimpleDateFormat();
            tweetDate.applyPattern("dd-MM-yyyy");
            String tweetDateStr = tweetDate.format(status.getCreatedAt());

            try {
                indexer.openWriter(currentDateStr);
            } catch (IOException ex) {
                Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
            }

            //CHIUDO se cambio giorno ma non ho raggiunto l'1%
            if (!tweetDateStr.equals(currentDateStr)) {
                try {
                    indexer.closeWriter();
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }
                writeOnJson(currentDateStr);
                System.out.println("Giorno successivo, completato senza aver raggiunto 1%");
                System.exit(0);
            }
            tot_count++;

            if (status.getLang().equals("it")) {
                it_count++;
                try {
                    indexer.addTweet(id, status.getText());
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
            try {
                indexer.closeWriter();
            } catch (IOException ex) {
                Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
            }

        }

        @Override
        public void onDeletionNotice(StatusDeletionNotice sdn) {
        }

        @Override
        public void onTrackLimitationNotice(int i) {
        }

        @Override
        public void onScrubGeo(long l, long l1) {
        }

        @Override
        public void onStallWarning(StallWarning sw) {
        }

        @Override
        public void onException(Exception excptn) {
            TwitterException exc = (TwitterException) excptn;
            if (exc.exceededRateLimitation()) {
                try {
                    indexer.closeWriter();
                } catch (IOException ex) {
                    Logger.getLogger(TwitterStreamAnalizer.class.getName()).log(Level.SEVERE, null, ex);
                }
                writeOnJson(currentDateStr);
                System.out.println("1% raccolto, dati raccolti. Amen");
            }
        }

    };

    TwitterStream twitterStream = new TwitterStreamFactory(cfg.build()).getInstance();
    twitterStream.addListener(listener);

    indexer.addIndex(currentDateStr);
    indexer.openWriter(currentDateStr);
    twitterStream.sample();

}

From source file:xdsoft.cloudera.flume.twitter.source.TwitterSource.java

License:Apache License

/**
 * Start processing events. This uses the Twitter Streaming API to sample
 * Twitter, and process tweets.//from   ww w  .j ava2 s  .  c  o  m
 */
@Override
public void start() {
    // The channel is the piece of Flume that sits between the Source and
    // Sink,
    // and is used to process events.
    final ChannelProcessor channel = getChannelProcessor();

    final Map<String, String> headers = new HashMap<String, String>();

    // The StatusListener is a twitter4j API, which can be added to a
    // Twitter
    // stream, and will execute methods every time a message comes in
    // through
    // the stream.
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes in.
        public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the headers
            // and
            // the raw JSON of a tweet
            logger.debug(status.getUser().getScreenName() + ": " + status.getText());

            headers.put("timestamp", String.valueOf(status.getCreatedAt().getTime()));
            Event event = EventBuilder.withBody(DataObjectFactory.getRawJSON(status).getBytes(), headers);

            channel.processEvent(event);
        }

        // This listener will ignore everything except for new tweets
        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    logger.debug("Setting up Twitter sample stream using consumer key {} and" + " access token {}",
            new String[] { consumerKey, accessToken });
    // Set up the stream's listener (defined above),
    twitterStream.addListener(listener);

    // Set up a filter to pull out industry-relevant tweets
    if (keywords.length == 0) {
        logger.debug("Starting up Twitter sampling...");
        twitterStream.sample();
    } else {
        logger.debug("Starting up Twitter filtering...");

        FilterQuery query = new FilterQuery().track(keywords);
        twitterStream.filter(query);
    }
    super.start();
}