Skip to content

Commit 57259c4

Browse files
AbhinabKanrarmaibin
authored andcommitted
Twitter4J (eugenp#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
1 parent 5392bdd commit 57259c4

9 files changed

Lines changed: 219 additions & 0 deletions

File tree

Twitter4J/pom.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.mabsisa</groupId>
5+
<artifactId>Twitter4J</artifactId>
6+
<packaging>jar</packaging>
7+
<version>1.0-SNAPSHOT</version>
8+
<name>Twitter4J</name>
9+
<url>http://maven.apache.org</url>
10+
11+
<properties>
12+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
13+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
14+
<java.version>1.8</java.version>
15+
<maven.compiler.source>1.8</maven.compiler.source>
16+
<maven.compiler.target>1.8</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.twitter4j</groupId>
22+
<artifactId>twitter4j-core</artifactId>
23+
<version>4.0.6</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>org.twitter4j</groupId>
27+
<artifactId>twitter4j-stream</artifactId>
28+
<version>4.0.6</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit</artifactId>
33+
<version>4.12</version>
34+
</dependency>
35+
</dependencies>
36+
37+
<build>
38+
<finalName>${project.artifactId}</finalName>
39+
<resources>
40+
<resource>
41+
<directory>src/main/resources</directory>
42+
</resource>
43+
</resources>
44+
<plugins>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-surefire-plugin</artifactId>
48+
<version>2.19.1</version>
49+
<configuration>
50+
<excludes>
51+
<exclude>**/ApplicationTest.java</exclude>
52+
</excludes>
53+
</configuration>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/**
2+
*
3+
*/
4+
package com.baeldung;
5+
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
import twitter4j.DirectMessage;
10+
import twitter4j.Query;
11+
import twitter4j.QueryResult;
12+
import twitter4j.StallWarning;
13+
import twitter4j.Status;
14+
import twitter4j.StatusDeletionNotice;
15+
import twitter4j.StatusListener;
16+
import twitter4j.Twitter;
17+
import twitter4j.TwitterException;
18+
import twitter4j.TwitterFactory;
19+
import twitter4j.TwitterStream;
20+
import twitter4j.TwitterStreamFactory;
21+
import twitter4j.conf.ConfigurationBuilder;
22+
23+
public class Application {
24+
25+
public static Twitter getTwitterinstance() {
26+
/**
27+
* if not using properties file, we can set access token by following way
28+
*/
29+
// ConfigurationBuilder cb = new ConfigurationBuilder();
30+
// cb.setDebugEnabled(true)
31+
// .setOAuthConsumerKey("DPHTBsWWO42d8rzshxlK0OwSY")
32+
// .setOAuthConsumerSecret("ACLXkeRY98NiaVCG1ai8fdYt0GoEGJbFeTuxjulSCO7sLKqls1")
33+
// .setOAuthAccessToken("838080188214759428-9MSK1ddPTN5ZZHbddjFI7s75mYgmCFQ")
34+
// .setOAuthAccessTokenSecret("1eXAADpHShAzQh5hGWLBUQHLysOuAKIOapmQQ8U0OVk2c");
35+
//
36+
// TwitterFactory tf = new TwitterFactory(cb.build());
37+
// Twitter twitter = tf.getSingleton();
38+
39+
Twitter twitter = TwitterFactory.getSingleton();
40+
return twitter;
41+
42+
}
43+
44+
public static String createTweet(String tweet) throws TwitterException {
45+
Twitter twitter = getTwitterinstance();
46+
Status status = twitter.updateStatus("creating baeldung API");
47+
return status.getText();
48+
}
49+
50+
public static List<String> getTimeLine() throws TwitterException {
51+
Twitter twitter = getTwitterinstance();
52+
List<Status> statuses = twitter.getHomeTimeline();
53+
return statuses.stream().map(
54+
item -> item.getText()).collect(
55+
Collectors.toList());
56+
}
57+
58+
public static String sendDirectMessage(String recipientName, String msg) throws TwitterException {
59+
Twitter twitter = getTwitterinstance();
60+
DirectMessage message = twitter.sendDirectMessage(recipientName, msg);
61+
return message.getText();
62+
}
63+
64+
public static List<String> searchtweets() throws TwitterException {
65+
Twitter twitter = getTwitterinstance();
66+
Query query = new Query("source:twitter4j baeldung");
67+
QueryResult result = twitter.search(query);
68+
List<Status> statuses = result.getTweets();
69+
return statuses.stream().map(
70+
item -> item.getText()).collect(
71+
Collectors.toList());
72+
}
73+
74+
public static void streamFeed() {
75+
76+
StatusListener listener = new StatusListener(){
77+
78+
@Override
79+
public void onException(Exception e) {
80+
e.printStackTrace();
81+
}
82+
83+
@Override
84+
public void onDeletionNotice(StatusDeletionNotice arg) {
85+
System.out.println("Got a status deletion notice id:" + arg.getStatusId());
86+
}
87+
88+
@Override
89+
public void onScrubGeo(long userId, long upToStatusId) {
90+
System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
91+
}
92+
93+
@Override
94+
public void onStallWarning(StallWarning warning) {
95+
System.out.println("Got stall warning:" + warning);
96+
}
97+
98+
@Override
99+
public void onStatus(Status status) {
100+
System.out.println(status.getUser().getName() + " : " + status.getText());
101+
}
102+
103+
@Override
104+
public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
105+
System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
106+
}
107+
};
108+
109+
TwitterStream twitterStream = new TwitterStreamFactory().getInstance();
110+
111+
twitterStream.addListener(listener);
112+
113+
twitterStream.sample();
114+
115+
}
116+
117+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
oauth.consumerKey=//TODO
2+
oauth.consumerSecret=//TODO
3+
oauth.accessToken=//TODO
4+
oauth.accessTokenSecret=//TODO
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import org.junit.Test;
9+
10+
import twitter4j.TwitterException;
11+
12+
public class ApplicationTest {
13+
14+
/**
15+
* In order run this jUnit test you need to configure your API details in the twitter4j.properties
16+
*/
17+
18+
String tweet = "baeldung is awsome";
19+
20+
@Test
21+
public void givenText_updateStatus() throws TwitterException {
22+
String text = Application.createTweet(tweet);
23+
assertEquals(tweet, text);
24+
}
25+
26+
@Test
27+
public void givenCredential_fetchStatus() throws TwitterException {
28+
List<String> statuses = Application.getTimeLine();
29+
List<String> expectedStatuses = new ArrayList<String>();
30+
expectedStatuses.add(tweet);
31+
assertEquals(expectedStatuses, statuses);
32+
}
33+
34+
@Test
35+
public void givenRecipientNameAndMessage_sendDirectMessage() throws TwitterException {
36+
String msg = Application.sendDirectMessage("YOUR_RECCIPIENT_ID", tweet);
37+
assertEquals(msg, tweet);
38+
}
39+
40+
}

core-java/0.004102810554955205

Whitespace-only changes.

core-java/0.04832801936270381

Whitespace-only changes.

core-java/0.5633433244738808

Whitespace-only changes.

core-java/0.6256429734439612

Whitespace-only changes.

core-java/0.9799201796740292

Whitespace-only changes.

0 commit comments

Comments
 (0)