Created
October 14, 2014 21:06
-
-
Save mberberoglu/d2bec08775078fad6f96 to your computer and use it in GitHub Desktop.
Twitter4j Örnek
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
keywords=Keyword1,Keyword2,Keyword3 | |
consumerKey=######################### | |
consumerSecret==######################### | |
accessToken=######-=######################### | |
accessTokenSecret==######################### | |
dbName=MongoTwiterDB | |
tweetCollectionName=TweetCollection | |
userCollectionName=UserCollection | |
dbHost=HOST | |
dbUser=USER | |
dbPassword=PASSWORD |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mustafab; | |
import com.mongodb.*; | |
import twitter4j.*; | |
import twitter4j.conf.ConfigurationBuilder; | |
import java.io.*; | |
import java.net.UnknownHostException; | |
import java.util.Properties; | |
public class TweetListener { | |
public static void main(String[] args) throws UnknownHostException { | |
final Properties properties = getProperties(); | |
ConfigurationBuilder cb = new ConfigurationBuilder(); | |
cb.setDebugEnabled(true); | |
cb.setOAuthConsumerKey(properties.getProperty("consumerKey")); | |
cb.setOAuthConsumerSecret(properties.getProperty("consumerSecret")); | |
cb.setOAuthAccessToken(properties.getProperty("accessToken")); | |
cb.setOAuthAccessTokenSecret(properties.getProperty("accessTokenSecret")); | |
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); | |
MongoClient mongoClient = new MongoClient(properties.getProperty("dbHost")); | |
final DB db = mongoClient.getDB(properties.getProperty("dbName")); | |
if (properties.getProperty("dbUser").length() > 0 && properties.getProperty("dbPassword").length() > 0) { | |
db.authenticate(properties.getProperty("dbUser"), properties.getProperty("dbPassword").toCharArray()); | |
System.out.println("# DB Authentication => "+db.isAuthenticated()); | |
if (!db.isAuthenticated()) { | |
System.exit(0); | |
} | |
} | |
final DBCollection collTweet = db.getCollection(properties.getProperty("tweetCollectionName")); | |
final DBCollection collUser = db.getCollection(properties.getProperty("userCollectionName")); | |
StatusListener listener = new StatusListener() { | |
@Override | |
public void onStallWarning(StallWarning stallWarning) { | |
//TODO Warning | |
} | |
@Override | |
public void onException(Exception arg0) { | |
//TODO Exception Handling | |
} | |
@Override | |
public void onDeletionNotice(StatusDeletionNotice arg0) { | |
System.out.println("################ Status Delete"+arg0); | |
} | |
@Override | |
public void onScrubGeo(long arg0, long arg1) {} | |
@Override | |
public void onStatus(final Status status) { | |
Runnable runnableTweet = new Runnable(){ | |
public void run(){ | |
BasicDBObject tweet = new BasicDBObject("_id", status.getId()) | |
.append("userId", status.getUser().getId()) | |
.append("createdAT", status.getCreatedAt()) | |
.append("text", status.getText()) | |
.append("favCount", status.getFavoriteCount()) | |
.append("rtCount", status.getRetweetCount()) | |
.append("isRetweet", status.isRetweet()); | |
if (status.getGeoLocation() != null) { | |
tweet.append("hasLocation", true) | |
.append("latitude", status.getGeoLocation().getLatitude()) | |
.append("longitude", status.getGeoLocation().getLongitude()); | |
} else { | |
tweet.append("hasLocation", false); | |
} | |
if (status.isRetweet()) { | |
tweet.append("originUserId", status.getRetweetedStatus().getUser().getId()) | |
.append("originId", status.getRetweetedStatus().getId()); | |
BasicDBObject originTweet = new BasicDBObject("_id", status.getRetweetedStatus().getId()) | |
.append("userId", status.getRetweetedStatus().getUser().getId()) | |
.append("createdAT", status.getRetweetedStatus().getCreatedAt()) | |
.append("text", status.getRetweetedStatus().getText()) | |
.append("favCount", status.getRetweetedStatus().getFavoriteCount()) | |
.append("rtCount", status.getRetweetedStatus().getRetweetCount()) | |
.append("isRetweet", status.getRetweetedStatus().isRetweet()) | |
.append("hasLocation", false); | |
if (status.getRetweetedStatus().getGeoLocation() != null) { | |
originTweet.append("hasLocation", true) | |
.append("latitude", status.getRetweetedStatus().getGeoLocation().getLatitude()) | |
.append("longitude", status.getRetweetedStatus().getGeoLocation().getLongitude()); | |
} | |
collTweet.save(originTweet); | |
} | |
collTweet.save(tweet); | |
BasicDBObject user = new BasicDBObject("_id", status.getUser().getId()) | |
.append("name", status.getUser().getName()) | |
.append("screenName", status.getUser().getScreenName()) | |
.append("image", status.getUser().getProfileImageURL()) | |
.append("isVerified", status.getUser().isVerified()); | |
collUser.save(user); | |
if (status.isRetweet()) { | |
BasicDBObject originUser = new BasicDBObject("_id", status.getRetweetedStatus().getUser().getId()) | |
.append("name", status.getRetweetedStatus().getUser().getName()) | |
.append("screenName", status.getRetweetedStatus().getUser().getScreenName()) | |
.append("image", status.getRetweetedStatus().getUser().getProfileImageURL()) | |
.append("isVerified", status.getRetweetedStatus().getUser().isVerified()); | |
collUser.save(originUser); | |
} | |
System.out.print("#"); | |
} | |
}; | |
Thread threadTweet = new Thread(runnableTweet); | |
threadTweet.start(); | |
} | |
@Override | |
public void onTrackLimitationNotice(final int limit) { | |
//TODO Triigger Search Api for missing tweets | |
} | |
}; | |
FilterQuery fq = new FilterQuery(); | |
String[] keywords = properties.getProperty("keywords").split(","); | |
fq.track(keywords); | |
twitterStream.addListener(listener); | |
twitterStream.filter(fq); | |
} | |
private static Properties getProperties() { | |
Properties prop = new Properties(); | |
InputStream input = null; | |
try { | |
input = new FileInputStream("config.properties"); | |
prop.load(input); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} finally { | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
return prop; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment