Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Add Sync List regular task
Browse files Browse the repository at this point in the history
  • Loading branch information
Slash Nephy committed Aug 26, 2018
1 parent 061e6f3 commit 949bf8f
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/main/kotlin/jp/nephy/tweetstorm/task/SyncList.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package jp.nephy.tweetstorm.task

import jp.nephy.penicillin.request.allIds
import jp.nephy.penicillin.request.allUsers
import jp.nephy.tweetstorm.TaskManager
import java.util.concurrent.TimeUnit

class SyncList(override val manager: TaskManager): RegularTask(5, TimeUnit.MINUTES) {
override fun run() {
val followingIds = if (manager.account.syncListIncludeSelf) {
manager.twitter.friend.listIds().complete().untilLast().allIds + manager.account.id
} else {
manager.twitter.friend.listIds().complete().untilLast().allIds
}

if (followingIds.size > 3000) {
logger.warn { "This list exceeded 3000 members limit." }
return
}

val listMemberIds = manager.twitter.list.members(listId = manager.account.listId).complete().untilLast().allUsers.map { it.id }

val willBeRemoved = listMemberIds.filter { it !in followingIds }
if (willBeRemoved.isNotEmpty()) {
willBeRemoved.chunked(100).forEach {
manager.twitter.list.removeMembers(listId = manager.account.listId, userIds = it).queue()
}
logger.debug { "Removing ${willBeRemoved.size} user(s)." }
}

val willBeAdded = followingIds.filter { it !in listMemberIds }
if (willBeAdded.isNotEmpty()) {
willBeAdded.chunked(100).forEach {
manager.twitter.list.addMembers(listId = manager.account.listId, userIds = it).queue()
}
logger.debug { "Adding ${willBeAdded.size} user(s)." }
}
}
}

0 comments on commit 949bf8f

Please sign in to comment.