リビジョン bfbed4b5
みぞ @mizo0203 さんがほぼ6年前に追加
src/com/mizo0203/twitter/timeline/talker/TwitterTimelineTalker.java | ||
---|---|---|
1 | 1 |
package com.mizo0203.twitter.timeline.talker; |
2 | 2 |
|
3 |
import twitter4j.*; |
|
4 |
import twitter4j.conf.Configuration; |
|
5 |
|
|
6 |
import java.util.Collections; |
|
3 | 7 |
import java.util.Locale; |
8 |
import java.util.Timer; |
|
9 |
import java.util.TimerTask; |
|
10 |
import java.util.concurrent.TimeUnit; |
|
4 | 11 |
import java.util.regex.Matcher; |
5 | 12 |
import java.util.regex.Pattern; |
6 |
import twitter4j.StallWarning; |
|
7 |
import twitter4j.Status; |
|
8 |
import twitter4j.StatusDeletionNotice; |
|
9 |
import twitter4j.StatusListener; |
|
10 |
import twitter4j.TwitterStream; |
|
11 |
import twitter4j.TwitterStreamFactory; |
|
12 |
import twitter4j.conf.Configuration; |
|
13 | 13 |
|
14 | 14 |
public class TwitterTimelineTalker { |
15 | 15 |
|
... | ... | |
18 | 18 |
*/ |
19 | 19 |
public static final String LANG_JA = Locale.JAPAN.getLanguage(); |
20 | 20 |
|
21 |
private Talker.YukkuriVoice mYukkuriVoice = Talker.YukkuriVoice.REIMU; |
|
22 |
private final TwitterStream mTwitterStream; |
|
23 |
private final Talker mTalker; |
|
21 |
private final RequestHomeTimelineTimerTask mRequestHomeTimelineTimerTask; |
|
24 | 22 |
|
25 | 23 |
public TwitterTimelineTalker(Configuration configuration, Talker talker) { |
26 |
mTwitterStream = new TwitterStreamFactory(configuration).getInstance(); |
|
27 |
mTwitterStream.addListener(new OnStatusEvent()); |
|
28 |
mTalker = talker; |
|
29 |
} |
|
30 |
|
|
31 |
public void start() { |
|
32 |
// OnStatusEvent に Twitter タイムラインが通知される |
|
33 |
mTwitterStream.user(); |
|
24 |
Twitter twitter = new TwitterFactory(configuration).getInstance(); |
|
25 |
mRequestHomeTimelineTimerTask = new RequestHomeTimelineTimerTask(twitter, talker); |
|
34 | 26 |
} |
35 | 27 |
|
36 | 28 |
private static String getUserNameWithoutContext(String name) { |
... | ... | |
39 | 31 |
return m.replaceFirst("$1"); |
40 | 32 |
} |
41 | 33 |
|
42 |
private class OnStatusEvent implements StatusListener { |
|
34 |
public void start() { |
|
35 |
new Timer().schedule(mRequestHomeTimelineTimerTask, 0L, TimeUnit.MINUTES.toMillis(1)); |
|
36 |
} |
|
37 |
|
|
38 |
private static class RequestHomeTimelineTimerTask extends TimerTask { |
|
39 |
|
|
40 |
private static final int HOME_TIMELINE_COUNT_MAX = 200; |
|
41 |
private static final int HOME_TIMELINE_COUNT_MIN = 1; |
|
42 |
|
|
43 |
private final Twitter mTwitter; |
|
44 |
private final Talker mTalker; |
|
45 |
|
|
46 |
private Talker.YukkuriVoice mYukkuriVoice = Talker.YukkuriVoice.REIMU; |
|
47 |
|
|
48 |
/** |
|
49 |
* mStatusSinceId より大きい(つまり、より新しい) ID を持つ HomeTimeline をリクエストする |
|
50 |
*/ |
|
51 |
private long mStatusSinceId = 1L; |
|
52 |
|
|
53 |
private boolean mIsUpdatedStatusSinceId = false; |
|
54 |
|
|
55 |
private RequestHomeTimelineTimerTask(Twitter twitter, Talker talker) { |
|
56 |
mTwitter = twitter; |
|
57 |
mTalker = talker; |
|
58 |
} |
|
59 |
|
|
60 |
/** |
|
61 |
* The action to be performed by this timer task. |
|
62 |
*/ |
|
63 |
@Override |
|
64 |
public void run() { |
|
65 |
try { |
|
66 |
// mStatusSinceId が未更新ならば、 Status を 1 つだけ取得する |
|
67 |
int count = mIsUpdatedStatusSinceId ? HOME_TIMELINE_COUNT_MAX : HOME_TIMELINE_COUNT_MIN; |
|
68 |
Paging paging = new Paging(1, count, mStatusSinceId); |
|
69 |
ResponseList<Status> statusResponseList = mTwitter.getHomeTimeline(paging); |
|
70 |
|
|
71 |
if (statusResponseList.isEmpty()) { |
|
72 |
return; |
|
73 |
} |
|
74 |
|
|
75 |
// mStatusSinceId を、取得した最新の ID に更新する |
|
76 |
mStatusSinceId = statusResponseList.get(0).getId(); |
|
77 |
mIsUpdatedStatusSinceId = true; |
|
78 |
|
|
79 |
// Status が古い順になるよう、 statusResponseList を逆順に並び替える |
|
80 |
Collections.reverse(statusResponseList); |
|
81 |
|
|
82 |
for (Status status : statusResponseList) { |
|
83 |
onStatus(status); |
|
84 |
} |
|
85 |
|
|
86 |
} catch (TwitterException e) { |
|
87 |
e.printStackTrace(); |
|
88 |
} |
|
89 |
} |
|
43 | 90 |
|
44 |
public void onStatus(final Status status) {
|
|
91 |
private void onStatus(final Status status) {
|
|
45 | 92 |
if (!LANG_JA.equalsIgnoreCase(status.getLang())) { |
46 | 93 |
return; |
47 | 94 |
} |
... | ... | |
66 | 113 |
} else { |
67 | 114 |
mYukkuriVoice = Talker.YukkuriVoice.REIMU; |
68 | 115 |
} |
69 |
|
|
70 |
} |
|
71 |
|
|
72 |
public void onDeletionNotice(StatusDeletionNotice sdn) { |
|
73 |
System.err.println("onDeletionNotice."); |
|
74 |
} |
|
75 |
|
|
76 |
public void onTrackLimitationNotice(int i) { |
|
77 |
System.err.println("onTrackLimitationNotice.(" + i + ")"); |
|
78 |
} |
|
79 |
|
|
80 |
public void onScrubGeo(long lat, long lng) { |
|
81 |
System.err.println("onScrubGeo.(" + lat + ", " + lng + ")"); |
|
82 |
} |
|
83 |
|
|
84 |
public void onException(Exception excptn) { |
|
85 |
System.err.println("onException."); |
|
86 | 116 |
} |
87 |
|
|
88 |
@Override |
|
89 |
public void onStallWarning(StallWarning arg0) {} |
|
90 | 117 |
} |
91 |
|
|
92 | 118 |
} |
他の形式にエクスポート: Unified diff
Change API to request Twitter timeline from User Streams API to statuses/home_timeline API. fix #334 @2.25h