OSDN Git Service

921c3c4a02c1639c9a9b1e213b0bddf672674a73
[traindelaybot/source.git] / workspace / .metadata / .plugins / org.eclipse.core.resources / .history / a1 / 00485adb2c7200111835fa7adce7cfd2
1 package com.yuji.tdb.twitter;\r
2 \r
3 import java.text.DateFormat;\r
4 import java.text.SimpleDateFormat;\r
5 import java.util.ArrayList;\r
6 import java.util.Calendar;\r
7 import java.util.Date;\r
8 import java.util.HashMap;\r
9 import java.util.List;\r
10 import java.util.Map;\r
11 import java.util.TimeZone;\r
12 import java.util.logging.Logger;\r
13 \r
14 import twitter4j.Query;\r
15 import twitter4j.QueryResult;\r
16 import twitter4j.Status;\r
17 import twitter4j.Tweet;\r
18 import twitter4j.Twitter;\r
19 import twitter4j.TwitterException;\r
20 import twitter4j.TwitterFactory;\r
21 import twitter4j.auth.AccessToken;\r
22 import twitter4j.conf.ConfigurationBuilder;\r
23 \r
24 import com.yuji.tdb.common.CommonUtil;\r
25 import com.yuji.tdb.db.KeyValueDao;\r
26 import com.yuji.tdb.db.Train;\r
27 import com.yuji.tdb.db.TrainDao;\r
28 import com.yuji.tdb.debug.Debug;\r
29 import com.yuji.tdb.utility.StringUtility;\r
30 \r
31 public class TwitterUtil {\r
32         private static final Logger log = Logger\r
33                         .getLogger(TwitterUtil.class.getName());\r
34         private static TwitterUtil instance = null;\r
35         private static Object obj = new Object();\r
36         //private DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");\r
37         private DateFormat df = new SimpleDateFormat("HH:mm");\r
38         private static List<String> keywords = null;\r
39         private long searchPeriod = 0;\r
40         private long twitPeriod = 0;\r
41         private Map<String, Long> twitTimeList = new HashMap<String, Long>();\r
42         \r
43         public static TwitterUtil getInstance() {\r
44                 if (instance == null) {\r
45                         synchronized (obj) {\r
46                                 if (instance == null) {\r
47                                         instance = new TwitterUtil();\r
48                                 }\r
49                         }\r
50                 }\r
51                 return instance;\r
52         }\r
53 \r
54         private TwitterUtil() {\r
55                 df.setTimeZone(TimeZone.getTimeZone("JST")); \r
56         }\r
57 \r
58         public void main(String consumerKey, String consumerSecret, AccessToken accessToken) {\r
59                 try {\r
60                         ConfigurationBuilder confBuilder = new ConfigurationBuilder();\r
61                         confBuilder.setDebugEnabled(true).setOAuthConsumerKey(consumerKey)\r
62                                         .setOAuthConsumerSecret(consumerSecret);\r
63                         TwitterFactory factory = new TwitterFactory(confBuilder.build());\r
64                         Twitter twitter = factory.getInstance(accessToken);\r
65 \r
66                         List<Train> list = getTrainList();\r
67                         for (Train train : list){\r
68                                 Calendar cal = Calendar.getInstance();\r
69                                 Date date = cal.getTime();\r
70                                 long cur = date.getTime();\r
71 \r
72                                 long id = train.getId();\r
73                                 long t = getTwitTime(id);\r
74                                 long period = getTwitPeriod();\r
75                                 \r
76                                 if (t + period < cur){\r
77                                         continue;\r
78                                 }\r
79                                 twite(twitter, train);\r
80                         }\r
81                 } catch (TwitterException e) {\r
82                         // TODO Auto-generated catch block\r
83                         e.printStackTrace();\r
84                         log.severe(e.toString());\r
85                 }\r
86         }\r
87 \r
88         private void twite(Twitter twitter, Train train) throws TwitterException {\r
89                 Query query = new Query();\r
90                 //query.setRpp(1000); // TODO\r
91                 query.setQuery(train.getSearchWord());\r
92                 QueryResult result = null;\r
93                 try {\r
94                         result = twitter.search(query);\r
95                 }\r
96                 catch (TwitterException e){\r
97                         log.severe("" + Debug.getLineNo());\r
98                         throw e;\r
99                 }\r
100                 List<Tweet> tweets = result.getTweets();\r
101                 \r
102                 Calendar cal = Calendar.getInstance();\r
103                 Date date = cal.getTime();\r
104                 long cur = date.getTime();\r
105 \r
106                 int count = 0;\r
107                 int hit = 0;\r
108                 int mhit = 0;\r
109                 int period = getSearchPeriod();\r
110                 \r
111                 int index;\r
112                 int mindex = 0;\r
113                 int size = tweets.size();\r
114                 for (index = 0; index < size; index++) {\r
115                         Tweet tweet = tweets.get(index);\r
116                         \r
117                         Date at = tweet.getCreatedAt();\r
118                         long t = at.getTime();\r
119                         \r
120                         String message = tweet.getText();\r
121                         \r
122                         if (t < cur - period * 60 * 1000){\r
123                                 System.out.println("\81~" + tweet.getFromUser() + " - "\r
124                                                 + tweet.getText() + at);\r
125                                 continue;\r
126                         }\r
127                         hit = filter(message);\r
128                         if (hit <= 0) {\r
129                                 System.out.println("\81¢" + tweet.getFromUser() + " - "\r
130                                                 + tweet.getText() + at);\r
131                                 // \92x\89\84\88È\8aO\82Ì\8f\91\82«\8d\9e\82Ý\r
132                                 continue;\r
133                         }\r
134                         if (hit > mhit){\r
135                                 hit = mhit;\r
136                                 mindex = index;\r
137                         }\r
138 \r
139                         System.out.println("@" + tweet.getFromUser() + " - "\r
140                                         + tweet.getText() + at);\r
141                         count++;\r
142                 }\r
143 \r
144                 String message = "[" + train.getName() + "] ";\r
145                 if (count < train.getCount()){\r
146                         message += df.format(date) + " \92x\89\84\82È\82µ";       \r
147                 }\r
148                 else {\r
149                         int N = 100;\r
150                         \r
151                         String sample = tweets.get(mindex).getText();\r
152                         sample = CommonUtil.replaceString(sample, "@", "(a)");\r
153                         sample = StringUtility.parseSubstring(sample, N);\r
154                         message += df.format(date) + " " + sample + " count=" + count;                                                  \r
155                 }\r
156                 System.out.println(message); //TODO\r
157                 \r
158                 Status status = null;\r
159                 try {\r
160                         status = twitter.updateStatus(message); //TODO\r
161                 }\r
162                 catch (TwitterException e){\r
163                         log.severe(Debug.getLineNo() + " " + message);\r
164                         throw e;\r
165                 }\r
166         }\r
167         \r
168         private List<Train> getTrainList(){\r
169                 TrainDao dao = TrainDao.getInstance();\r
170                 List<Train> list = dao.search();\r
171                 if (list.size() <= 0){\r
172                         Train train;\r
173                         \r
174                         train = new Train("\8b\9e\95l\93\8c\96k\90ü", "\8b\9e\95l\93\8c\96k", 5);\r
175                         dao.put(train);\r
176                         train = new Train("\93\8c\8aC\93¹\90ü", "\93\8c\8aC\93¹", 5);\r
177                         dao.put(train);\r
178                         train = new Train("\8b\9e\95l\8b}\8ds\90ü", "\8b\9e\8b}", 5);\r
179                         dao.put(train);\r
180                         list = dao.search();\r
181                 }\r
182                 return list;\r
183         }\r
184         \r
185         public int filter(String text) {\r
186                 if (keywords == null) {\r
187                         keywords = new ArrayList<String>();\r
188                         keywords.add("\92x\89\84");\r
189                         keywords.add("\92x\82ê");\r
190                         keywords.add("\90U\82è\91Ö\82¦");\r
191                         keywords.add("\90U\91Ö");\r
192                         keywords.add("\92â\8e~");\r
193                         keywords.add("\8e\96\8cÌ");\r
194                 }\r
195 \r
196                 int count = 0;\r
197                 for (String keyword : keywords) {\r
198                         if (text.indexOf(keyword) >= 0) {\r
199                                 count++;\r
200                         }\r
201                 }\r
202                 return count;\r
203         }\r
204         \r
205         public int getSearchPeriod(){\r
206                 if (searchPeriod <= 0){\r
207                         KeyValueDao dao = KeyValueDao.getInstance();\r
208                         searchPeriod = dao.getInt(KeyValueDao.KEY_SEARCH_PERIOD, 5);\r
209                 }\r
210                 return searchPeriod;\r
211         }\r
212         \r
213         public int getTwitPeriod(){\r
214                 if (twitPeriod <= 0){\r
215                         KeyValueDao dao = KeyValueDao.getInstance();\r
216                         twitPeriod = dao.getInt(KeyValueDao.KEY_TWIT_PERIOD, 5);\r
217                 }\r
218                 return searchPeriod;\r
219         }\r
220         \r
221         private long getTwitTime(long id){\r
222                 String key = KeyValueDao.KEY_TWIT_TIME + id;\r
223                 Long value = (long)twitTimeList.get(key);\r
224                 if (value == null){\r
225                         value = 0L;\r
226                         twitTimeList.put(key, value);\r
227                 }\r
228                 return value;\r
229         }\r
230 }\r