diff --git a/Twitter4J/pom.xml b/Twitter4J/pom.xml new file mode 100644 index 0000000000..ebe5a7d409 --- /dev/null +++ b/Twitter4J/pom.xml @@ -0,0 +1,58 @@ + + 4.0.0 + com.mabsisa + Twitter4J + jar + 1.0-SNAPSHOT + Twitter4J + http://maven.apache.org + + + UTF-8 + UTF-8 + 1.8 + 1.8 + 1.8 + + + + + org.twitter4j + twitter4j-core + 4.0.6 + + + org.twitter4j + twitter4j-stream + 4.0.6 + + + junit + junit + 4.12 + + + + + ${project.artifactId} + + + src/main/resources + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + **/ApplicationTest.java + + + + + + + diff --git a/Twitter4J/src/main/java/com/baeldung/Application.java b/Twitter4J/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..f7da27db29 --- /dev/null +++ b/Twitter4J/src/main/java/com/baeldung/Application.java @@ -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 getTimeLine() throws TwitterException { + Twitter twitter = getTwitterinstance(); + List 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 searchtweets() throws TwitterException { + Twitter twitter = getTwitterinstance(); + Query query = new Query("source:twitter4j baeldung"); + QueryResult result = twitter.search(query); + List 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(); + + } + +} diff --git a/Twitter4J/src/main/resources/twitter4j.properties b/Twitter4J/src/main/resources/twitter4j.properties new file mode 100644 index 0000000000..ee11dc62a1 --- /dev/null +++ b/Twitter4J/src/main/resources/twitter4j.properties @@ -0,0 +1,4 @@ +oauth.consumerKey=//TODO +oauth.consumerSecret=//TODO +oauth.accessToken=//TODO +oauth.accessTokenSecret=//TODO diff --git a/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java b/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java new file mode 100644 index 0000000000..a1c778679c --- /dev/null +++ b/Twitter4J/src/test/java/com/baeldung/ApplicationTest.java @@ -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 statuses = Application.getTimeLine(); + List expectedStatuses = new ArrayList(); + expectedStatuses.add(tweet); + assertEquals(expectedStatuses, statuses); + } + + @Test + public void givenRecipientNameAndMessage_sendDirectMessage() throws TwitterException { + String msg = Application.sendDirectMessage("YOUR_RECCIPIENT_ID", tweet); + assertEquals(msg, tweet); + } + +} diff --git a/core-java/0.004102810554955205 b/core-java/0.004102810554955205 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.04832801936270381 b/core-java/0.04832801936270381 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.5633433244738808 b/core-java/0.5633433244738808 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.6256429734439612 b/core-java/0.6256429734439612 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/core-java/0.9799201796740292 b/core-java/0.9799201796740292 new file mode 100644 index 0000000000..e69de29bb2