Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
Nep edited this page Aug 1, 2021 · 8 revisions

Usage

⚠ This section has not been updated in a long time. It may contain outdated APIs.

How to create PenicillinClient

val client = PenicillinClient {
    // Configures about your credentials.
    account {
        application("ConsumerKey", "ConsumerSecret")
        // Official clients are pre-defined.
        // application(OfficialClient.OAuth1a.TwitterForiPhone)
    
        // For OAuth 1.0a
        token("AccessToken", "AccessTokenSecret")
        // For OAuth 2.0
        // token("BearerToken")
    }
    
    // Configures about API connection.
    api {
        // Pretends "Twitter for iPhone". All the requests are based on "Twitter for iPhone".
        // It is required when you'd like to access some Private APIs (such as Polling Tweets).
        emulationMode = EmulationMode.TwitterForiPhone

        // When the client encounters a Twitter API error, retries up to 5 times.
        maxRetries = 5

        // When the client encounters a Twitter API error, retries every 1 sec.
        retryInterval(1, TimeUnit.SECONDS)
    }
}

API executions

  • Retrieves favorites from @Twitter up to 10 tweets. (Blocking; with current thread)
val favorites = client.favorites.listByScreenName("Twitter", count = 10).complete()

favorites.forEach { tweet ->
    println("${tweet.user.name} @${tweet.user.screenName}\n${tweet.text}")

    // xxx @yyy
    // Tweet text
}
  • Retrieves the home timeline. (Asynchronous; callback style)
client.timeline.home.queue { timeline ->
    timeline.forEach { tweet ->
        println("${tweet.user.name} @${tweet.user.screenName}\n${tweet.text}")

        // xxx @yyy
        // Tweet text
    }
}
  • Retrieves all the your followers. (Blocking; cursor operation)
client.followers.listUsers.untilLast().allUsers.forEach { user ->
    println("${user.name} @${user.screenName}")

    // xxx @yyy
}
  • Posts a tweet with a image. (Suspend function)
client.statuses.createWithMedia(
    status = "this is a media tweet.",
    media = listOf(
        MediaComponent(
            Paths.get("/path/to/image.png"),
            type = MediaType.PNG,
            category = MediaCategory.TweetImage
        )
    )
).await()
  • Posts a tweet with a video.
client.statuses.createWithMedia(
    status = "this is a video tweet.",
    media = listOf(
        MediaComponent(
            Paths.get("/path/to/file.mp4"),
            type = MediaType.MP4,
            category = MediaCategory.TweetVideo
        )
    )
).complete()
  • Posts a polling tweet. (Requires emulationMode = EmulationMode.TwitterForiPhone!)
client.statuses.createPollTweet(
    status = "Which SNSs do you like?",
    choices = listOf("Twitter", "Facebook", "Mastodon", "Weibo"),
    minutes = 60 * 24 * 5 // 5 days
).complete()
  • Connects to the Tweetstorm.
client.stream.tweetstorm("tweetstorm.example.com").listen(object: TweetstormListener {
    override suspend fun onConnect() {
        println("Connected to the Tweetstorm.")
    }

    override suspend fun onDisconnect(cause: Throwable) {
        println("Disconnected from the Tweetstorm")
    }

    override suspend fun onStatus(status: Status) {
        println(status.text)
    }
}).await()
  • Generates Access Token & Access Token Secret.
val rt = client.oauth.requestToken()

// Prints authenticate url.
// Open this link in browser and input pin code to console.
println(client.oauth.authenticateUrl(rt.requestToken))

val pin = readLine()!!

// Prints credentials.
println(client.oauth.accessToken(ck, cs, rt.requestToken, rt.requestTokenSecret, pin))
Clone this wiki locally