org.springframework.social.twitter.api.Tweet Java Examples

The following examples show how to use org.springframework.social.twitter.api.Tweet. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TimelineToMusicService.java    From computoser with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Sentiment determines major or minor scale (or lydian/dorian ~= neutral)
 * Average length of tweets determines pentatonic or heptatonic
 *
 * @param tweets
 * @return scale
 */
private Scale getScale(List<Tweet> tweets, TimelineMusic meta) {
    Set<String> documents = new HashSet<>();
    for (Tweet tweet : tweets) {
        documents.add(tweet.getText());
    }
    SentimentResult sentiment = sentimentAnalyzer.getSentiment(documents, meta);

    double totalLength = 0;
    for (String document : documents) {
        totalLength += document.length();
    }
    double averageLength = totalLength / documents.size();

    meta.setSentiment(sentiment);
    meta.setAverageLength(averageLength);

    if (sentiment == SentimentResult.POSITIVE) {
        return averageLength < 40 ? Scale.MAJOR_PENTATONIC : Scale.MAJOR;
    } else if (sentiment == SentimentResult.NEGATIVE) {
        return averageLength < 40 ? Scale.MINOR_PENTATONIC : Scale.MINOR;
    }
    // choose rarer scales for neutral tweets
    return Chance.test(50) ? Scale.LYDIAN : Scale.DORIAN;
}
 
Example #2
Source File: TimelineToMusicService.java    From computoser with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * Gets the tempo, depending on the rate of tweeting
 *
 * @param tweets
 * @return tempo
 */
private Tempo getTempo(List<Tweet> tweets, TimelineMusic meta) {
    long totalSpacingInMillis = 0;
    Tweet previousTweet = null;
    for (Tweet tweet : tweets) {
       if (previousTweet != null) {
           totalSpacingInMillis += Math.abs(previousTweet.getCreatedAt().getTime() - tweet.getCreatedAt().getTime());
       }
       previousTweet = tweet;
    }

    double averageSpacing = totalSpacingInMillis / (tweets.size() - 1);
    meta.setAverageSpacing(averageSpacing);

    if (averageSpacing > 3 * DateTimeConstants.MILLIS_PER_DAY) { //once every three days
        return Tempo.VERY_SLOW;
    } else if (averageSpacing > 1.5 * DateTimeConstants.MILLIS_PER_DAY) { // more than once every 1.5 days
        return Tempo.SLOW;
    } else if (averageSpacing > 16 * DateTimeConstants.MILLIS_PER_HOUR) { // more than once every 16 hours
        return Tempo.MEDIUM;
    } else if (averageSpacing > 4 * DateTimeConstants.MILLIS_PER_HOUR) { // more than once every 4 hours
        return Tempo.FAST;
    } else {
        return Tempo.VERY_FAST;
    }
}
 
Example #3
Source File: TimelineToMusicService.java    From computoser with GNU Affero General Public License v3.0 6 votes vote down vote up
@Transactional
public TimelineMusic storeUserTimelinePiece(User user) {
    if (user == null) {
        return null;
    }

    SocialAuthentication auth = userDao.getTwitterAuthentication(user);

    if (auth == null) {
        return null;
    }

    Twitter twitter = provider.getApi(auth.getToken(), auth.getSecret());
    List<Tweet> tweets = twitter.timelineOperations().getUserTimeline(200);

    TimelineMusic meta = getUserTimelinePiece(tweets);
    meta.setTwitterHandle(twitter.userOperations().getScreenName());
    meta.setUser(user);

    meta = pieceDao.persist(meta);
    return meta;
}
 
Example #4
Source File: SearchController.java    From spring-twitter-stream with MIT License 6 votes vote down vote up
@GetMapping(path = "tweet")
public String searchTwitter(Model model, @RequestParam String search) {
	
	int count = 200;
	
    SearchResults results = twitterTemplate.searchOperations().search(
    	    new SearchParameters(search)
    	        .resultType(SearchParameters.ResultType.RECENT)
    	        .count(count));
    
    List<Tweet> tweets = results.getTweets();        
    model.addAttribute("tweets", tweets);
    model.addAttribute("count", count);
    model.addAttribute("search", search);
    
    return "search";
}
 
Example #5
Source File: TwitterService.java    From hive with Apache License 2.0 5 votes vote down vote up
/**
* 
* @param twitterUser
* @return
*/
public List <Tweet> getUserTimeline(String twitterUser) {
    TimelineOperations timelineOps = twitter.timelineOperations();
    List<Tweet> tweets = timelineOps.getUserTimeline("@" + twitterUser);

    return tweets;
}
 
Example #6
Source File: TimelineToMusicService.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
public TimelineMusic getUserTimelinePiece(List<Tweet> tweets) {
    TimelineMusic meta = new TimelineMusic();
    Scale scale = getScale(tweets, meta);
    meta.setScale(scale);

    Tempo tempo = getTempo(tweets, meta);
    meta.setTempo(tempo);

    Variation variation = getVariation(tweets, meta);
    meta.setVariation(variation);

    UserPreferences prefs = new UserPreferences();
    prefs.setTempo(tempo);
    prefs.setScale(scale);
    prefs.setVariation(variation);

    List<Piece> pieces = pieceDao.getByPreferences(prefs);
    if (pieces.isEmpty()) {
        logger.warn("No piece found for preferences " + prefs + ". Getting relaxing criteria");
        prefs.setVariation(Variation.ANY);
        pieces = pieceDao.getByPreferences(prefs);
        if (pieces.isEmpty()) {
            prefs.setTempo(Tempo.ANY);
            pieces = pieceDao.getByPreferences(prefs);
        }
    }

    Piece piece = pieces.get(random.nextInt(pieces.size()));
    meta.setPiece(piece);
    return meta;
}
 
