Example usage for twitter4j.conf ConfigurationBuilder setMediaProvider

List of usage examples for twitter4j.conf ConfigurationBuilder setMediaProvider

Introduction

In this page you can find the example usage for twitter4j.conf ConfigurationBuilder setMediaProvider.

Prototype

public ConfigurationBuilder setMediaProvider(String mediaProvider) 

Source Link

Usage

From source file:com.javielinux.api.loaders.ImageUploadLoader.java

License:Apache License

@Override
public BaseResponse loadInBackground() {

    try {/*from ww w. jav  a 2  s.  c  o  m*/

        ImageUploadResponse response = new ImageUploadResponse();

        String f = Utils.appUploadImageDirectory + request.getFilename();

        response.setFile(request.getFilename());
        /*
           int size = Integer.parseInt(Utils.getPreference(mContext).getString("prf_size_photo", "2"));
                
           if (size == 1) {
           Bitmap resizedBitmap = Utils.getResizeBitmapFromFile(f, Utils.HEIGHT_PHOTO_SIZE_SMALL);
           if (resizedBitmap!=null) {
               FileOutputStream out = new FileOutputStream(f);
               resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
           }
           } else if (size == 2) {
           Bitmap resizedBitmap = Utils.getResizeBitmapFromFile(f, Utils.HEIGHT_PHOTO_SIZE_MIDDLE);
           if (resizedBitmap!=null) {
               FileOutputStream out = new FileOutputStream(f);
               resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
           }
           }
           */
        response.setBmp(ImageUtils.getBitmapFromFile(f, Utils.HEIGHT_THUMB_NEWSTATUS, true));

        File file = new File(f);

        int type = Integer.parseInt(Utils.getPreference(getContext()).getString("prf_service_image", "1"));

        MediaProvider mp = MediaProvider.YFROG;

        if (type == 1) {
            mp = MediaProvider.YFROG;
        } else if (type == 2) {
            mp = MediaProvider.TWITPIC;
        } else if (type == 3) {
            mp = MediaProvider.PLIXI;
        }

        loadConsumerKeys();

        ConfigurationBuilder confBuild = new ConfigurationBuilder();
        confBuild.setOAuthConsumerKey(consumerKey);
        confBuild.setOAuthConsumerSecret(consumerSecretKey);
        confBuild.setOAuthAccessToken(ConnectionManager.getInstance().getTwitter(request.getUserId())
                .getOAuthAccessToken().getToken());
        confBuild.setOAuthAccessTokenSecret(ConnectionManager.getInstance().getTwitter(request.getUserId())
                .getOAuthAccessToken().getTokenSecret());
        if (type == 1) {
        } else if (type == 2) {
            confBuild.setMediaProviderAPIKey("e3533af853e8f63a0018a2c63d7ee69f");
        } else if (type == 3) {
            confBuild.setMediaProviderAPIKey("57438faa-51e4-43a5-9e15-0c63b6f73950");
        }

        confBuild.setMediaProvider(mp.name());
        Configuration config = confBuild.build();

        ImageUpload upload = new ImageUploadFactory(config).getInstance(mp);
        if (upload != null) {
            response.setUrl(upload.upload(file));
            return response;
        } else {
            ErrorResponse errorResponse = new ErrorResponse();
            errorResponse.setError("Image upload is null");
            return errorResponse;
        }

    } catch (TwitterException exception) {
        exception.printStackTrace();
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setError(exception, exception.getMessage());
        return errorResponse;
    } catch (Exception exception) {
        exception.printStackTrace();
        ErrorResponse errorResponse = new ErrorResponse();
        errorResponse.setError(exception, exception.getMessage());
        return errorResponse;
    }

}

From source file:com.javielinux.task.ImageUploadAsyncTask.java

License:Apache License

