Twitter4J (#1300)
* rest with spark java * 4 * Update Application.java * indentation changes * spring @requestmapping shortcuts * removing spring requestmapping and pushing spring-mvc-java * Joining/Splitting Strings with Java and Stream API * adding more join/split functionality * changing package name * testcase change * adding webutils * adding testcase for WebUtils and ServletRequestUtils * adding testcase * spring-security-stormpath * Twitter4J * Update Application.java * Update ApplicationTest.java * Update twitter4j.properties
This commit is contained in:
parent
5392bdd8f3
commit
57259c4a06
|
@ -0,0 +1,58 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.mabsisa</groupId>
|
||||
<artifactId>Twitter4J</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Twitter4J</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-core</artifactId>
|
||||
<version>4.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-stream</artifactId>
|
||||
<version>4.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.19.1</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/ApplicationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,117 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import twitter4j.DirectMessage;
|
||||
import twitter4j.Query;
|
||||
import twitter4j.QueryResult;
|
||||
import twitter4j.StallWarning;
|
||||
import twitter4j.Status;
|
||||
import twitter4j.StatusDeletionNotice;
|
||||
import twitter4j.StatusListener;
|
||||
import twitter4j.Twitter;
|
||||
import twitter4j.TwitterException;
|
||||
import twitter4j.TwitterFactory;
|
||||
import twitter4j.TwitterStream;
|
||||
import twitter4j.TwitterStreamFactory;
|
||||
import twitter4j.conf.ConfigurationBuilder;
|
||||
|
||||
public class Application {
|
||||
|
||||
public static Twitter getTwitterinstance() {
|
||||
/**
|
||||
* if not using properties file, we can set access token by following way
|
||||
*/
|
||||
// ConfigurationBuilder cb = new ConfigurationBuilder();
|
||||
// cb.setDebugEnabled(true)
|
||||
// .setOAuthConsumerKey("DPHTBsWWO42d8rzshxlK0OwSY")
|
||||
// .setOAuthConsumerSecret("ACLXkeRY98NiaVCG1ai8fdYt0GoEGJbFeTuxjulSCO7sLKqls1")
|
||||
// .setOAuthAccessToken("838080188214759428-9MSK1ddPTN5ZZHbddjFI7s75mYgmCFQ")
|
||||
// .setOAuthAccessTokenSecret("1eXAADpHShAzQh5hGWLBUQHLysOuAKIOapmQQ8U0OVk2c");
|
||||
//
|
||||
// TwitterFactory tf = new TwitterFactory(cb.build());
|
||||
// Twitter twitter = tf.getSingleton();
|
||||
|
||||
Twitter twitter = TwitterFactory.getSingleton();
|
||||
return twitter;
|
||||
|
||||
}
|
||||
|
||||
public static String createTweet(String tweet) throws TwitterException {
|
||||
Twitter twitter = getTwitterinstance();
|
||||
Status status = twitter.updateStatus("creating baeldung API");
|
||||
return status.getText();
|
||||
}
|
||||
|
||||
public static List<String> getTimeLine() throws TwitterException {
|
||||
Twitter twitter = getTwitterinstance();
|
||||
List<Status> statuses = twitter.getHomeTimeline();
|
||||
return statuses.stream().map(
|
||||
item -> item.getText()).collect(
|
||||
Collectors.toList());
|
||||
}
|
||||
|
||||
public static String sendDirectMessage(String recipientName, String msg) throws TwitterException {
|
||||
Twitter twitter = getTwitterinstance();
|
||||
DirectMessage message = twitter.sendDirectMessage(recipientName, msg);
|
||||
return message.getText();
|
||||
}
|
||||
|
||||
public static List<String> searchtweets() throws TwitterException {
|
||||
Twitter twitter = getTwitterinstance();
|
||||
Query query = new Query("source:twitter4j baeldung");
|
||||
QueryResult result = twitter.search(query);
|
||||
List<Status> statuses = result.getTweets();
|
||||
return statuses.stream().map(
|
||||
item -> item.getText()).collect(
|
||||
Collectors.toList());
|
||||
}
|
||||
|
||||
public static void streamFeed() {
|
||||
|
||||
StatusListener listener = new StatusListener(){
|
||||
|
||||
@Override
|
||||
public void onException(Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDeletionNotice(StatusDeletionNotice arg) {
|
||||
System.out.println("Got a status deletion notice id:" + arg.getStatusId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onScrubGeo(long userId, long upToStatusId) {
|
||||
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStallWarning(StallWarning warning) {
|
||||
System.out.println("Got stall warning:" + warning);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStatus(Status status) {
|
||||
System.out.println(status.getUser().getName() + " : " + status.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
|
||||
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
|
||||
}
|
||||
};
|
||||
|
||||
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
|
||||
|
||||
twitterStream.addListener(listener);
|
||||
|
||||
twitterStream.sample();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
oauth.consumerKey=//TODO
|
||||
oauth.consumerSecret=//TODO
|
||||
oauth.accessToken=//TODO
|
||||
oauth.accessTokenSecret=//TODO
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import twitter4j.TwitterException;
|
||||
|
||||
public class ApplicationTest {
|
||||
|
||||
/**
|
||||
* In order run this jUnit test you need to configure your API details in the twitter4j.properties
|
||||
*/
|
||||
|
||||
String tweet = "baeldung is awsome";
|
||||
|
||||
@Test
|
||||
public void givenText_updateStatus() throws TwitterException {
|
||||
String text = Application.createTweet(tweet);
|
||||
assertEquals(tweet, text);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCredential_fetchStatus() throws TwitterException {
|
||||
List<String> statuses = Application.getTimeLine();
|
||||
List<String> expectedStatuses = new ArrayList<String>();
|
||||
expectedStatuses.add(tweet);
|
||||
assertEquals(expectedStatuses, statuses);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRecipientNameAndMessage_sendDirectMessage() throws TwitterException {
|
||||
String msg = Application.sendDirectMessage("YOUR_RECCIPIENT_ID", tweet);
|
||||
assertEquals(msg, tweet);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue