Skip to content

suihan74/hatenaclient-kotlin

Repository files navigation

HatenaClient-kotlin

under development...

The modules for accessing Hatena Bookmark by using Kotlin.

How to use

Android Project

build.gradle(:project)

allprojects {
    repositories {
        // ...
        // add
        maven { url 'https://jitpack.io' }
    }
}

build.gradle(:app)

dependencies {
    // ...

    // HatenaClient
    implementation 'com.github.suihan74:hatenaclient-kotlin:0.0.29'
}

APIs

User

Sign-in

suspend fun signIn() {
    val rk = "..."

    // 認証情報付きのクライアントを取得する
    val certifiedClient = HatenaClient.signIn(rk)
}

rkはアプリ内ブラウザなど別の手段でHatenaにサインインし,クッキーを取得する必要があります。

Account

suspend fun account() {
    val account = certifiedClient.user.getAccount()
}

Notice

Get Notices
suspend fun getNotices() {
    // 通知を取得
    val response = certifiedClient.user.getNotices()
    // 通知の最終確認時刻を更新
    certifiedClient.user.readNotices()
}

Ignored Users

Ignoring / UnIgnoreing
suspend fun ignoreUser() {
    // userIdを非表示設定
    certifiedClient.user.ignore("userId")
    
    // userIdの非表示設定を解除
    certifiedClient.user.unIgnore("userId")
}
Get Ignored Users
suspend fun getIgnoredUsersIncremental() {
    // 適当な件数ずつ取得
    val response = certifiedClient.user.getIgnoredUsers(limit = null, cursor = null)
    val users = response.users
    val cursor = response.cursor
}
suspend fun getIgnoredUsersAll() {
    // 全件取得
    val response = certifiedClient.user.getIgnoredUsersAll()
    val users = response.users
    val cursor = response.cursor  // 途中で失敗した場合null以外の値が入る
}

Entry

Get Entries

suspend fun entries() {
    // 指定カテゴリのホットエントリ一覧を取得
    val hotEntries = HatenaClient.entry.getEntries(EntriesType.HOT, Category.ALL)

    // 指定カテゴリの新着エントリ一覧を取得
    val recentEntries = HatenaClient.entry.getEntries(EntriesType.RECENT, Category.ALL)
}

Get Issues

suspend fun issues() {
    // 指定カテゴリの「特集」を取得
    val issues = HatenaClient.entry.getIssues(Category.IT)

    // 特集を指定してエントリ一覧を取得
    val entriesOfFirstItIssue = HatenaClient.entry.getEntries(EntriesType.HOT, issues[0])
}

My hot entries

suspend fun myHotEntries() {
    val entries = certifiedClient.entry.getMyHotEntries()
}

Entries bookmarked by the specified user

suspend fun userBookmarkedEntries() {
    // 指定ユーザーがブクマしたエントリ
    val suihanEntries = HatenaClient.entry.getBookmarkedEntries(user = "suihan74")
    
    // サインインしているユーザーがブクマしたエントリ
    val userEntries = certifiedClient.entry.getBookmarkedEntries()
    
    // サインインしているユーザーがブクマしたエントリを検索
    val searchResult = certifiedClient.entry.searchBookmarkedEntries(
        SearchType.TAG,
        query = "あとで読む"
    )
}

Bookmark

Get Bookmarks

suspend fun bookmarks() {
    // 新着ブクマを取得
    val recentBookmarksResponse = HatenaClient.bookmark.getRecentBookmarks(
        url = "https://foobarbaz/",
        limit = null,
        cursor = null
    )
    val recentBookmarks = recentBookmarksResponse.bookmarks
    val cursor = recentBookmarksResponse.cursor  // 続きを読み込むときに使用
    
    // 人気ブクマを取得
    val bookmarksDigest = HatenaClient.bookmark.getBookmarksDigest(url = "https://foobarbaz/")
    val popularBookmarks = bookmarksDigest.scoredBookmarks
}

Star

Get Stars

suspend fun stars() {
    val starsEntry = HatenaClient.star.getStarsEntry("url")
    val stars = starsEntry.allStars
    val yellowStars = starsEntry.stars
    val coloredStars = starsEntry.coloredStars
}

suspend fun stars_multi_urls() {
    val starsEntries = HatenaClient.star.getStarsEntries(listOf("url0", "url1", ...))
    starsEntries.forEach { starsEntry ->
        // ...
    }
}

Get My Recent Stars

suspend fun getMyRecentStars() {
    val starEntries = certifiedClient.star.getMyRecentStars()
}

Get Recent Stars Report

suspend fun getRecentStarsReport() {
    val response = certifiedClient.star.getRecentStarsReport()
    val starEntries = response.entries
}