リビジョン 6a3fcb6d
みぞ @mizo0203 さんが7年以上前に追加
| src/com/mizo0203/timeline/talker/TimelineTalker.java | ||
|---|---|---|
| 1 | 1 |
package com.mizo0203.timeline.talker; |
| 2 | 2 |
|
| 3 |
import twitter4j.*; |
|
| 4 |
import twitter4j.conf.Configuration; |
|
| 3 |
/* package */ interface TimelineTalker {
|
|
| 5 | 4 |
|
| 6 |
import java.util.Collections; |
|
| 7 |
import java.util.Locale; |
|
| 8 |
import java.util.Timer; |
|
| 9 |
import java.util.TimerTask; |
|
| 10 |
import java.util.concurrent.TimeUnit; |
|
| 11 |
import java.util.regex.Matcher; |
|
| 12 |
import java.util.regex.Pattern; |
|
| 13 |
|
|
| 14 |
public class TimelineTalker {
|
|
| 15 |
|
|
| 16 |
/** |
|
| 17 |
* ISO 639 言語コード - 日本語 (ja) |
|
| 18 |
*/ |
|
| 19 |
public static final String LANG_JA = Locale.JAPAN.getLanguage(); |
|
| 20 |
|
|
| 21 |
private final RequestHomeTimelineTimerTask mRequestHomeTimelineTimerTask; |
|
| 22 |
|
|
| 23 |
public TimelineTalker(Configuration configuration, Talker talker) {
|
|
| 24 |
Twitter twitter = new TwitterFactory(configuration).getInstance(); |
|
| 25 |
mRequestHomeTimelineTimerTask = new RequestHomeTimelineTimerTask(twitter, talker); |
|
| 26 |
} |
|
| 27 |
|
|
| 28 |
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 |
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 |
|
|
| 91 |
private void onStatus(final Status status) {
|
|
| 92 |
if (!LANG_JA.equalsIgnoreCase(status.getLang())) {
|
|
| 93 |
return; |
|
| 94 |
} |
|
| 95 |
|
|
| 96 |
final StringBuffer buffer = new StringBuffer(); |
|
| 97 |
|
|
| 98 |
if (status.isRetweet()) {
|
|
| 99 |
Status retweetedStatus = status.getRetweetedStatus(); |
|
| 100 |
buffer.append(getUserNameWithoutContext(status.getUser().getName()) + "さんがリツイート。"); |
|
| 101 |
buffer.append(getUserNameWithoutContext(retweetedStatus.getUser().getName()) + "さんから、"); |
|
| 102 |
buffer.append(retweetedStatus.getText()); |
|
| 103 |
} else {
|
|
| 104 |
buffer.append(getUserNameWithoutContext(status.getUser().getName()) + "さんから、"); |
|
| 105 |
buffer.append(status.getText()); |
|
| 106 |
} |
|
| 107 |
|
|
| 108 |
mTalker.talkAsync(UrlUtil.convURLEmpty(buffer).replaceAll("\n", "。"), mYukkuriVoice);
|
|
| 109 |
|
|
| 110 |
// 読み上げは、霊夢と魔理沙が交互に行なう |
|
| 111 |
if (mYukkuriVoice == Talker.YukkuriVoice.REIMU) {
|
|
| 112 |
mYukkuriVoice = Talker.YukkuriVoice.MARISA; |
|
| 113 |
} else {
|
|
| 114 |
mYukkuriVoice = Talker.YukkuriVoice.REIMU; |
|
| 115 |
} |
|
| 116 |
} |
|
| 117 |
} |
|
| 5 |
/* package */ void start(); |
|
| 118 | 6 |
} |
他の形式にエクスポート: Unified diff
Add feature Mastodon's timeline talking. fix #422 @16.0h