Example usage for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

List of usage examples for twitter4j.conf ConfigurationBuilder ConfigurationBuilder

Introduction

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

Prototype

ConfigurationBuilder

Source Link

Usage

From source file:com.github.moko256.twitlatte.GlobalApplication.java

License:Apache License

@NonNull
public Twitter createTwitterInstance(@NonNull AccessToken accessToken) {
    Twitter t;/*from w w w. j  a v a  2  s  .  c o  m*/

    Configuration conf;

    if (accessToken.getType() == ClientType.TWITTER) {
        conf = new ConfigurationBuilder().setTweetModeExtended(true)
                .setOAuthConsumerKey(BuildConfig.CONSUMER_KEY)
                .setOAuthConsumerSecret(BuildConfig.CONSUMER_SECRET).setOAuthAccessToken(accessToken.getToken())
                .setOAuthAccessTokenSecret(accessToken.getTokenSecret()).build();

        replaceCompatibleOkHttpClient(conf);

        t = twitterCache.get(conf);

        if (t == null) {
            t = AlternativeTwitterFactoryKt.createTwitterInstance(conf);
            twitterCache.put(conf, t);
        }
    } else {
        conf = new ConfigurationBuilder().setOAuthAccessToken(accessToken.getToken())
                .setRestBaseURL(accessToken.getUrl()).build();

        replaceCompatibleOkHttpClient(conf);

        t = twitterCache.get(conf);

        if (t == null) {
            t = new MastodonTwitterImpl(conf, accessToken.getUserId(), getOkHttpClient(conf).newBuilder());
            twitterCache.put(conf, t);
        }
    }

    return t;
}

From source file:com.hortonworks.amuise.cdrstorm.kafka.producers.CDRTestDataProducer.java

private void start() {

    /**//from ww  w.j  av a 2  s  .co m
     * Kafka Twitter Producer properties *
     */
    Properties twitterconprops = new Properties();
    twitterconprops.put("metadata.broker.list", globalconfigs.getProperty("twitter4j.brokerlist"));
    twitterconprops.put("serializer.class", globalconfigs.getProperty("twitter4j.serializer"));
    twitterconprops.put("request.required.acks", globalconfigs.getProperty("twitter4j.requiredacks"));
    ProducerConfig twitterproducerconfig = new ProducerConfig(twitterconprops);
    final Producer<String, String> twitterproducer = new Producer<String, String>(twitterproducerconfig);

    /**
     * Kafka CDR Producer properties *
     */
    Properties cdrconprops = new Properties();
    cdrconprops.put("metadata.broker.list", globalconfigs.getProperty("cdr.brokerlist"));
    cdrconprops.put("serializer.class", globalconfigs.getProperty("cdr.serializer"));
    cdrconprops.put("request.required.acks", globalconfigs.getProperty("cdr.requiredacks"));
    ProducerConfig cdrproducerconfig = new ProducerConfig(cdrconprops);
    final Producer<String, String> cdrproducer = new Producer<String, String>(cdrproducerconfig);

    /**
     * Twitter4j properties *
     */
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(globalconfigs.getProperty("twitter4j.consumerkey"));
    cb.setOAuthConsumerSecret(globalconfigs.getProperty("twitter4j.consumersecretkey"));
    cb.setOAuthAccessToken(globalconfigs.getProperty("twitter4j.accesstokenkey"));
    cb.setOAuthAccessTokenSecret(globalconfigs.getProperty("twitter4j.accesstokensecretkey"));
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    /**
     * Twitter listener *
     */
    StatusListener listener = new StatusListener() {
        // The onStatus method is executed every time a new tweet comes
        // in.
        public void onStatus(Status status) {

            StringBuilder sb = new StringBuilder();
            sb.append(status.getUser().getScreenName());
            sb.append("|");
            sb.append(status.getCreatedAt());
            sb.append("|");
            sb.append(status.getRetweetCount());
            sb.append("|");
            sb.append(status.getSource());
            sb.append("|");
            sb.append(status.getText());

            //call CDR create message
            String cdrmessage = createCDRMessage(status.getText());

            //Debug output
            System.out.println("_________________________________________________________");
            System.out.println(sb.toString());
            System.out.println("cdr message: " + cdrmessage);
            System.out.println("_________________________________________________________");

            //call producer for tweet
            KeyedMessage<String, String> twitterdata = new KeyedMessage<String, String>(
                    globalconfigs.getProperty("twitter4j.kafkatopic"), sb.toString());
            twitterproducer.send(twitterdata);

            //call producer for cdr
            KeyedMessage<String, String> cdrmessagedata = new KeyedMessage<String, String>(
                    globalconfigs.getProperty("cdr.kafkatopic"), cdrmessage);
            cdrproducer.send(cdrmessagedata);

        }

        public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
        }

        public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
        }

        public void onScrubGeo(long userId, long upToStatusId) {
        }

        public void onException(Exception ex) {
            System.out.println("General Exception: shutting down Twitter sample stream...");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
            twitterStream.shutdown();
        }

        public void onStallWarning(StallWarning warning) {
        }
    };

    twitterStream.addListener(listener);

    // Filter stream with targeted words
    String filterstring = globalconfigs.getProperty("twitter4j.filterwords");
    FilterQuery filterq = new FilterQuery();
    filterq.track(filterstring.split(","));
    twitterStream.filter(filterq);

    //twitterStream.sample();
}

