OSDN Git Service

aaa
[traindelaybot/source.git] / workspace / .metadata / .plugins / org.eclipse.core.resources / .history / 9 / b07f0731ec6d001110a188bae663c898
diff --git a/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/9/b07f0731ec6d001110a188bae663c898 b/workspace/.metadata/.plugins/org.eclipse.core.resources/.history/9/b07f0731ec6d001110a188bae663c898
new file mode 100644 (file)
index 0000000..9ab9f11
--- /dev/null
@@ -0,0 +1,171 @@
+package com.yuji.tdb.twitter;\r
+\r
+import java.text.DateFormat;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+import java.util.List;\r
+import java.util.TimeZone;\r
+import java.util.logging.Logger;\r
+\r
+import twitter4j.Query;\r
+import twitter4j.QueryResult;\r
+import twitter4j.Status;\r
+import twitter4j.Tweet;\r
+import twitter4j.Twitter;\r
+import twitter4j.TwitterException;\r
+import twitter4j.TwitterFactory;\r
+import twitter4j.auth.AccessToken;\r
+import twitter4j.conf.ConfigurationBuilder;\r
+\r
+import com.yuji.tdb.common.CommonUtil;\r
+import com.yuji.tdb.db.Train;\r
+import com.yuji.tdb.db.TrainDao;\r
+import com.yuji.tdb.utility.StringUtility;\r
+\r
+public class TwitterUtil {\r
+       private static final Logger log = Logger\r
+                       .getLogger(TwitterUtil.class.getName());\r
+       private static TwitterUtil instance = null;\r
+       private static Object obj = new Object();\r
+       //private DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");\r
+       private DateFormat df = new SimpleDateFormat("HH:mm");\r
+\r
+       public static TwitterUtil getInstance() {\r
+               if (instance == null) {\r
+                       synchronized (obj) {\r
+                               if (instance == null) {\r
+                                       instance = new TwitterUtil();\r
+                               }\r
+                       }\r
+               }\r
+               return instance;\r
+       }\r
+\r
+       private TwitterUtil() {\r
+               df.setTimeZone(TimeZone.getTimeZone("JST")); \r
+       }\r
+\r
+       public void main(String consumerKey, String consumerSecret, AccessToken accessToken) {\r
+               try {\r
+                       ConfigurationBuilder confBuilder = new ConfigurationBuilder();\r
+                       confBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)\r
+                                       .setOAuthConsumerSecret(consumerSecret);\r
+                       TwitterFactory factory = new TwitterFactory(confBuilder.build());\r
+                       Twitter twitter = factory.getInstance(accessToken);\r
+\r
+                       List<Train> list = getTrainList();\r
+                       for (Train train : list){\r
+                               twite(twitter, train);\r
+                       }\r
+               } catch (TwitterException e) {\r
+                       // TODO Auto-generated catch block\r
+                       e.printStackTrace();\r
+                       log.severe(e.toString());\r
+               }\r
+       }\r
+\r
+       private void twite(Twitter twitter, Train train) throws TwitterException {\r
+               Query query = new Query();\r
+               //query.setRpp(1000); // TODO\r
+               query.setQuery(train.getSearchWord());\r
+               QueryResult result = twitter.search(query);\r
+               List<Tweet> tweets = result.getTweets();\r
+               \r
+               Calendar cal = Calendar.getInstance();\r
+               Date date = cal.getTime();\r
+               long cur = date.getTime();\r
+\r
+               int count = 0;\r
+               int hit = 0;\r
+               int mhit = 0;\r
+               \r
+               int index;\r
+               int mindex = 0;\r
+               int size = tweets.size();\r
+               for (index = 0; index < size; index++) {\r
+                       Tweet tweet = tweets.get(index);\r
+                       \r
+                       Date at = tweet.getCreatedAt();\r
+                       long t = at.getTime();\r
+                       \r
+                       String message = tweet.getText();\r
+                       \r
+                       if (t < cur - 30 * 60 * 1000){\r
+                               System.out.println("\81~" + tweet.getFromUser() + " - "\r
+                                               + tweet.getText() + at);\r
+                               continue;\r
+                       }\r
+                       hit = filter(message);\r
+                       if (hit <= 0) {\r
+                               System.out.println("\81¢" + tweet.getFromUser() + " - "\r
+                                               + tweet.getText() + at);\r
+                               // \92x\89\84\88È\8aO\82Ì\8f\91\82«\8d\9e\82Ý\r
+                               continue;\r
+                       }\r
+                       if (hit > mhit){\r
+                               hit = mhit;\r
+                               mindex = index;\r
+                       }\r
+\r
+                       System.out.println("@" + tweet.getFromUser() + " - "\r
+                                       + tweet.getText() + at);\r
+                       count++;\r
+               }\r
+\r
+               String message = "[" + train.getName() + "] ";\r
+               if (count < train.getCount()){\r
+                       message += df.format(date) + " \92x\89\84\82È\82µ";       \r
+               }\r
+               else {\r
+                       int N = 100;\r
+                       \r
+                       String sample = tweets.get(mindex).getText();\r
+                       sample = CommonUtil.replaceString(sample, "@", "(a)");\r
+                       sample = StringUtility.parseSubstring(sample, N);\r
+                       message += df.format(date) + " " + sample + " count=" + count;                                                  \r
+               }\r
+               System.out.println(message); //TODO\r
+               Status status = twitter.updateStatus(message); //TODO\r
+       }\r
+       \r
+       private List<Train> getTrainList(){\r
+               TrainDao dao = TrainDao.getInstance();\r
+               List<Train> list = dao.search();\r
+               if (list.size() <= 0){\r
+                       Train train;\r
+                       \r
+                       train = new Train("\8b\9e\95l\93\8c\96k\90ü", "\8b\9e\95l\93\8c\96k", 5);\r
+                       dao.put(train);\r
+                       train = new Train("\93\8c\8aC\93¹\90ü", "\93\8c\8aC\93¹", 5);\r
+                       dao.put(train);\r
+                       train = new Train("\8b\9e\95l\8b}\8ds\90ü", "\8b\9e\8b}", 5);\r
+                       dao.put(train);\r
+                       list = dao.search();\r
+               }\r
+               return list;\r
+       }\r
+       \r
+       private static List<String> keywords = null;\r
+\r
+       public int filter(String text) {\r
+               if (keywords == null) {\r
+                       keywords = new ArrayList<String>();\r
+                       keywords.add("\92x\89\84");\r
+                       keywords.add("\92x\82ê");\r
+                       keywords.add("\90U\82è\91Ö\82¦");\r
+                       keywords.add("\90U\91Ö");\r
+                       keywords.add("\92â\8e~");\r
+                       keywords.add("\8e\96\8cÌ");\r
+               }\r
+\r
+               int count = 0;\r
+               for (String keyword : keywords) {\r
+                       if (text.indexOf(keyword) >= 0) {\r
+                               count++;\r
+                       }\r
+               }\r
+               return count;\r
+       }\r
+}\r