@Override
protected ImageUploadAsyncTask.ImageUploadResult doInBackground(String... args) {
    ImageUploadResult iur = new ImageUploadResult();
    try {/*from   www.ja  va  2 s.  c  om*/

        String f = Utils.appUploadImageDirectory + args[0];

        iur.file = args[0];
        /*
        int size = Integer.parseInt(Utils.getPreference(mContext).getString("prf_size_photo", "2"));
                
        if (size == 1) {
           Bitmap resizedBitmap = Utils.getResizeBitmapFromFile(f, Utils.HEIGHT_PHOTO_SIZE_SMALL);
           if (resizedBitmap!=null) {
        FileOutputStream out = new FileOutputStream(f);
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
           }
        } else if (size == 2) {
           Bitmap resizedBitmap = Utils.getResizeBitmapFromFile(f, Utils.HEIGHT_PHOTO_SIZE_MIDDLE);
           if (resizedBitmap!=null) {
        FileOutputStream out = new FileOutputStream(f);
        resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, out);
           }
        }
        */
        iur.bmp = ImageUtils.getBitmapFromFile(f, Utils.HEIGHT_THUMB_NEWSTATUS, true);

        File file = new File(f);

        int type = Integer.parseInt(Utils.getPreference(mContext).getString("prf_service_image", "1"));

        MediaProvider mp = MediaProvider.YFROG;

        if (type == 1) {
            mp = MediaProvider.YFROG;
        } else if (type == 2) {
            mp = MediaProvider.TWITPIC;
        } else if (type == 3) {
            mp = MediaProvider.PLIXI;
        }

        loadConsumerKeys();

        ConfigurationBuilder confBuild = new ConfigurationBuilder();
        confBuild.setOAuthConsumerKey(consumerKey);
        confBuild.setOAuthConsumerSecret(consumerSecretKey);
        confBuild.setOAuthAccessToken(twitter.getOAuthAccessToken().getToken());
        confBuild.setOAuthAccessTokenSecret(twitter.getOAuthAccessToken().getTokenSecret());
        if (type == 1) {
        } else if (type == 2) {
            confBuild.setMediaProviderAPIKey("e3533af853e8f63a0018a2c63d7ee69f");
        } else if (type == 3) {
            confBuild.setMediaProviderAPIKey("57438faa-51e4-43a5-9e15-0c63b6f73950");
        }

        confBuild.setMediaProvider(mp.name());
        Configuration config = confBuild.build();

        ImageUpload upload = new ImageUploadFactory(config).getInstance(mp);
        if (upload != null) {
            iur.url = upload.upload(file);
            iur.error = false;
        } else {
            iur.error = true;
        }

    } catch (TwitterException e) {
        e.printStackTrace();
        iur.error = true;

    } catch (Exception e) {
        e.printStackTrace();
        iur.error = true;
    }
    return iur;
}

From source file:de.vanita5.twittnuker.util.Utils.java

License:Open Source License

