ツイート検索にStreaming APIを使ったものを作った #24964
@@ -41,6 +41,8 @@ | ||
41 | 41 | private TweetManager tweetManager = null; |
42 | 42 | //検索ワードに対応したリスナー |
43 | 43 | private Map<String, TweetStreamingListener> listeners = null; |
44 | + //指定したワードの最終更新id | |
45 | + private Map<String, Long> lastUpdate = null; | |
44 | 46 | |
45 | 47 | /** |
46 | 48 | * |
@@ -58,6 +60,7 @@ | ||
58 | 60 | filter = new FilterQuery(); |
59 | 61 | filterWords = new HashSet<String>(); |
60 | 62 | listeners = new HashMap<String, TweetStreamingListener>(); |
63 | + lastUpdate = new HashMap<String, Long>(); | |
61 | 64 | } |
62 | 65 | |
63 | 66 | /** |
@@ -106,12 +109,26 @@ | ||
106 | 109 | if( status.getText().contains( word.toString() ) ) { |
107 | 110 | TweetStreamingListener listener = listeners.get(word); |
108 | 111 | listener.update(status); |
112 | + //最終更新id | |
113 | + lastUpdate.put(word, status.getId()); | |
109 | 114 | } |
110 | - System.out.println("WORD:" + word + " searching -- " + status.getText()); | |
111 | 115 | } |
112 | 116 | } |
113 | 117 | |
114 | 118 | /** |
119 | + * 最終更新ステータスのidの取得 | |
120 | + * @param word | |
121 | + * @return | |
122 | + */ | |
123 | + public long getLastUpdateID(String word) { | |
124 | + Long id = lastUpdate.get(word); | |
125 | + if( id == null ) { | |
126 | + return 0; | |
127 | + } | |
128 | + return id; | |
129 | + } | |
130 | + | |
131 | + /** | |
115 | 132 | * |
116 | 133 | */ |
117 | 134 | @Override |
@@ -84,4 +84,13 @@ | ||
84 | 84 | public void stopSearchListener(String word) { |
85 | 85 | searchStream.removeSearchWord(word); |
86 | 86 | } |
87 | + | |
88 | + /** | |
89 | + * 指定したワードの最終更新status IDを取得 | |
90 | + * @param word | |
91 | + * @return | |
92 | + */ | |
93 | + public long getSearchLastUpdateID(String word) { | |
94 | + return searchStream.getLastUpdateID(word); | |
95 | + } | |
87 | 96 | } |
@@ -58,6 +58,12 @@ | ||
58 | 58 | * @return |
59 | 59 | */ |
60 | 60 | public List<Status> getNewTweetData() { |
61 | + //streaming利用時のsinceidを取得 | |
62 | + long streamingSinceID = tweetManager.getStreamManager().getSearchLastUpdateID(this.searchWord); | |
63 | + if( streamingSinceID > 0 ) { | |
64 | + sinceID = streamingSinceID; | |
65 | + } | |
66 | + | |
61 | 67 | List<Status> status = tweetManager.getNewSearchResult(this.sinceID, this.searchWord); |
62 | 68 | if( status != null ) { |
63 | 69 | //一番最後のtweetのsinceIDを取得する |