| 1 |
6a3fcb6d
|
みぞ@CrazyBeatCoder
|
package com.mizo0203.timeline.talker;
|
| 2 |
|
|
|
| 3 |
|
|
import com.mizo0203.timeline.talker.util.DisplayNameUtil;
|
| 4 |
|
|
import com.mizo0203.timeline.talker.util.UrlUtil;
|
| 5 |
|
|
import org.jetbrains.annotations.NotNull;
|
| 6 |
|
|
import twitter4j.*;
|
| 7 |
|
|
import twitter4j.conf.Configuration;
|
| 8 |
|
|
|
| 9 |
|
|
import java.util.Collections;
|
| 10 |
|
|
import java.util.Locale;
|
| 11 |
|
|
import java.util.Timer;
|
| 12 |
|
|
import java.util.TimerTask;
|
| 13 |
|
|
import java.util.concurrent.TimeUnit;
|
| 14 |
|
|
|
| 15 |
|
|
public class TwitterTimelineTalker implements TimelineTalker {
|
| 16 |
|
|
|
| 17 |
|
|
/** ISO 639 言語コード - 日本語 (ja) */
|
| 18 |
|
|
private static final String LANG_JA = Locale.JAPAN.getLanguage();
|
| 19 |
|
|
|
| 20 |
|
|
@NotNull private final RequestHomeTimelineTimerTask mRequestHomeTimelineTimerTask;
|
| 21 |
|
|
|
| 22 |
|
|
public TwitterTimelineTalker(@NotNull Configuration configuration, Talker talker) {
|
| 23 |
|
|
Twitter twitter = new TwitterFactory(configuration).getInstance();
|
| 24 |
|
|
mRequestHomeTimelineTimerTask = new RequestHomeTimelineTimerTask(twitter, talker);
|
| 25 |
|
|
}
|
| 26 |
|
|
|
| 27 |
|
|
@Override
|
| 28 |
|
|
public void start() {
|
| 29 |
|
|
new Timer().schedule(mRequestHomeTimelineTimerTask, 0L, TimeUnit.MINUTES.toMillis(1));
|
| 30 |
|
|
}
|
| 31 |
|
|
|
| 32 |
|
|
private static class RequestHomeTimelineTimerTask extends TimerTask {
|
| 33 |
|
|
|
| 34 |
|
|
private static final int HOME_TIMELINE_COUNT_MAX = 200;
|
| 35 |
|
|
private static final int HOME_TIMELINE_COUNT_MIN = 1;
|
| 36 |
|
|
|
| 37 |
|
|
private final Twitter mTwitter;
|
| 38 |
|
|
private final Talker mTalker;
|
| 39 |
|
|
|
| 40 |
|
|
/** mStatusSinceId より大きい(つまり、より新しい) ID を持つ HomeTimeline をリクエストする */
|
| 41 |
|
|
private long mStatusSinceId = 1L;
|
| 42 |
|
|
|
| 43 |
|
|
private boolean mIsUpdatedStatusSinceId = false;
|
| 44 |
|
|
|
| 45 |
|
|
private RequestHomeTimelineTimerTask(Twitter twitter, Talker talker) {
|
| 46 |
|
|
mTwitter = twitter;
|
| 47 |
|
|
mTalker = talker;
|
| 48 |
|
|
}
|
| 49 |
|
|
|
| 50 |
|
|
/** The action to be performed by this timer task. */
|
| 51 |
|
|
@Override
|
| 52 |
|
|
public void run() {
|
| 53 |
|
|
try {
|
| 54 |
|
|
// mStatusSinceId が未更新ならば、 Status を 1 つだけ取得する
|
| 55 |
|
|
int count = mIsUpdatedStatusSinceId ? HOME_TIMELINE_COUNT_MAX : HOME_TIMELINE_COUNT_MIN;
|
| 56 |
|
|
Paging paging = new Paging(1, count, mStatusSinceId);
|
| 57 |
|
|
ResponseList<Status> statusResponseList = mTwitter.getHomeTimeline(paging);
|
| 58 |
|
|
|
| 59 |
|
|
if (statusResponseList.isEmpty()) {
|
| 60 |
|
|
return;
|
| 61 |
|
|
}
|
| 62 |
|
|
|
| 63 |
|
|
// mStatusSinceId を、取得した最新の ID に更新する
|
| 64 |
|
|
mStatusSinceId = statusResponseList.get(0).getId();
|
| 65 |
|
|
mIsUpdatedStatusSinceId = true;
|
| 66 |
|
|
|
| 67 |
|
|
// Status が古い順になるよう、 statusResponseList を逆順に並び替える
|
| 68 |
|
|
Collections.reverse(statusResponseList);
|
| 69 |
|
|
|
| 70 |
|
|
for (Status status : statusResponseList) {
|
| 71 |
|
|
onStatus(status);
|
| 72 |
|
|
}
|
| 73 |
|
|
|
| 74 |
|
|
} catch (TwitterException e) {
|
| 75 |
|
|
e.printStackTrace();
|
| 76 |
|
|
}
|
| 77 |
|
|
}
|
| 78 |
|
|
|
| 79 |
|
|
private void onStatus(final Status status) {
|
| 80 |
|
|
if (!LANG_JA.equalsIgnoreCase(status.getLang())) {
|
| 81 |
|
|
return;
|
| 82 |
|
|
}
|
| 83 |
|
|
|
| 84 |
|
|
final StringBuffer buffer = new StringBuffer();
|
| 85 |
|
|
|
| 86 |
|
|
if (status.isRetweet()) {
|
| 87 |
|
|
Status retweetedStatus = status.getRetweetedStatus();
|
| 88 |
|
|
buffer
|
| 89 |
|
|
.append(DisplayNameUtil.removeContext(status.getUser().getName()))
|
| 90 |
|
|
.append("さんがリツイート。");
|
| 91 |
|
|
buffer
|
| 92 |
|
|
.append(DisplayNameUtil.removeContext(retweetedStatus.getUser().getName()))
|
| 93 |
|
|
.append("さんから、");
|
| 94 |
|
|
buffer.append(retweetedStatus.getText());
|
| 95 |
|
|
} else {
|
| 96 |
|
|
buffer.append(DisplayNameUtil.removeContext(status.getUser().getName())).append("さんから、");
|
| 97 |
|
|
buffer.append(status.getText());
|
| 98 |
|
|
}
|
| 99 |
|
|
|
| 100 |
|
|
mTalker.talkAlternatelyAsync(UrlUtil.convURLEmpty(buffer).replaceAll("\n", "。"));
|
| 101 |
|
|
}
|
| 102 |
|
|
}
|
| 103 |
|
|
}
|