Example #7
Source File: TwitterService.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
* 
* @param twitterUser
* @return
*/
public List <Tweet> getUserTimeline(String twitterUser) {
    TimelineOperations timelineOps = twitter.timelineOperations();
    List<Tweet> tweets = timelineOps.getUserTimeline("@" + twitterUser);

    return tweets;
}
 
Example #8
Source File: Tweets.java    From lemonaid with MIT License 5 votes vote down vote up
public Response(SearchResults results, int count) {
	for (int i = 0; i < results.getTweets().size(); i++) {
		if (statuses == null) this.statuses = new ArrayList<Tweet>(); 
		this.statuses.add(results.getTweets().get(i));
		if (i >= count - 1) break;
	}
	search_metadata = results.getSearchMetadata();
}
 
Example #9
Source File: TwitterService.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
* 
* @param twitterUser
* @return
*/
public List <Tweet> getUserTimeline(String twitterUser) {
    TimelineOperations timelineOps = twitter.timelineOperations();
    List<Tweet> tweets = timelineOps.getUserTimeline("@" + twitterUser);

    return tweets;
}
 
Example #10
Source File: TwitterService.java    From hive with Apache License 2.0 5 votes vote down vote up
/**
* 
* @param twitterUser
* @return
*/
public List <Tweet> getUserTimeline(String twitterUser) {
    TimelineOperations timelineOps = twitter.timelineOperations();
    List<Tweet> tweets = timelineOps.getUserTimeline("@" + twitterUser);

    return tweets;
}
 
Example #11
Source File: DataController.java    From hive with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/timeline/{twitterUser}")
public List<Tweet> getUserTimeline(@PathVariable String twitterUser) {
	String inputValue = String.format(twitterView, twitterUser);
logger.error(inputValue);
   return twitterService.getUserTimeline(twitterUser);
}
 
Example #12
Source File: DataController.java    From hive with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/timeline/{twitterUser}")
public List<Tweet> getUserTimeline(@PathVariable String twitterUser) {
	String inputValue = String.format(twitterView, twitterUser);
logger.error(inputValue);
   return twitterService.getUserTimeline(twitterUser);
}
 
Example #13
Source File: DataController.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/timeline/{twitterUser}")
public List<Tweet> getUserTimeline(@PathVariable String twitterUser) {
	String inputValue = String.format(twitterView, twitterUser);
logger.error(inputValue);
   return twitterService.getUserTimeline(twitterUser);
}
 
Example #14
Source File: Tweets.java    From lemonaid with MIT License 4 votes vote down vote up
public void setStatuses(List<Tweet> statuses) {
	this.statuses = statuses;
}
 
Example #15
Source File: Tweets.java    From lemonaid with MIT License 4 votes vote down vote up
public List<Tweet> getStatuses() {
	return statuses;
}
 
Example #16
Source File: StreamTweetEventService.java    From spring-twitter-stream with MIT License 4 votes vote down vote up
public void streamTweetEvent(List<SseEmitter> emitters) throws InterruptedException{

    	List<StreamListener> listeners = new ArrayList<StreamListener>();
    	
    	StreamListener streamListener = new StreamListener() {
			@Override
			public void onWarning(StreamWarningEvent warningEvent) {
			}

			@Override
			public void onTweet(Tweet tweet) {
				//log.info("User '{}', Tweeted : {}, from ; {}", tweet.getUser().getName() , tweet.getText(), tweet.getUser().getLocation());
				Integer connectedUsers =  emitters.size();
				
				//log.info("Streaming to :" + connectedUsers +" connected Users");
				
				if (connectedUsers!=0) {
					for (SseEmitter emiter : emitters) {
						try {
							emiter.send(SseEmitter.event().name("streamLocation").data(tweet.getUser().getLocation()));
							
							StringBuilder hashTag = new StringBuilder();
							
							List<HashTagEntity> hashTags = tweet.getEntities().getHashTags();
							for (HashTagEntity hash : hashTags) {
								hashTag.append("#"+hash.getText() + " ");
							}
							//System.out.println(hashTag);
							emiter.send(SseEmitter.event().name("streamHashtags").data(hashTag));
						} catch (IOException e) {
							System.out.println("User Disconnected from the Stream");
							//e.printStackTrace();
						}
					}
				}else{
					//Close Stream when all Users are disconnected.
					userStream.close();
					log.info("Zero Connected Users - Closing Stream");
				}
				
			}

			@Override
			public void onLimit(int numberOfLimitedTweets) {
			}

			@Override
			public void onDelete(StreamDeleteEvent deleteEvent) {
			}
		};
		//Start Stream when a User is connected
		if (emitters.size()==1) {
			listeners.add(streamListener);
			userStream = twitter.streamingOperations().sample(listeners);
		}
		
//		Stream from a specific Location:
//		Float west=-122.75f;
//		Float south=36.8f;
//		Float east=-121.75f;
//		Float north = 37.8f;
//
//		FilterStreamParameters filterStreamParameters = new FilterStreamParameters();
//		filterStreamParameters.addLocation(west, south, east, north);
		//Stream userStream = twitter.streamingOperations().filter(filterStreamParameters, listeners);
	
	}
 
Example #17
Source File: DataController.java    From phoenix with Apache License 2.0 4 votes vote down vote up
@RequestMapping(value = "/timeline/{twitterUser}")
public List<Tweet> getUserTimeline(@PathVariable String twitterUser) {
	String inputValue = String.format(twitterView, twitterUser);
logger.error(inputValue);
   return twitterService.getUserTimeline(twitterUser);
}