Skip to content

Commit

Permalink
TFJ-443 adding search API example
Browse files Browse the repository at this point in the history
  • Loading branch information
yusuke committed Nov 20, 2010
1 parent e286baa commit d3f45ab
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 3 deletions.
9 changes: 9 additions & 0 deletions twitter4j-examples/bin/search/searchTweets.cmd
@@ -0,0 +1,9 @@
echo off
SETLOCAL enabledelayedexpansion
cd ..
call setEnv.cmd

echo on
"%JAVA%" %MEM_ARGS% -classpath "%CLASSPATH%" twitter4j.examples.search.SearchTweets %*

ENDLOCAL
6 changes: 6 additions & 0 deletions twitter4j-examples/bin/search/searchTweets.sh
@@ -0,0 +1,6 @@
#/bin/sh
cd ..
. ./setEnv.sh
RUN_CMD="$JAVA_HOME/bin/java $MEM_ARGS -cp $CLASSPATH twitter4j.examples.search.SearchTweets $@"
echo $RUN_CMD
exec $RUN_CMD
@@ -0,0 +1,71 @@
/*
Copyright (c) 2007-2010, Yusuke Yamamoto
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Yusuke Yamamoto nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package twitter4j.examples.search;

import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Tweet;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;

import java.io.*;
import java.util.List;
import java.util.Properties;

/**
* @author Yusuke Yamamoto - yusuke at mac.com
* @since Twitter4J 2.1.7
*/
public class SearchTweets {
/**
* Usage: java twitter4j.examples.search SearchTweets [query]
*
* @param args
*/
public static void main(String[] args) {
if(args.length < 1){
System.out.println("java twitter4j.examples.search SearchTweets [query]");
System.exit(-1);
}
Twitter twitter = new TwitterFactory().getInstance();
try {
QueryResult result = twitter.search(new Query(args[0]));
List<Tweet> tweets = result.getTweets();
for (Tweet tweet : tweets) {
System.out.println("@" + tweet.getFromUser() + " - " + tweet.getText());
}
System.exit(0);
} catch (TwitterException te) {
te.printStackTrace();
System.out.println("Failed to search tweets: " + te.getMessage());
System.exit(-1);
}
}
}
Expand Up @@ -35,6 +35,7 @@

/**
* Example application that gets public, user and friend timeline using specified account.<br>
* Usage: java twitter4j.examples.GetPublicTimeline
* @author Yusuke Yamamoto - yusuke at mac.com
*/
public final class GetPublicTimeline {
Expand All @@ -43,11 +44,10 @@ public final class GetPublicTimeline {
* @param args String[]
*/
public static void main(String[] args) {

Twitter unauthenticatedTwitter = new TwitterFactory().getInstance();
Twitter twitter = new TwitterFactory().getInstance();
System.out.println("Showing public timeline.");
try {
List<Status> statuses = unauthenticatedTwitter.getPublicTimeline();
List<Status> statuses = twitter.getPublicTimeline();
for (Status status : statuses) {
System.out.println(status.getUser().getName() + ":" +
status.getText());
Expand Down

0 comments on commit d3f45ab

Please sign in to comment.