Search in sources :

Example 1 with MastodonTwitterImpl

use of com.github.moko256.mastodon.MastodonTwitterImpl in project twicalico by moko256.

the class GlobalApplication method getTwitterInstance.

@NonNull
public Twitter getTwitterInstance(@NonNull AccessToken accessToken) {
    Twitter t;
    Configuration conf;
    if (accessToken.getType() == Type.TWITTER) {
        conf = new ConfigurationBuilder().setTweetModeExtended(true).setOAuthConsumerKey(BuildConfig.CONSUMER_KEY).setOAuthConsumerSecret(BuildConfig.CONSUMER_SECRET).setOAuthAccessToken(accessToken.getToken()).setOAuthAccessTokenSecret(accessToken.getTokenSecret()).build();
        t = twitterCache.get(conf);
        if (t == null) {
            t = new TwitterFactory(conf).getInstance();
            twitterCache.put(conf, t);
        }
    } else {
        conf = new ConfigurationBuilder().setOAuthAccessToken(accessToken.getToken()).setRestBaseURL(accessToken.getUrl()).build();
        t = twitterCache.get(conf);
        if (t == null) {
            t = new MastodonTwitterImpl(conf, accessToken.getUserId(), getOkHttpClient(conf.getHttpClientConfiguration()).newBuilder());
            twitterCache.put(conf, t);
        }
    }
    return t;
}
Also used : ConfigurationBuilder(twitter4j.conf.ConfigurationBuilder) AppConfiguration(com.github.moko256.twicalico.config.AppConfiguration) HttpClientConfiguration(twitter4j.HttpClientConfiguration) Configuration(twitter4j.conf.Configuration) Twitter(twitter4j.Twitter) TwitterFactory(twitter4j.TwitterFactory) MastodonTwitterImpl(com.github.moko256.mastodon.MastodonTwitterImpl) NonNull(android.support.annotation.NonNull)

Example 2 with MastodonTwitterImpl

use of com.github.moko256.mastodon.MastodonTwitterImpl in project twicalico by moko256.

the class PostTweetModelImpl method postTweet.

@Override
public Single<Status> postTweet() {
    return Single.create(subscriber -> {
        try {
            List<Long> ids = null;
            if (uriList.size() > 0) {
                ids = new ArrayList<>(uriList.size());
                for (Uri uri : uriList) {
                    InputStream image = contentResolver.openInputStream(uri);
                    ByteArrayOutputStream bout = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    while (true) {
                        int len = image.read(buffer);
                        if (len < 0) {
                            break;
                        }
                        bout.write(buffer, 0, len);
                    }
                    Attachment attachment = new Media(((MastodonTwitterImpl) GlobalApplication.twitter).client).postMedia(MultipartBody.Part.createFormData("file", uri.getLastPathSegment(), RequestBody.create(MediaType.parse(contentResolver.getType(uri)), bout.toByteArray()))).execute();
                    ids.add(attachment.getId());
                }
            }
            subscriber.onSuccess(new MTStatus(new Statuses(client).postStatus(tweetText, inReplyToStatusId == -1 ? null : inReplyToStatusId, ids, possiblySensitive, null, com.sys1yagi.mastodon4j.api.entity.Status.Visibility.Public).execute()));
        } catch (IOException | Mastodon4jRequestException e) {
            subscriber.onError(e);
        }
    });
}
Also used : MTStatus(com.github.moko256.mastodon.MTStatus) Mastodon4jRequestException(com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException) InputStream(java.io.InputStream) Media(com.sys1yagi.mastodon4j.api.method.Media) Attachment(com.sys1yagi.mastodon4j.api.entity.Attachment) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Uri(android.net.Uri) Statuses(com.sys1yagi.mastodon4j.api.method.Statuses)

Aggregations

Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 MTStatus (com.github.moko256.mastodon.MTStatus)1 MastodonTwitterImpl (com.github.moko256.mastodon.MastodonTwitterImpl)1 AppConfiguration (com.github.moko256.twicalico.config.AppConfiguration)1 Attachment (com.sys1yagi.mastodon4j.api.entity.Attachment)1 Mastodon4jRequestException (com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException)1 Media (com.sys1yagi.mastodon4j.api.method.Media)1 Statuses (com.sys1yagi.mastodon4j.api.method.Statuses)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 HttpClientConfiguration (twitter4j.HttpClientConfiguration)1 Twitter (twitter4j.Twitter)1 TwitterFactory (twitter4j.TwitterFactory)1 Configuration (twitter4j.conf.Configuration)1 ConfigurationBuilder (twitter4j.conf.ConfigurationBuilder)1