• This repository has been archived on 30/Jul/2023
  • Stars
    star
    135
  • Rank 269,297 (Top 6 %)
  • Language
    Kotlin
  • License
    MIT License
  • Created over 7 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

mastodon client for java, kotlin https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md

mastodon4j

wercker status codecov Android Arsenal

mastodon4j is mastodon client for Java and Kotlin.

Official API Doc

https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md

Sample App

Android App

Get Started

Mastodon4j publish in jitpack. Add it in your root build.gradle at the end of repositories:

allprojects {
  repositories {
    ...
    maven { url 'https://jitpack.io' }
  }
}
compile 'com.github.sys1yagi.mastodon4j:mastodon4j:$version'
compile 'com.github.sys1yagi.mastodon4j:mastodon4j-rx:$version'

Check latest version on Jitpack

Usage

Get Public Timeline

kotlin

val client: MastodonClient = MastodonClient.Builder("mstdn.jp", OkHttpClient.Builder(), Gson()).build()
        
val timelines = Timelines(client)
val statuses: List<Status> = timelines.getPublic().execute()

java

MastodonClient client = new MastodonClient.Builder("mstdn.jp", new OkHttpClient.Builder(), new Gson()).build();
Timelines timelines = new Timelines(client);

try {
  List<Status> statuses = timelines.getPublic(new Range()).execute();
  statuses.forEach(status->{
    System.out.println("=============");
    System.out.println(status.getAccount().getDisplayName());
    System.out.println(status.getContent());
  });
} catch (Mastodon4jRequestException e) {
  e.printStackTrace();
}

Register App

If you want to access the auth required API, you need create client credential and get access token. see more docs

kotlin

val client: MastodonClient = MastodonClient.Builder("mstdn.jp", OkHttpClient.Builder(), Gson()).build()
val apps = Apps(client)
val appRegistration = apps.createApp(
	clientName = "client name",
	redirectUris = "urn:ietf:wg:oauth:2.0:oob",
	scope = Scope(Scope.Name.ALL),
	website = "https://sample.com"
).execute()
save(appRegistration) // appRegistration needs to be saved.

AppRegistration has client id and client secret.

java

MastodonClient client = new MastodonClient.Builder("mstdn.jp", new OkHttpClient.Builder(), new Gson()).build();
Apps apps = new Apps(client);
try {
	AppRegistration registration = apps.createApp(
	    "mastodon4j-sample-app",
	    "urn:ietf:wg:oauth:2.0:oob",
	    new Scope(Scope.Name.ALL),
        "https://sample.com"
    ).execute();
    System.out.println("instance=" + registration.getInstanceName());
    System.out.println("client_id=" + registration.getClientId());
    System.out.println("client_secret=" + registration.getClientSecret());
} catch (Mastodon4jRequestException e) {
	int statusCode = e.getResponse().code();
	// error handling.
}

OAuth login and get Access Token

kotlin

val client: MastodonClient = MastodonClient.Builder("mstdn.jp", OkHttpClient.Builder(), Gson()).build()
val clientId = appRegistration.clientId
val apps = Apps(client)

val url = apps.getOAuthUrl(clientId, Scope(Scope.Name.ALL))
// url like bellow
// https://:instance_name/oauth/authorize?client_id=:client_id&redirect_uri=:redirect_uri&response_type=code&scope=read 
// open url and OAuth login and get auth code

val authCode = //...
val clientSecret = appRegistration.clientSecret
val redirectUri = appRegistration.redirectUri
val accessToken = apps.getAccessToken(
			clientId,
			clientSecret,
			redirectUri,
			authCode,
			"authorization_code"
		)
// 	accessToken needs to be saved.

Get Home Timeline

kotlin

// Need parameter of accessToken
val client: MastodonClient = MastodonClient.Builder("mstdn.jp", OkHttpClient.Builder(), Gson())
  .accessToken(accessToken)
  .build()

val statuses: List<Status> = timelines.getHome().execute()

Get raw json

v0.0.7 or later

kotlin

val client = //...
val publicMethod = Public(client)

publicMethod.getLocalPublic()
  .doOnJson { jsonString -> 
    // You can get raw json for each element.
    println(jsonString)
  }
  .execute() 

Streaming API

v1.0.0 or later

kotlin

val client: MastodonClient = MastodonClient.Builder("mstdn.jp", OkHttpClient.Builder(), Gson())
  .accessToken(accessToken)
  .useStreamingApi()
  .build()

