1 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
package com.mizo0203.twitter.timeline.talker;
|
2 |
|
|
|
3 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
import twitter4j.*;
|
4 |
|
|
import twitter4j.conf.Configuration;
|
5 |
|
|
|
6 |
|
|
import java.util.Collections;
|
7 |
79a6e367
|
みぞ@CrazyBeatCoder
|
import java.util.Locale;
|
8 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
import java.util.Timer;
|
9 |
|
|
import java.util.TimerTask;
|
10 |
|
|
import java.util.concurrent.TimeUnit;
|
11 |
eb7a557c
|
みぞ@CrazyBeatCoder
|
import java.util.regex.Matcher;
|
12 |
|
|
import java.util.regex.Pattern;
|
13 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
|
14 |
|
|
public class TwitterTimelineTalker {
|
15 |
|
|
|
16 |
79a6e367
|
みぞ@CrazyBeatCoder
|
/**
|
17 |
|
|
* ISO 639 言語コード - 日本語 (ja)
|
18 |
|
|
*/
|
19 |
|
|
public static final String LANG_JA = Locale.JAPAN.getLanguage();
|
20 |
|
|
|
21 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
private final RequestHomeTimelineTimerTask mRequestHomeTimelineTimerTask;
|
22 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
|
23 |
|
|
public TwitterTimelineTalker(Configuration configuration, Talker talker) {
|
24 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
Twitter twitter = new TwitterFactory(configuration).getInstance();
|
25 |
|
|
mRequestHomeTimelineTimerTask = new RequestHomeTimelineTimerTask(twitter, talker);
|
26 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
}
|
27 |
|
|
|
28 |
eb7a557c
|
みぞ@CrazyBeatCoder
|
private static String getUserNameWithoutContext(String name) {
|
29 |
|
|
Pattern p = Pattern.compile("([^@@]+).+");
|
30 |
|
|
Matcher m = p.matcher(name);
|
31 |
|
|
return m.replaceFirst("$1");
|
32 |
|
|
}
|
33 |
|
|
|
34 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
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 |
|
|
}
|
90 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
|
91 |
bfbed4b5
|
みぞ@CrazyBeatCoder
|
private void onStatus(final Status status) {
|
92 |
79a6e367
|
みぞ@CrazyBeatCoder
|
if (!LANG_JA.equalsIgnoreCase(status.getLang())) {
|
93 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
return;
|
94 |
|
|
}
|
95 |
79a6e367
|
みぞ@CrazyBeatCoder
|
|
96 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
final StringBuffer buffer = new StringBuffer();
|
97 |
3c5e61d4
|
みぞ@CrazyBeatCoder
|
|
98 |
|
|
if (status.isRetweet()) {
|
99 |
|
|
Status retweetedStatus = status.getRetweetedStatus();
|
100 |
eb7a557c
|
みぞ@CrazyBeatCoder
|
buffer.append(getUserNameWithoutContext(status.getUser().getName()) + "さんがリツイート。");
|
101 |
|
|
buffer.append(getUserNameWithoutContext(retweetedStatus.getUser().getName()) + "さんから、");
|
102 |
3c5e61d4
|
みぞ@CrazyBeatCoder
|
buffer.append(retweetedStatus.getText());
|
103 |
|
|
} else {
|
104 |
eb7a557c
|
みぞ@CrazyBeatCoder
|
buffer.append(getUserNameWithoutContext(status.getUser().getName()) + "さんから、");
|
105 |
3c5e61d4
|
みぞ@CrazyBeatCoder
|
buffer.append(status.getText());
|
106 |
|
|
}
|
107 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
|
108 |
79a6e367
|
みぞ@CrazyBeatCoder
|
mTalker.talkAsync(UrlUtil.convURLEmpty(buffer).replaceAll("\n", "。"), mYukkuriVoice);
|
109 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
|
110 |
79a6e367
|
みぞ@CrazyBeatCoder
|
// 読み上げは、霊夢と魔理沙が交互に行なう
|
111 |
|
|
if (mYukkuriVoice == Talker.YukkuriVoice.REIMU) {
|
112 |
|
|
mYukkuriVoice = Talker.YukkuriVoice.MARISA;
|
113 |
|
|
} else {
|
114 |
|
|
mYukkuriVoice = Talker.YukkuriVoice.REIMU;
|
115 |
|
|
}
|
116 |
df4ee0a0
|
みぞ@CrazyBeatCoder
|
}
|
117 |
|
|
}
|
118 |
|
|
}
|