fixed stream throws exception

This commit is contained in:
Mariotaku Lee 2017-04-07 10:31:07 +08:00
parent 4db3a4e851
commit 21a325ebda
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 35 additions and 17 deletions

View File

@ -23,13 +23,13 @@ import org.mariotaku.microblog.library.twitter.annotation.StreamWith;
import org.mariotaku.microblog.library.twitter.callback.UserStreamCallback; import org.mariotaku.microblog.library.twitter.callback.UserStreamCallback;
import org.mariotaku.microblog.library.twitter.template.StatusAnnotationTemplate; import org.mariotaku.microblog.library.twitter.template.StatusAnnotationTemplate;
import org.mariotaku.restfu.annotation.method.GET; import org.mariotaku.restfu.annotation.method.GET;
import org.mariotaku.restfu.annotation.param.Queries; import org.mariotaku.restfu.annotation.param.Params;
/** /**
* Twitter UserStream API * Twitter UserStream API
* Created by mariotaku on 15/5/26. * Created by mariotaku on 15/5/26.
*/ */
@Queries(template = StatusAnnotationTemplate.class) @Params(template = StatusAnnotationTemplate.class)
public interface TwitterUserStream { public interface TwitterUserStream {
@GET("/user.json") @GET("/user.json")

View File

@ -57,6 +57,7 @@ class UserStreamDumperPlugin(val context: Context) : DumperPlugin {
val options = Options() val options = Options()
options.addOption("t", "timeline", false, "Include timeline") options.addOption("t", "timeline", false, "Include timeline")
options.addOption("i", "interactions", false, "Include interactions") options.addOption("i", "interactions", false, "Include interactions")
options.addOption("v", "verbose", false, "Print full object")
val argsList = dumpContext.argsAsList val argsList = dumpContext.argsAsList
val formatter = HelpFormatter() val formatter = HelpFormatter()
if (argsList.isEmpty()) { if (argsList.isEmpty()) {
@ -67,15 +68,18 @@ class UserStreamDumperPlugin(val context: Context) : DumperPlugin {
val manager = DependencyHolder.get(context).userColorNameManager val manager = DependencyHolder.get(context).userColorNameManager
val includeTimeline = cmdLine.hasOption("timeline") val includeTimeline = cmdLine.hasOption("timeline")
val includeInteractions = cmdLine.hasOption("interactions") val includeInteractions = cmdLine.hasOption("interactions")
val verboseMode = cmdLine.hasOption("verbose")
val accountKey = UserKey.valueOf(argsList[0]) val accountKey = UserKey.valueOf(argsList[0])
val am = AccountManager.get(context) val am = AccountManager.get(context)
val account = AccountUtils.getAccountDetails(am, accountKey, true) ?: return val account = AccountUtils.getAccountDetails(am, accountKey, true) ?: return
when (account.type) { when (account.type) {
AccountType.TWITTER -> { AccountType.TWITTER -> {
beginTwitterStream(account, dumpContext, includeInteractions, includeTimeline, manager) beginTwitterStream(account, dumpContext, includeInteractions, includeTimeline,
verboseMode, manager)
} }
AccountType.FANFOU -> { AccountType.FANFOU -> {
beginFanfouStream(account, dumpContext, includeInteractions, includeTimeline, manager) beginFanfouStream(account, dumpContext, includeInteractions, includeTimeline,
verboseMode, manager)
} }
else -> { else -> {
dumpContext.stderr.println("Unsupported account type ${account.type}") dumpContext.stderr.println("Unsupported account type ${account.type}")
@ -85,8 +89,9 @@ class UserStreamDumperPlugin(val context: Context) : DumperPlugin {
} }
private fun beginTwitterStream(account: AccountDetails, dumpContext: DumperContext, includeInteractions: Boolean, private fun beginTwitterStream(account: AccountDetails, dumpContext: DumperContext,
includeTimeline: Boolean, manager: UserColorNameManager) { includeInteractions: Boolean, includeTimeline: Boolean, verboseMode: Boolean,
manager: UserColorNameManager) {
val userStream = account.newMicroBlogInstance(context, cls = TwitterUserStream::class.java) val userStream = account.newMicroBlogInstance(context, cls = TwitterUserStream::class.java)
dumpContext.stdout.println("Beginning user stream...") dumpContext.stdout.println("Beginning user stream...")
dumpContext.stdout.flush() dumpContext.stdout.flush()
@ -99,28 +104,40 @@ class UserStreamDumperPlugin(val context: Context) : DumperPlugin {
override fun onHomeTimeline(status: Status): Boolean { override fun onHomeTimeline(status: Status): Boolean {
if (!includeTimeline && includeInteractions) return true if (!includeTimeline && includeInteractions) return true
if (verboseMode) {
dumpContext.stdout.println("Home: @${status.user.screenName}: ${status.toString().trim('\n')}")
} else {
dumpContext.stdout.println("Home: @${status.user.screenName}: ${status.text.trim('\n')}") dumpContext.stdout.println("Home: @${status.user.screenName}: ${status.text.trim('\n')}")
}
dumpContext.stdout.flush() dumpContext.stdout.flush()
return true return true
} }
override fun onActivityAboutMe(activity: Activity): Boolean { override fun onActivityAboutMe(activity: Activity): Boolean {
if (!includeInteractions && includeTimeline) return true if (!includeInteractions && includeTimeline) return true
if (verboseMode) {
dumpContext.stdout.println("Activity: @${activity.toString().trim('\n')}")
} else {
val pActivity = ParcelableActivityUtils.fromActivity(activity, account.key, val pActivity = ParcelableActivityUtils.fromActivity(activity, account.key,
account.type) account.type)
val message = ActivityTitleSummaryMessage.get(context, manager, pActivity, pActivity.sources, 0, val message = ActivityTitleSummaryMessage.get(context, manager, pActivity,
true, true) pActivity.sources, 0, true, true)
if (message != null) { if (message != null) {
dumpContext.stdout.println("Activity: ${message.title}: ${message.summary}") dumpContext.stdout.println("Activity: ${message.title}: ${message.summary}")
} else { } else {
dumpContext.stdout.println("Activity unsupported: ${activity.action}") dumpContext.stdout.println("Activity unsupported: ${activity.action}")
} }
}
dumpContext.stdout.flush() dumpContext.stdout.flush()
return true return true
} }
override fun onDirectMessage(directMessage: DirectMessage): Boolean { override fun onDirectMessage(directMessage: DirectMessage): Boolean {
if (verboseMode) {
dumpContext.stdout.println("Message: @${directMessage.senderScreenName}: ${directMessage.toString().trim('\n')}")
} else {
dumpContext.stdout.println("Message: @${directMessage.senderScreenName}: ${directMessage.text.trim('\n')}") dumpContext.stdout.println("Message: @${directMessage.senderScreenName}: ${directMessage.text.trim('\n')}")
}
dumpContext.stdout.flush() dumpContext.stdout.flush()
return true return true
} }
@ -132,8 +149,9 @@ class UserStreamDumperPlugin(val context: Context) : DumperPlugin {
} }
} }
private fun beginFanfouStream(account: AccountDetails, dumpContext: DumperContext, includeInteractions: Boolean, private fun beginFanfouStream(account: AccountDetails, dumpContext: DumperContext,
includeTimeline: Boolean, manager: UserColorNameManager) { includeInteractions: Boolean, includeTimeline: Boolean, verboseMode: Boolean,
manager: UserColorNameManager) {
val userStream = account.newMicroBlogInstance(context, cls = FanfouStream::class.java) val userStream = account.newMicroBlogInstance(context, cls = FanfouStream::class.java)
dumpContext.stdout.println("Beginning user stream...") dumpContext.stdout.println("Beginning user stream...")
dumpContext.stdout.flush() dumpContext.stdout.flush()