List of usage examples for twitter4j Status getId
long getId();
From source file:GetLiveMedia.java
License:Open Source License
public void TweetCollect(String searchString) throws TwitterException { tXCoord = 0;/* w ww. j ava 2 s . c o m*/ tYCoord = 0; ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey("0c1v1wqqNATaVcWpINaHoW3o5") .setOAuthConsumerSecret("a6sEAJ1NLY3cXhkMTrMZF62bIfww6qLvlEetDAU9O09Mp04lyh") .setOAuthAccessToken("330427831-RJig6B6YycJL3xQVoEjjyRaujwnr1xHEQGtSHOOc") .setOAuthAccessTokenSecret("Da71qk4SN6Nu88mLkugDDR6CftVcgOcz3QB8dHNw4hVbD"); TwitterFactory tf = new TwitterFactory(cb.build()); twitter = tf.getInstance(); query = new Query("#" + searchString); result = twitter.search(query); for (Status status : result.getTweets()) { try { System.out.println(status.getGeoLocation()); System.out.println(status.getCreatedAt()); tXCoord = status.getGeoLocation().getLatitude(); tYCoord = status.getGeoLocation().getLongitude(); twitterString = ("@" + status.getUser().getScreenName() + ": " + status.getText() + " : " + "(" + tXCoord + ", " + tYCoord + ")"); twitterURL = "https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId(); } catch (Exception e) { } } }
From source file:CollectAndPredict.java
public static void main(String[] args) { //StreamingExamples.setStreamingLogLevels(); // Set logging level if log4j not configured (override by adding log4j.properties to classpath) if (!Logger.getRootLogger().getAllAppenders().hasMoreElements()) { Logger.getRootLogger().setLevel(Level.WARN); }/*from w ww . ja v a 2 s .c o m*/ String OAuthConsumerKey = "QxeynbXmN93DuNiZKkYfZcS2o"; String OAuthConsumerSecret = "2rAzjerHeW6sIgeDim0A77iGaRn9O683m0DrTbBhaoIuRRq7oU"; String OAuthAccessToken = "825094416935297025-jCegwA25yj3QxF2rHeJ5hRoVu86AfaY"; String OAuthAccessTokenSecret = "CwfNmGcWHoL8qvr5dWDdknYM4k4KvAZc7XlGZuYl2DcR8"; String[] filters = Arrays.copyOfRange(args, 0, args.length); // Set the system properties so that Twitter4j library used by Twitter stream // can use them to generate OAuth credentials System.setProperty("twitter4j.oauth.consumerKey", OAuthConsumerKey); System.setProperty("twitter4j.oauth.consumerSecret", OAuthConsumerSecret); System.setProperty("twitter4j.oauth.accessToken", OAuthAccessToken); System.setProperty("twitter4j.oauth.accessTokenSecret", OAuthAccessTokenSecret); SparkConf sparkConf = new SparkConf().setAppName("JavaTwitterHashTagJoinSentiments"); // check Spark configuration for master URL, set it to local if not configured if (!sparkConf.contains("spark.master")) { sparkConf.setMaster("local[2]"); } SparkSession spark = SparkSession.builder().appName("teste2").config(sparkConf).getOrCreate(); JavaStreamingContext jssc = new JavaStreamingContext(new JavaSparkContext(spark.sparkContext()), Seconds.apply(30)); TokenizerFactory tokFactory = TwitterTokenizerFactory.getTokFactory(); NaiveBayesModel model = NaiveBayesModel.load(spark.sparkContext(), "Docker/myNaiveBayesModel"); HashingTF hashingTF = new HashingTF(1000); JavaReceiverInputDStream<Status> stream = TwitterUtils.createStream(jssc, filters); JavaDStream<Tweet> statuses = stream.map((Status status) -> new Tweet() .addUser(new TwitterUser().addID(status.getUser().getId()).addName(status.getUser().getName()) .addLocation(status.getUser().getLocation()).addDateSignin(status.getUser().getCreatedAt()) .addCountTweets(status.getUser().getStatusesCount()) .addCountFavorites(status.getUser().getFavouritesCount()) .addCountFriends(status.getUser().getFriendsCount()) .addCountFollowers(status.getUser().getFollowersCount())) .addText(status.getText()).addID(status.getId()).addDate(status.getCreatedAt()) .addLatitude( status.getGeoLocation() != null ? status.getGeoLocation().getLatitude() : Double.MAX_VALUE) .addLongitude(status.getGeoLocation() != null ? status.getGeoLocation().getLongitude() : Double.MAX_VALUE)); statuses.foreachRDD(new VoidFunction2<JavaRDD<Tweet>, Time>() { long numTweetsCollected = 0; long numTweetsToCollect = 200; @Override public void call(JavaRDD<Tweet> t1, Time t2) throws Exception { List<Tweet> collect = t1.collect(); long count = collect.size(); if (count > 0) { for (Tweet tweet : collect) { String textoSemUrl = URLRemove.remove(tweet.getText()); Vector v = hashingTF.transform(Arrays.asList(tokFactory .tokenizer(textoSemUrl.toCharArray(), 0, textoSemUrl.length()).tokenize())); double predict = model.predict(v); if (predict == 1) { tweet.setClassifier("POSITIVE"); } else { tweet.setClassifier("NEGATIVE"); } } ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter(); try { ow.writeValue( new FileOutputStream(new File("Docker/Twitter" + t2.milliseconds() + ".json")), collect); } catch (Exception ex) { spark.log().error(ex.getMessage(), ex); } numTweetsCollected += count; spark.log().info("coletou :" + numTweetsCollected + " tweets"); if (numTweetsCollected > numTweetsToCollect) { System.exit(0); } } } }); // statuses.foreachRDD(new VoidFunction2<JavaRDD<String>, Time>() { // long numTweetsCollected = 0; // long numTweetsToCollect = 200; // // @Override // public void call(JavaRDD<String> rdd, Time time) throws Exception { // long count = rdd.count(); // if (count > 0) { // JavaRDD<String> outputRDD = rdd.repartition(10); // outputRDD.saveAsTextFile("/Docker/tweets_" + time.milliseconds()); // numTweetsCollected += count; // if (numTweetsCollected > numTweetsToCollect) { // System.exit(0); // } // } // } // }); // JavaDStream<String> words = stream.flatMap(new FlatMapFunction<Status, String>() { // // @Override // public Iterable<String> call(Status t) throws Exception { // return Arrays.asList(t.getText().split(" ")); // } // }); // // JavaDStream<String> hashTags = words.filter(new Function<String, Boolean>() { // @Override // public Boolean call(String word) { // return word.startsWith("#"); // } // }); // // // Read in the word-sentiment list and create a static RDD from it // String wordSentimentFilePath = "streaming-twitter/examples/data/AFINN-111.txt"; // final JavaPairRDD<String, Double> wordSentiments = jssc.sparkContext() // .textFile(wordSentimentFilePath) // .mapToPair(new PairFunction<String, String, Double>() { // @Override // public Tuple2<String, Double> call(String line) { // String[] columns = line.split("\t"); // return new Tuple2<>(columns[0], Double.parseDouble(columns[1])); // } // }); // // JavaPairDStream<String, Integer> hashTagCount = hashTags.mapToPair( // new PairFunction<String, String, Integer>() { // @Override // public Tuple2<String, Integer> call(String s) { // // leave out the # character // return new Tuple2<>(s.substring(1), 1); // } // }); // // JavaPairDStream<String, Integer> hashTagTotals = hashTagCount.reduceByKeyAndWindow( // new Function2<Integer, Integer, Integer>() { // @Override // public Integer call(Integer a, Integer b) { // return a + b; // } // }, new Duration(10000)); // // // Determine the hash tags with the highest sentiment values by joining the streaming RDD // // with the static RDD inside the transform() method and then multiplying // // the frequency of the hash tag by its sentiment value // JavaPairDStream<String, Tuple2<Double, Integer>> joinedTuples // = hashTagTotals.transformToPair(new Function<JavaPairRDD<String, Integer>, JavaPairRDD<String, Tuple2<Double, Integer>>>() { // @Override // public JavaPairRDD<String, Tuple2<Double, Integer>> call( // JavaPairRDD<String, Integer> topicCount) { // return wordSentiments.join(topicCount); // } // }); // // JavaPairDStream<String, Double> topicHappiness = joinedTuples.mapToPair( // new PairFunction<Tuple2<String, Tuple2<Double, Integer>>, String, Double>() { // @Override // public Tuple2<String, Double> call(Tuple2<String, Tuple2<Double, Integer>> topicAndTuplePair) { // Tuple2<Double, Integer> happinessAndCount = topicAndTuplePair._2(); // return new Tuple2<>(topicAndTuplePair._1(), // happinessAndCount._1() * happinessAndCount._2()); // } // }); // // JavaPairDStream<Double, String> happinessTopicPairs = topicHappiness.mapToPair( // new PairFunction<Tuple2<String, Double>, Double, String>() { // @Override // public Tuple2<Double, String> call(Tuple2<String, Double> topicHappiness) { // return new Tuple2<>(topicHappiness._2(), // topicHappiness._1()); // } // }); // // JavaPairDStream<Double, String> happiest10 = happinessTopicPairs.transformToPair( // new Function<JavaPairRDD<Double, String>, JavaPairRDD<Double, String>>() { // @Override // public JavaPairRDD<Double, String> call( // JavaPairRDD<Double, String> happinessAndTopics) { // return happinessAndTopics.sortByKey(false); // } // } // ); // // // Print hash tags with the most positive sentiment values // happiest10.foreachRDD(new VoidFunction<JavaPairRDD<Double, String>>() { // @Override // public void call(JavaPairRDD<Double, String> happinessTopicPairs) { // List<Tuple2<Double, String>> topList = happinessTopicPairs.take(10); // System.out.println( // String.format("\nHappiest topics in last 10 seconds (%s total):", // happinessTopicPairs.count())); // for (Tuple2<Double, String> pair : topList) { // System.out.println( // String.format("%s (%s happiness)", pair._2(), pair._1())); // } // } // }); jssc.start(); try { jssc.awaitTermination(); } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:ac.simons.tweetarchive.tweets.TweetStorageService.java
License:Apache License
@Transactional public TweetEntity store(final Status status, final String rawContent) { final Optional<TweetEntity> existingTweet = this.tweetRepository.findOne(status.getId()); if (existingTweet.isPresent()) { final TweetEntity rv = existingTweet.get(); log.warn("Tweet with status {} already existed...", rv.getId()); return rv; }/* www . j a v a 2 s . c om*/ final TweetEntity tweet = new TweetEntity(status.getId(), status.getUser().getId(), status.getUser().getScreenName(), status.getCreatedAt().toInstant().atZone(ZoneId.of("UTC")), extractContent(status), extractSource(status), rawContent); tweet.setCountryCode(Optional.ofNullable(status.getPlace()).map(Place::getCountryCode).orElse(null)); if (status.getInReplyToStatusId() != -1L && status.getInReplyToUserId() != -1L && status.getInReplyToScreenName() != null) { tweet.setInReplyTo(new InReplyTo(status.getInReplyToStatusId(), status.getInReplyToScreenName(), status.getInReplyToUserId())); } tweet.setLang(status.getLang()); tweet.setLocation(Optional.ofNullable(status.getGeoLocation()) .map(g -> new TweetEntity.Location(g.getLatitude(), g.getLongitude())).orElse(null)); // TODO Handle quoted tweets return this.tweetRepository.save(tweet); }
From source file:adapter.TwitterAllAdapter.java
License:Apache License
@Override public void run() { while (true) { try {/*w w w . jav a 2 s .co m*/ Status status = this.messageQueue.take(); Event event = new Event(); int rtCount = 0; int fvCount = 0; if (status.isRetweet()) { rtCount = status.getRetweetedStatus().getRetweetCount(); fvCount = status.getRetweetedStatus().getFavoriteCount(); } Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(), status.getUser().getScreenName(), status.getUser().getLang(), status.getUser().getFollowersCount(), status.getUser().getFriendsCount(), status.getHashtagEntities(), fvCount, rtCount, status.getGeoLocation()); //status.getRetweetedStatus().getRetweetCount(), status.getGeoLocation()); eventCount++; // cantReplicas: Cantidad de PEs que se quieren generar para el proximo operador // Nota: recuerden que la topologa no necesariamente deba ser de este estilo // tambin poda ser por un hash int cantReplicas = 10; event.put("levelTweet", Integer.class, eventCount % cantReplicas); event.put("id", Integer.class, eventCount); event.put("tweet", Tweet.class, tweet); getRemoteStream().put(event); } catch (Exception e) { logger.error("Error: " + e); logger.error("Error al crear evento"); } } }
From source file:adapter.TwitterKeywordsAdapter.java
License:Apache License
@Override public void run() { while (true) { try {/*w ww . j a v a 2 s . co m*/ Status status = this.messageQueue.take(); Event event = new Event(); Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(), status.getUser().getScreenName(), status.getUser().getLang(), status.getUser().getFollowersCount(), status.getUser().getFriendsCount(), status.getHashtagEntities(), status.getFavoriteCount(), status.getRetweetCount(), status.getGeoLocation()); eventCount++; // cantReplicas: Cantidad de PEs que se quieren generar para el proximo operador // Nota: recuerden que la topologa no necesariamente deba ser de este estilo // tambin poda ser por un hash int cantReplicas = 10; event.put("levelTweet", Integer.class, eventCount % cantReplicas); event.put("id", Integer.class, eventCount); event.put("tweet", Tweet.class, tweet); getRemoteStream().put(event); } catch (Exception e) { logger.error("Error: " + e); logger.error("Error al crear evento"); } } }
From source file:adapter.TwitterLanguageAdapter.java
License:Apache License
@Override public void run() { while (true) { try {//from w ww . j a v a 2 s.co m Status status = this.messageQueue.take(); Event event = new Event(); Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(), status.getUser().getScreenName(), status.getUser().getLang(), status.getUser().getFollowersCount(), status.getUser().getFriendsCount(), status.getHashtagEntities(), status.getFavoriteCount(), status.getRetweetCount(), status.getGeoLocation()); eventCount++; // cantReplicas: Cantidad de PEs que se quieren generar para el proximo operador // Nota: recuerden que la topologa no necesariamente deba ser de este estilo // tambin poda ser por un hash int cantReplicas = 10; event.put("levelTweet", Integer.class, eventCount % cantReplicas); event.put("id", Integer.class, eventCount); event.put("tweet", Tweet.class, tweet); getRemoteStream().put(event); } catch (Exception e) { logger.error("Error: " + e); logger.error("Error al crear evento"); } } }
From source file:adapter.TwitterLocationAdapter.java
License:Apache License
@Override public void run() { while (true) { try {/*from w w w . ja v a2 s . c om*/ Status status = this.messageQueue.take(); Event event = new Event(); Tweet tweet = new Tweet(status.getId(), status.getText(), status.getCreatedAt(), status.getPlace(), status.getUser().getScreenName(), status.getUser().getLang(), status.getUser().getFollowersCount(), status.getUser().getFriendsCount(), status.getHashtagEntities(), status.getFavoriteCount(), status.getRetweetCount(), status.getGeoLocation()); eventCount++; // cantReplicas: Cantidad de PEs que se quieren generar para el // proximo operador // Nota: recuerden que la topologa no necesariamente deba ser // de este estilo // tambin poda ser por un hash int cantReplicas = 10; event.put("levelTweet", Integer.class, eventCount % cantReplicas); event.put("id", Integer.class, eventCount); event.put("tweet", Tweet.class, tweet); getRemoteStream().put(event); } catch (Exception e) { logger.error("Error: " + e); logger.error("Error al crear evento"); } } }
From source file:at.illecker.sentistorm.spout.TwitterStreamSpout.java
License:Apache License
@Override public void nextTuple() { Status tweet = m_tweetsQueue.poll(); if (tweet == null) { TimeUtils.sleepMillis(50); // sleep 50 ms } else {/*from w ww . jav a2 s . c om*/ // Emit tweet m_collector.emit(new Values(tweet.getId(), tweet.getText(), null)); } }
From source file:at.illecker.storm.commons.spout.TwitterFilesSpout.java
License:Apache License
public void nextTuple() { Status tweet = m_tweets.get(m_index); // infinite loop m_index++;// w w w .j a v a 2 s . c o m if (m_index >= m_tweets.size()) { m_index = 0; } // Emit tweet m_collector.emit(new Values(tweet.getId(), tweet.getText(), null)); // Optional sleep between emitting tuples if (m_tupleSleepMs != 0) { TimeUtils.sleepMillis(m_tupleSleepMs); } }
From source file:au.edu.anu.portal.portlets.tweetal.servlet.TweetalServlet.java
License:Apache License
public void getTweets(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); PrintWriter out = response.getWriter(); String userToken = request.getParameter("u"); String userSecret = request.getParameter("s"); Twitter twitter = twitterLogic.getTwitterAuthForUser(userToken, userSecret); if (twitter == null) { // no connection response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); return;// w ww . j a va2 s. c om } String cacheKey = userToken; Element element = null; // force refresh boolean force = Boolean.parseBoolean(request.getParameter("force")); if (force) { log.debug("force refresh for " + userToken); // remove tweets cache tweetsCache.remove(cacheKey); } else { element = tweetsCache.get(cacheKey); } if (element == null) { synchronized (tweetsCache) { // if it is still null after acquiring lock element = tweetsCache.get(cacheKey); if (element == null) { log.debug("cache miss: getting tweets for " + userToken); System.out.println("Last refreshed: " + Calendar.getInstance().getTime()); try { ResponseList<Status> friendStatus = twitter.getFriendsTimeline(); long maxId = Long.MIN_VALUE; JSONObject json = new JSONObject(); lastRefreshed = Calendar.getInstance().getTime().toString(); if (lastRefreshed == null) { json.element("lastRefreshed", "unable to retrieve last refreshed"); } else { json.element("lastRefreshed", lastRefreshed.toString()); } User currentUser = twitter.showUser(twitter.getId()); Status lastUserStatus = currentUser.getStatus(); if (lastUserStatus == null) { json.element("lastStatusUpdate", "unable to retrieve last status"); } else { Date lastStatusUpdate = lastUserStatus.getCreatedAt(); json.element("lastStatusUpdate", lastStatusUpdate.toString()); } for (Status status : friendStatus) { maxId = Math.max(maxId, status.getId()); json.accumulate("statusList", getStatusJSON(twitter, status)); } if (log.isDebugEnabled()) { log.debug(json.toString(2)); } out.print(json.toString()); element = new Element(cacheKey, new TweetsCacheElement(System.currentTimeMillis(), maxId, json)); tweetsCache.put(element); return; } catch (TwitterException e) { log.error("GetTweets: " + e.getStatusCode() + ": " + e.getClass() + e.getMessage()); if (e.getStatusCode() == 401) { // invalid credentials response.sendError(HttpServletResponse.SC_UNAUTHORIZED); } else if (e.getStatusCode() == -1) { // no connection response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE); } else { // general error response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return; } } } } // tweets available in cache log.debug("cache hit: getting tweets for " + userToken); TweetsCacheElement tweets = (TweetsCacheElement) element.getObjectValue(); // if just refreshed too quickly, don't request update, just use // whatever in cache long period = System.currentTimeMillis() - tweets.getLastRefresh(); System.out.println("Already refreshed: " + (period / 1000) + " second(s) ago"); if (period < 2 * 60 * 1000) { log.debug("refreshed too quickly: " + (period / 1000) + " seconds"); JSONObject json = tweets.getResult(); lastRefreshed = Calendar.getInstance().getTime().toString(); json.element("lastRefreshed", lastRefreshed.toString()); out.print(json.toString()); return; } // get new updates since the last id long maxId = tweets.lastId; try { JSONObject json = tweets.getResult(); ResponseList<Status> friendStatus = twitter.getFriendsTimeline(new Paging(maxId)); tweets.setLastRefresh(System.currentTimeMillis()); log.debug(String.format("Got %d new tweets", friendStatus.size())); if (friendStatus.size() > 0) { JSONArray newTweets = new JSONArray(); lastRefreshed = Calendar.getInstance().getTime().toString(); json.element("lastRefreshed", lastRefreshed.toString()); for (Status status : friendStatus) { maxId = Math.max(maxId, status.getId()); newTweets.add(getStatusJSON(twitter, status)); } if (log.isDebugEnabled()) { log.debug("new tweets:\n" + newTweets.toString(2)); } json.getJSONArray("statusList").addAll(0, newTweets); tweets.setLastId(maxId); User currentUser = twitter.showUser(twitter.getId()); Status lastUserStatus = currentUser.getStatus(); if (lastUserStatus == null) { json.element("lastStatusUpdate", "unable to retrieve last status"); } else { Date lastStatusUpdate = lastUserStatus.getCreatedAt(); json.element("lastStatusUpdate", lastStatusUpdate.toString()); } } out.print(json.toString()); } catch (TwitterException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (JSONException e) { log.error(e); } }