X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=workspace%2F.metadata%2F.plugins%2Forg.eclipse.core.resources%2F.history%2Fc%2F30f7a5ad2f7200111835fa7adce7cfd2;fp=workspace%2F.metadata%2F.plugins%2Forg.eclipse.core.resources%2F.history%2Fc%2F30f7a5ad2f7200111835fa7adce7cfd2;h=9350088b698c03e32ec48c69e487e8a4478b4f31;hb=07d63fb4ad57a32cdde8e8882a8f86151e6d1d3f;hp=0000000000000000000000000000000000000000;hpb=9be7d50b45c555b120e4ec8fa8b5928afa3cc942;p=traindelaybot%2Fsource.git diff --git a/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/c/30f7a5ad2f7200111835fa7adce7cfd2 b/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/c/30f7a5ad2f7200111835fa7adce7cfd2 new file mode 100644 index 0000000..9350088 --- /dev/null +++ b/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/c/30f7a5ad2f7200111835fa7adce7cfd2 @@ -0,0 +1,238 @@ +package com.yuji.tdb.twitter; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.TimeZone; +import java.util.logging.Logger; + +import twitter4j.Query; +import twitter4j.QueryResult; +import twitter4j.Status; +import twitter4j.Tweet; +import twitter4j.Twitter; +import twitter4j.TwitterException; +import twitter4j.TwitterFactory; +import twitter4j.auth.AccessToken; +import twitter4j.conf.ConfigurationBuilder; + +import com.yuji.tdb.common.CommonUtil; +import com.yuji.tdb.db.KeyValueDao; +import com.yuji.tdb.db.Train; +import com.yuji.tdb.db.TrainDao; +import com.yuji.tdb.debug.Debug; +import com.yuji.tdb.utility.StringUtility; + +public class TwitterUtil { + private static final Logger log = Logger + .getLogger(TwitterUtil.class.getName()); + private static TwitterUtil instance = null; + private static Object obj = new Object(); + //private DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); + private DateFormat df = new SimpleDateFormat("HH:mm"); + private static List keywords = null; + private long searchPeriod = 0; + private long twitPeriod = 0; + private Map twitTimeList = new HashMap(); + + public static TwitterUtil getInstance() { + if (instance == null) { + synchronized (obj) { + if (instance == null) { + instance = new TwitterUtil(); + } + } + } + return instance; + } + + private TwitterUtil() { + df.setTimeZone(TimeZone.getTimeZone("JST")); + } + + public void main(String consumerKey, String consumerSecret, AccessToken accessToken) { + try { + ConfigurationBuilder confBuilder = new ConfigurationBuilder(); + confBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey) + .setOAuthConsumerSecret(consumerSecret); + TwitterFactory factory = new TwitterFactory(confBuilder.build()); + Twitter twitter = factory.getInstance(accessToken); + + List list = getTrainList(); + for (Train train : list){ + Calendar cal = Calendar.getInstance(); + Date date = cal.getTime(); + long cur = date.getTime(); + + long id = train.getId(); + long t = getTwitTime(id); + long period = getTwitPeriod(); + + if (t > 0 && t + period < cur){ + continue; + } +// twite(twitter, train); + } +// } catch (TwitterException e) { +// // TODO Auto-generated catch block +// e.printStackTrace(); +// log.severe(e.toString()); + } + } + + private void twite(Twitter twitter, Train train) throws TwitterException { + Query query = new Query(); + //query.setRpp(1000); // TODO + query.setQuery(train.getSearchWord()); + QueryResult result = null; + try { + result = twitter.search(query); + } + catch (TwitterException e){ + log.severe("" + Debug.getLineNo()); + throw e; + } + List tweets = result.getTweets(); + + Calendar cal = Calendar.getInstance(); + Date date = cal.getTime(); + long cur = date.getTime(); + + int count = 0; + int hit = 0; + int mhit = 0; + long period = getSearchPeriod(); + long id = train.getId(); + + int index; + int mindex = 0; + int size = tweets.size(); + for (index = 0; index < size; index++) { + Tweet tweet = tweets.get(index); + + Date at = tweet.getCreatedAt(); + long t = at.getTime(); + + String message = tweet.getText(); + + if (t < cur - period * 60 * 1000){ + System.out.println("~" + tweet.getFromUser() + " - " + + tweet.getText() + at); + continue; + } + hit = filter(message); + if (hit <= 0) { + System.out.println("¢" + tweet.getFromUser() + " - " + + tweet.getText() + at); + // ’x‰„ˆÈŠO‚̏‘‚«ž‚Ý + continue; + } + if (hit > mhit){ + hit = mhit; + mindex = index; + } + + System.out.println("@" + tweet.getFromUser() + " - " + + tweet.getText() + at); + count++; + } + + String message = "[" + train.getName() + "] "; + if (count < train.getCount()){ + message += df.format(date) + " ’x‰„‚È‚µ"; + } + else { + int N = 100; + + String sample = tweets.get(mindex).getText(); + sample = CommonUtil.replaceString(sample, "@", "(a)"); + sample = StringUtility.parseSubstring(sample, N); + message += df.format(date) + " " + sample + " count=" + count; + } + System.out.println(message); //TODO + + Status status = null; + try { + status = twitter.updateStatus(message); //TODO + setTwitTime(id, cur); + } + catch (TwitterException e){ + log.severe(Debug.getLineNo() + " " + message); + log.severe(Debug.getLineNo() + " " + status); + throw e; + } + } + + private List getTrainList(){ + TrainDao dao = TrainDao.getInstance(); + List list = dao.search(); + if (list.size() <= 0){ + Train train; + + train = new Train("‹ž•l“Œ–kü", "‹ž•l“Œ–k", 5); + dao.put(train); + train = new Train("“ŒŠC“¹ü", "“ŒŠC“¹", 5); + dao.put(train); + train = new Train("‹ž•l‹}sü", "‹ž‹}", 5); + dao.put(train); + list = dao.search(); + } + return list; + } + + public int filter(String text) { + if (keywords == null) { + keywords = new ArrayList(); + keywords.add("’x‰„"); + keywords.add("’x‚ê"); + keywords.add("U‚è‘Ö‚¦"); + keywords.add("U‘Ö"); + keywords.add("’âŽ~"); + keywords.add("Ž–ŒÌ"); + } + + int count = 0; + for (String keyword : keywords) { + if (text.indexOf(keyword) >= 0) { + count++; + } + } + return count; + } + + public long getSearchPeriod(){ + if (searchPeriod <= 0){ + KeyValueDao dao = KeyValueDao.getInstance(); + searchPeriod = dao.getInt(KeyValueDao.KEY_SEARCH_PERIOD, 5); + } + return searchPeriod; + } + + public long getTwitPeriod(){ + if (twitPeriod <= 0){ + KeyValueDao dao = KeyValueDao.getInstance(); + twitPeriod = dao.getInt(KeyValueDao.KEY_TWIT_PERIOD, 5); + } + return twitPeriod; + } + + private long getTwitTime(long id){ + String key = KeyValueDao.KEY_TWIT_TIME + id; + Long value = twitTimeList.get(key); + if (value == null){ + value = 0L; + twitTimeList.put(key, value); + } + return value; + } + + private void setTwitTime(long id, long value){ + String key = KeyValueDao.KEY_TWIT_TIME + id; + twitTimeList.put(key, value); + } +}