Search in sources :

Example 1 with Mastodon4jRequestException

use of com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException in project twicalico by moko256.

the class MastodonTwitterImpl method search.

@Override
public QueryResult search(Query query) {
    Pageable<com.sys1yagi.mastodon4j.api.entity.Status> pageable = null;
    try {
        pageable = new Public(client).getFederatedTag(query.getQuery(), MTRangeConverter.convert(query)).execute();
    } catch (Mastodon4jRequestException e) {
        e.printStackTrace();
    }
    long previous;
    long next;
    if (pageable == null)
        return null;
    if (pageable.getLink() != null) {
        next = pageable.getLink().getMaxId();
        previous = pageable.getLink().getSinceId();
    } else {
        next = -1;
        previous = -1;
    }
    List<com.sys1yagi.mastodon4j.api.entity.Status> part = pageable.getPart();
    Pageable<com.sys1yagi.mastodon4j.api.entity.Status> finalPageable = pageable;
    return new QueryResult() {

        @Override
        public long getSinceId() {
            return previous;
        }

        @Override
        public long getMaxId() {
            return next;
        }

        @Override
        public String getRefreshURL() {
            return null;
        }

        @Override
        public int getCount() {
            return part.size();
        }

        @Override
        public double getCompletedIn() {
            return 0;
        }

        @Override
        public String getQuery() {
            return query.getQuery();
        }

        @Override
        public List<Status> getTweets() {
            return MTResponseList.convert(finalPageable);
        }

        @Override
        public Query nextQuery() {
            return new Query(query.getQuery()).sinceId(next);
        }

        @Override
        public boolean hasNext() {
            return next != -1;
        }

        @Override
        public RateLimitStatus getRateLimitStatus() {
            return null;
        }

        @Override
        public int getAccessLevel() {
            return 0;
        }
    };
}
Also used : Status(twitter4j.Status) RateLimitStatus(twitter4j.RateLimitStatus) QueryResult(twitter4j.QueryResult) Mastodon4jRequestException(com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException) GeoQuery(twitter4j.GeoQuery) Query(twitter4j.Query) Public(com.sys1yagi.mastodon4j.api.method.Public)

Example 2 with Mastodon4jRequestException

use of com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException 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

Mastodon4jRequestException (com.sys1yagi.mastodon4j.api.exception.Mastodon4jRequestException)2 Uri (android.net.Uri)1 MTStatus (com.github.moko256.mastodon.MTStatus)1 Attachment (com.sys1yagi.mastodon4j.api.entity.Attachment)1 Media (com.sys1yagi.mastodon4j.api.method.Media)1 Public (com.sys1yagi.mastodon4j.api.method.Public)1 Statuses (com.sys1yagi.mastodon4j.api.method.Statuses)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 GeoQuery (twitter4j.GeoQuery)1 Query (twitter4j.Query)1 QueryResult (twitter4j.QueryResult)1 RateLimitStatus (twitter4j.RateLimitStatus)1 Status (twitter4j.Status)1