val handler = object : Handler {
  override fun onStatus(status: Status) {
    println(status.content)
  }
  override fun onNotification(notification: Notification) {/* no op */}
  override fun onDelete(id: Long) {/* no op */}
}

val streaming = Streaming(client)
try {
  val shutdownable = streaming.localPublic(handler)
  Thread.sleep(10000L)
  shutdownable.shutdown()
} catch(e: Mastodon4jRequestException) {
  e.printStackTrace()
}

java

MastodonClient client = new MastodonClient.Builder("mstdn.jp", new OkHttpClient.Builder(), new Gson())
        .accessToken(accessToken)
        .useStreamingApi()
        .build();
Handler handler = new Handler() {
    @Override
    public void onStatus(@NotNull Status status) {
        System.out.println(status.getContent());
    }

    @Override
    public void onNotification(@NotNull Notification notification) {/* no op */}
    @Override
    public void onDelete(long id) {/* no op */}
};

Streaming streaming = new Streaming(client);
try {
    Shutdownable shutdownable = streaming.localPublic(handler);
    Thread.sleep(10000L);
    shutdownable.shutdown();
} catch (Exception e) {
    e.printStackTrace();
}

Versioning

Semantic Versioning 2.0.0

Implementation Progress

Methods

  • GET /api/v1/accounts/:id
  • GET /api/v1/accounts/verify_credentials
  • PATCH /api/v1/accounts/update_credentials
  • GET /api/v1/accounts/:id/followers
  • GET /api/v1/accounts/:id/following
  • GET /api/v1/accounts/:id/statuses
  • POST /api/v1/accounts/:id/follow
  • POST /api/v1/accounts/:id/unfollow
  • POST /api/v1/accounts/:id/block
  • POST /api/v1/accounts/:id/unblock
  • POST /api/v1/accounts/:id/mute
  • POST /api/v1/accounts/:id/unmute
  • GET /api/v1/accounts/relationships
  • GET /api/v1/accounts/search
  • POST /api/v1/apps
  • GET /api/v1/blocks
  • GET /api/v1/favourites
  • GET /api/v1/follow_requests
  • POST /api/v1/follow_requests/:id/authorize
  • POST /api/v1/follow_requests/:id/reject
  • POST /api/v1/follows
  • GET /api/v1/instance
  • POST /api/v1/media
  • GET /api/v1/mutes
  • GET /api/v1/notifications
  • GET /api/v1/notifications/:id
  • POST /api/v1/notifications/clear
  • GET /api/v1/reports
  • POST /api/v1/reports
  • GET /api/v1/search
  • GET /api/v1/statuses/:id
  • GET /api/v1/statuses/:id/context
  • GET /api/v1/statuses/:id/card
  • GET /api/v1/statuses/:id/reblogged_by
  • GET /api/v1/statuses/:id/favourited_by
  • POST /api/v1/statuses
  • DELETE /api/v1/statuses/:id
  • POST /api/v1/statuses/:id/reblog
  • POST /api/v1/statuses/:id/unreblog
  • POST /api/v1/statuses/:id/favourite
  • POST /api/v1/statuses/:id/unfavourite
  • GET /api/v1/timelines/home
  • GET /api/v1/timelines/public
  • GET /api/v1/timelines/tag/:hashtag

Streaming

v1.0.0 or later

  • GET /api/v1/streaming/user
  • GET /api/v1/streaming/public
  • GET /api/v1/streaming/public/local
  • GET /api/v1/streaming/hashtag
  • GET /api/v1/streaming/hashtag/local

Auth

  • Generate Url for OAuth /oauth/authorize
  • POST password authorize /oauth/token v0.0.2 or later
  • POST /oauth/token

Rx

v0.0.2 or later

  • RxAccounts
  • RxApps
  • RxBlocks
  • RxFavourites
  • RxFollowRequests
  • RxFollows
  • RxInstances
  • RxMedia
  • RxMutes
  • RxNotifications
  • RxReports
  • RxSearch
  • RxStatuses
  • RxTimelines

Rx Streaming

v1.0.0 or later

  • GET /api/v1/streaming/user
  • GET /api/v1/streaming/public
  • GET /api/v1/streaming/public/local
  • GET /api/v1/streaming/hashtag
  • GET /api/v1/streaming/hashtag/local

Contribution