From source file:com.ibuildapp.romanblack.CameraPlugin.SharingActivity.java

License:Open Source License

/**
 * Reinitializes twitter credentials.//from w ww  .j  ava  2  s .  c  om
 * @return the twitter instance
 */
private Twitter reInitTwitter() {
    com.appbuilder.sdk.android.authorization.entities.User twitterUser = Authorization
            .getAuthorizedUser(Authorization.AUTHORIZATION_TYPE_TWITTER);
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setDebugEnabled(true).setOAuthAccessToken(twitterUser.getAccessToken())
            .setOAuthAccessTokenSecret(twitterUser.getAccessTokenSecret())
            .setOAuthConsumerSecret(Statics.TWITTER_CONSUMER_SECRET)
            .setOAuthConsumerKey(Statics.TWITTER_CONSUMER_KEY);
    Configuration configuration = builder.build();
    return new TwitterFactory(configuration).getInstance();
}

From source file:com.ijuru.tweetgrab.Context.java

License:Open Source License

/**
 * Starts the application// w  w w . j  a  va  2 s  .  com
 * @throws Exception 
 */
public static void startApplication() throws Exception {
    // Look for environment variable or system property
    String optionsJSON = System.getenv(ENVVAR_NAME);
    if (optionsJSON == null) {
        optionsJSON = System.getProperty(JVMPROP_NAME);
        if (optionsJSON == null) {
            throw new Exception("Could not find environment variable (" + ENVVAR_NAME + ") or JVM property ("
                    + JVMPROP_NAME + ")");
        }
    }

    ObjectMapper mapper = new ObjectMapper();
    options = mapper.readValue(optionsJSON, Options.class);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(false).setOAuthConsumerKey(options.getCredentials().get("consumerKey"))
            .setOAuthConsumerSecret(options.getCredentials().get("consumerSecret"))
            .setOAuthAccessToken(options.getCredentials().get("accessToken"))
            .setOAuthAccessTokenSecret(options.getCredentials().get("accessTokenSecret"));

    twitterFactory = new TwitterFactory(cb.build());
}

From source file:com.illusionaryone.TwitterAPI.java

License:Open Source License

public Boolean authenticate() {
    com.gmt2001.Console.debug.println("Attempting to Authenticate");
    try {/*from   w ww .jav  a  2  s  .c  o m*/
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setOAuthConsumerKey(consumerKey);
        configurationBuilder.setOAuthConsumerSecret(consumerSecret);
        configurationBuilder.setOAuthAccessToken(oauthAccessToken);
        configurationBuilder.setOAuthAccessTokenSecret(oauthAccessSecret);

        TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build());
        twitter = twitterFactory.getInstance();

        accessToken = twitter.getOAuthAccessToken();
        com.gmt2001.Console.out.println("Authenticated with Twitter API");
        return true;
    } catch (TwitterException ex) {
        com.gmt2001.Console.err.println("Twitter Auth Failed: " + ex.getMessage());
        accessToken = null;
        return false;
    }
}

From source file:com.infine.android.devoxx.service.TwitterService.java

License:Apache License

@Override
public void onCreate() {
    super.onCreate();
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("************").setOAuthConsumerSecret("****************")
            .setOAuthAccessToken("***************").setOAuthAccessTokenSecret("**************");
    TwitterFactory tf = new TwitterFactory(cb.build());
    mTwitter = tf.getInstance();/*from ww  w.j  av  a  2s.c o m*/
    mResolver = getContentResolver();

}

From source file:com.irontec.mintzatu.EzarpenakDetailActivity.java

private void askOAuth() {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setOAuthConsumerKey(TwitterHelper.CONSUMER_KEY);
    configurationBuilder.setOAuthConsumerSecret(TwitterHelper.CONSUMER_SECRET);
    Configuration configuration = configurationBuilder.build();
    twitter = new TwitterFactory(configuration).getInstance();

    try {/* www  .  j ava 2  s  . co  m*/
        requestToken = twitter.getOAuthRequestToken(TwitterHelper.CALLBACK_URL);
        Intent i = new Intent(this, WebviewActivity.class);
        i.putExtra("URL", requestToken.getAuthenticationURL());
        startActivityForResult(i, TWITTER_AUTH);
    } catch (TwitterException e) {
        e.printStackTrace();
    }
}

From source file:com.isdp.twitterposter.TwitterManager.java

License:Open Source License

public void initTwitter() {
    ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
    configurationBuilder.setDebugEnabled(true).setOAuthConsumerKey(OAUTH_CONSUMER_KEY)
            .setOAuthConsumerSecret(OAUTH_CONSUMER_SECRET).setOAuthAccessToken(OAUTH_ACCESS_TOKEN)
            .setOAuthAccessTokenSecret(OAUTH_ACCESS_TOKEN_SECRET);
    configuration = configurationBuilder.build();

}

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

License:Apache License

@Override
public BaseResponse loadInBackground() {

    try {/*from  ww  w  . j  a va2  s.  com*/

        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 w ww  .j  av  a 2s  . 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;
}