public static Twitter getTwitterInstance(final Context context, final long accountId,
        final boolean includeEntities, final boolean includeRetweets, final boolean apacheHttp,
        final String mediaProvider, final String mediaProviderAPIKey) {
    if (context == null)
        return null;
    final TwittnukerApplication app = TwittnukerApplication.getInstance(context);
    final SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
    final int connection_timeout = prefs.getInt(KEY_CONNECTION_TIMEOUT, 10) * 1000;
    final boolean enableGzip = prefs.getBoolean(KEY_GZIP_COMPRESSING, true);
    final boolean ignoreSSLError = prefs.getBoolean(KEY_IGNORE_SSL_ERROR, false);
    final boolean enableProxy = prefs.getBoolean(KEY_ENABLE_PROXY, false);
    // Here I use old consumer key/secret because it's default key for older
    // versions//  w  w  w  .  java  2s  .c  om
    final String where = Where.equals(new Column(Accounts.ACCOUNT_ID), accountId).getSQL();
    final Cursor c = ContentResolverUtils.query(context.getContentResolver(), Accounts.CONTENT_URI,
            Accounts.COLUMNS, where, null, null);
    if (c == null)
        return null;
    try {
        if (!c.moveToFirst())
            return null;
        final ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setHostAddressResolverFactory(new TwidereHostResolverFactory(app));
        if (apacheHttp) {
            cb.setHttpClientFactory(new TwidereHttpClientFactory(app));
        }
        cb.setHttpConnectionTimeout(connection_timeout);
        cb.setGZIPEnabled(enableGzip);
        cb.setIgnoreSSLError(ignoreSSLError);
        if (enableProxy) {
            final String proxy_host = prefs.getString(KEY_PROXY_HOST, null);
            final int proxy_port = ParseUtils.parseInt(prefs.getString(KEY_PROXY_PORT, "-1"));
            if (!isEmpty(proxy_host) && proxy_port > 0) {
                cb.setHttpProxyHost(proxy_host);
                cb.setHttpProxyPort(proxy_port);
            }
        }
        final String prefConsumerKey = prefs.getString(KEY_CONSUMER_KEY, TWITTER_CONSUMER_KEY_2);
        final String prefConsumerSecret = prefs.getString(KEY_CONSUMER_SECRET, TWITTER_CONSUMER_SECRET_2);
        final String apiUrlFormat = c.getString(c.getColumnIndex(Accounts.API_URL_FORMAT));
        final String consumerKey = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_KEY)));
        final String consumerSecret = trim(c.getString(c.getColumnIndex(Accounts.CONSUMER_SECRET)));
        final boolean sameOAuthSigningUrl = c.getInt(c.getColumnIndex(Accounts.SAME_OAUTH_SIGNING_URL)) == 1;
        final boolean noVersionSuffix = c.getInt(c.getColumnIndex(Accounts.NO_VERSION_SUFFIX)) == 1;
        if (!isEmpty(apiUrlFormat)) {
            final String versionSuffix = noVersionSuffix ? null : "/1.1/";
            cb.setRestBaseURL(getApiUrl(apiUrlFormat, "api", versionSuffix));
            cb.setOAuthBaseURL(getApiUrl(apiUrlFormat, "api", "/oauth/"));
            cb.setUploadBaseURL(getApiUrl(apiUrlFormat, "upload", versionSuffix));
            if (!sameOAuthSigningUrl) {
                cb.setSigningRestBaseURL(DEFAULT_SIGNING_REST_BASE_URL);
                cb.setSigningOAuthBaseURL(DEFAULT_SIGNING_OAUTH_BASE_URL);
                cb.setSigningUploadBaseURL(DEFAULT_SIGNING_UPLOAD_BASE_URL);
            }
        }
        if (isOfficialConsumerKeySecret(context, consumerKey, consumerSecret)) {
            setMockOfficialUserAgent(context, cb);
        } else {
            setUserAgent(context, cb);
        }

        if (!isEmpty(mediaProvider)) {
            cb.setMediaProvider(mediaProvider);
        }
        if (!isEmpty(mediaProviderAPIKey)) {
            cb.setMediaProviderAPIKey(mediaProviderAPIKey);
        }
        cb.setIncludeEntitiesEnabled(includeEntities);
        cb.setIncludeRTsEnabled(includeRetweets);
        cb.setIncludeReplyCountEnabled(true);
        cb.setIncludeDescendentReplyCountEnabled(true);
        switch (c.getInt(c.getColumnIndexOrThrow(Accounts.AUTH_TYPE))) {
        case Accounts.AUTH_TYPE_OAUTH:
        case Accounts.AUTH_TYPE_XAUTH: {
            if (!isEmpty(consumerKey) && !isEmpty(consumerSecret)) {
                cb.setOAuthConsumerKey(consumerKey);
                cb.setOAuthConsumerSecret(consumerSecret);
            } else if (!isEmpty(prefConsumerKey) && !isEmpty(prefConsumerSecret)) {
                cb.setOAuthConsumerKey(prefConsumerKey);
                cb.setOAuthConsumerSecret(prefConsumerSecret);
            } else {
                cb.setOAuthConsumerKey(TWITTER_CONSUMER_KEY_2);
                cb.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET_2);
            }
            final String token = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN));
            final String tokenSecret = c.getString(c.getColumnIndexOrThrow(Accounts.OAUTH_TOKEN_SECRET));
            if (isEmpty(token) || isEmpty(tokenSecret))
                return null;
            cb.setOAuthAccessToken(token);
            cb.setOAuthAccessTokenSecret(tokenSecret);
            return new TwitterFactory(cb.build()).getInstance(new AccessToken(token, tokenSecret));
        }
        case Accounts.AUTH_TYPE_BASIC: {
            final String screenName = c.getString(c.getColumnIndexOrThrow(Accounts.SCREEN_NAME));
            final String username = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_USERNAME));
            final String loginName = username != null ? username : screenName;
            final String password = c.getString(c.getColumnIndexOrThrow(Accounts.BASIC_AUTH_PASSWORD));
            if (isEmpty(loginName) || isEmpty(password))
                return null;
            return new TwitterFactory(cb.build()).getInstance(new BasicAuthorization(loginName, password));
        }
        case Accounts.AUTH_TYPE_TWIP_O_MODE: {
            return new TwitterFactory(cb.build()).getInstance(new TwipOModeAuthorization());
        }
        default: {
            return null;
        }
        }
    } finally {
        c.close();
    }
}