Reporting Issues

Found a problem? Want a new feature? First of all see if your issue or idea has already been reported. If don't, just open a new clear and descriptive issues

License

MIT License

Copyright (c) 2017 Toshihiro Yagi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

More Repositories

1

data-binding-sample

Java
71
star
2

fragment-creator

Fragment Creator is a code generation library to manage fragment class creation and arguments for Android.
Java
38
star
3

indirect-injector

The IndirectInjector is Android library.
Java
31
star
4

DroiDon

UNDER DEVELOPMENT: DroiDon is android app for Mastodon https://github.com/tootsuite/mastodon
HTML
30
star
5

ViewSticker

ViewSticker is Android Library
Java
28
star
6

AndroidPractice

Java
26
star
7

loco

loco (Log Coroutine) is a logging library using coroutine for Android.
Kotlin
23
star
8

implementation-of-async-request-with-rxjava2-or-coroutine

Kotlin
21
star
9

genymotion-peco

Launch genymotion emulator from console.
Shell
19
star
10

android-roughly-java8

Welcome to java8!
Java
19
star
11

kmockito

Mockito for Kotlin.
Kotlin
18
star
12

rxandroid-usecase

Groovy
15
star
13

android-complicated-problems

Java
12
star
14

expand-animator

This is a Android library project for realizing opening-and-closing animation.
Java
12
star
15

bazel-android-samples

Java
11
star
16

jetpack-compose-github-app

This is a sample of jetpack compose using ViewModel.
Kotlin
10
star
17

avd-peco

Launch android emulator from console.
Shell
10
star
18

alertdialog-channel

Kotlin
9
star
19

faucet

Super casual memory leak checker for Android.
Groovy
9
star
20

goat-reader-2-android-prototype

RSSγƒͺーダです。Kotlinで書いています。
Kotlin
7
star
21

DropboxPrintService

kitkat!
Java
6
star
22

leak-checker

casual memory leak checker for Android.
Java
6
star
23

toaster

Android Toaster! You can use toast anywhere.
Java
5
star
24

Goat-Reader

This is an alternative to Google Reader.
JavaScript
5
star
25

nativechain-kotlin

https://github.com/lhartikk/naivechain by kotlin
Kotlin
4
star
26

channel-list

Kotlin
4
star
27

aac-viewmodel-with

Kotlin
4
star
28

Market-Statistics

android market sales statistics tool
JavaScript
3
star
29

rxrecyclerview-load-more

Java
3
star
30

swipe-android

It is implementation of the swipe on android.
Kotlin
3
star
31

flight_sample

Flight framework from twitter. sample app. Todo List Application.
JavaScript
3
star
32

PrintingFrameworkSample

Android 4.4 Kitat!
Java
3
star
33

gof_design_pattern_implemented_in_Typescript

GoF design pattern implemented in Typescript
2
star
34

android-method-counts

Java
2
star
35

android-view-holder-generator-sample

Groovy
2
star
36

longest-streak-android

This is the Android app to check yours public contribution. Let's enjoy the longest streak of Github.
Kotlin
2
star
37

KotlinAndroidSample

Shell
2
star
38

loader_closet

Loader Closet
Java
2
star
39

TransfuseSample

transfuse sample!
Java
2
star
40

SublimeBingTranslator

Bing Translator plugin for Sublime Text 2.
Python
2
star
41

TechBoosterFeeds

Java
1
star
42

scheme-haskell-llvm

Haskell
1
star
43

sting

Sting is DI library for Kotlin.
Kotlin
1
star
44

GDD11jp-DevQuiz--sys1yagi

GDD11jp DevQuiz Result
1
star
45

ActionBarSwapper

Android action bar to move down.
Java
1
star
46

TodoCloudEndpoints

Java
1
star
47

AccountManagerSamples

Android API. AccountManager Sample Code.
1
star
48

gradle-dependencies-manager

for android
Go
1
star
49

GoatTranslater

english to japanese translater. study project.
Java
1
star
50

sugoi-haskell-learning-14

http://sugoihaskell.github.io/
1
star
51

sentry-android-sample

Java
1
star
52

HelloCloudEndpoints

Java
1
star
53

langchain-kt

Kotlin
1
star
54

jack-and-jill-sample

Java
1
star
55

alarm-manager-simplify

Alarm Manager Simplify is a code generation library to simplify the code for AlarmManager on Android.
Java
1
star