예제 #1
0
  public void onConfigure(DashboardRepository repository) {
    for (Entry<Key, Map<String, ?>> settingsEntry :
        getSettings().getServiceSettings(TwitterService.class).entrySet()) {
      Key key = settingsEntry.getKey();

      TwitterSettings settings = new TwitterSettings(settingsEntry.getValue());

      String oAuthConsumerKey = settings.getApplicationKey().getKey();
      String oAuthConsumerSecret = settings.getApplicationKey().getSecret();

      List<Twitter> keyTwitters = new ArrayList<Twitter>();
      for (Entry<String, OAuthKey> tokenEntry : settings.getTokens().entrySet()) {

        OAuthKey token = tokenEntry.getValue();

        ConfigurationBuilder builder = new ConfigurationBuilder();
        builder.setOAuthConsumerKey(oAuthConsumerKey);
        builder.setOAuthConsumerSecret(oAuthConsumerSecret);
        builder.setOAuthAccessToken(token.getKey());
        builder.setOAuthAccessTokenSecret(token.getSecret());
        Configuration conf = builder.build();

        Authorization authorization = AuthorizationFactory.getInstance(conf);
        Twitter twitter = new TwitterFactory().getInstance(authorization);
        keyTwitters.add(twitter);
      }
      twitters.put(key, keyTwitters);
      mentions.put(key, new TreeSet<Status>());
      timeline.put(key, new TreeSet<Status>());
      repository.addDataSource(key, TwitterTimeline.class, new TwitterTimelineImpl(key, this));
      repository.addDataSource(key, TwitterMentions.class, new TwitterMentionsImpl(key, this));
    }
  }
예제 #2
0
  public static void main(String[] args) throws Exception {
    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(CONSUMER_KEY);
    builder.setOAuthConsumerSecret(CONSUMER_SECRET);
    builder.setOAuthAccessToken(ACCESS_TOKEN);
    builder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

    // 現行のTwitter4JではAPIのデフォルト呼び先がbetastream.twitter.comになっているので修正
    builder.setUserStreamBaseURL("https://userstream.twitter.com/2/");

    // Configurationを生成
    Configuration configuration = builder.build();

    /*        Configuration configuration = new ConfigurationBuilder().setOAuthConsumerKey(CONSUMER_KEY)
                    .setOAuthConsumerSecret(CONSUMER_SECRET)
                    .setOAuthAccessToken(ACCESS_TOKEN)
                    .setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET)
                    .build();
    */
    TwitterStream twStream = new TwitterStreamFactory(configuration).getInstance();
    // TwitterStream twStream = new TwitterStreamFactory().getInstance();
    twStream.addListener(new MyStatusListener());
    twStream.user();
    // twStream.sample();
  }
예제 #3
0
 /**
  * Constructor to set the Twitter Access Details The details here are from a sample account i
  * created on twitter. if you have your account you can use that instead of this.
  */
 public TwitterClient() {
   cb = new ConfigurationBuilder();
   cb.setOAuthConsumerKey("X4X5rpyaRNvbwcVnV8j5i1izh");
   cb.setOAuthConsumerSecret("dosoSEWt0DeZbl0zO3Y61fiymszTvqrOZT9xzE8ikdI9imy1rl");
   cb.setOAuthAccessToken("2807566405-1jYRRu0Vs3tj3D56YM6ww4DqjByJqhLCl9Vpru9");
   cb.setOAuthAccessTokenSecret("svKkTebpr8jBmrmnfXiaygoYQndXJMPvo6CWIVniYrDb0");
   twitter = new TwitterFactory(cb.build()).getInstance();
 }
예제 #4
0
 private static twitter4j.conf.Configuration getTwitterConfiguration(
     String strAccessToken, String token_secret) {
   ConfigurationBuilder builder = new ConfigurationBuilder();
   builder.setUseSSL(true);
   builder.setOAuthConsumerKey(Constants.OFFICIAL_TWITTER_CONSUMER_KEY);
   builder.setOAuthConsumerSecret(Constants.OFFICIAL_TWITTER_CONSUMER_SECRET_KEY);
   if (token_secret != null && strAccessToken != null) {
     builder.setOAuthAccessToken(strAccessToken).setOAuthAccessTokenSecret(token_secret);
   }
   return builder.build();
 }
예제 #5
0
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      ADOT4J4A adot4j4a = (ADOT4J4A) getActivity().getApplicationContext();

      ConfigurationBuilder confbuilder = new ConfigurationBuilder();
      confbuilder.setOAuthConsumerKey(ADOT4J4A.CONSUMER_KEY);
      confbuilder.setOAuthConsumerSecret(ADOT4J4A.CONSUMER_SECRET);
      confbuilder.setOAuthAccessToken(adot4j4a.getToken());
      confbuilder.setOAuthAccessTokenSecret(adot4j4a.getTokenSecret());
      mTwitter = new TwitterFactory(confbuilder.build()).getInstance();
    }
예제 #6
0
  // construct Twitter instance using configuration
  static {
    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey(CONSUMER_KEY);
    cb.setOAuthConsumerSecret(CONSUMER_SECRET);
    cb.setOAuthAccessToken(ACCESS_TOKEN);
    cb.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);

    Configuration conf = cb.build();

    twitter = new TwitterFactory(conf).getInstance();
    auth = new OAuth1(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  }
예제 #7
0
  public static final Twitter getTwitter(Context context) {
    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(context);

    ConfigurationBuilder builder = new ConfigurationBuilder();
    builder.setOAuthConsumerKey(context.getString(R.string.twitter_consumer_key));
    builder.setOAuthConsumerSecret(context.getString(R.string.twitter_consumer_secret));
    builder.setOAuthAccessToken(pref.getString(PREF_TOKEN, null));
    builder.setOAuthAccessTokenSecret(pref.getString(PREF_TOKEN_SECRET, null));

    final Twitter twitter = new TwitterFactory(builder.build()).getInstance();

    return twitter;
  }
  /**
   * Posts a status to Twitter.
   *
   * @param statusText Text of the messsage.
   * @param at AccessToken to validate with
   * @throws Exception
   */
  public void updateStatus(String statusText, AccessToken at) throws Exception {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey(ourConsumerKey);
    cb.setOAuthConsumerSecret(ourConsumerSecret);
    cb.setOAuthAccessToken(at.getToken());
    cb.setOAuthAccessTokenSecret(at.getTokenSecret());

    myTwitter.setOAuthAccessToken(at);

    Status status = myTwitter.updateStatus(statusText);
    System.out.println("Successfully updated the status to [" + status.getText() + "].");
  }
예제 #9
0
  /**
   * Method to get authorization from Twitter API.
   *
   * @return A ConfigurationBuilder object.
   */
  public ConfigurationBuilder getAuthorization() {
    ConfigurationBuilder cb = new ConfigurationBuilder();

    Config conf;
    try {
      conf = new Config();
    } catch (IOException e) {
      Logger.getLogger(TweetsRetriever.class.getName()).log(Level.SEVERE, null, e);
      return null;
    }

    cb.setOAuthConsumerKey(conf.getConsumerKey());
    cb.setOAuthConsumerSecret(conf.getConsumerSecret());
    cb.setOAuthAccessToken(conf.getAccessToken());
    cb.setOAuthAccessTokenSecret(conf.getAccessTokenSecret());

    return cb;
  }
  public static void main(String[] args) throws Exception {

    Context context = new Context("conf/producer.conf");
    Tweety tp = new Tweety();

    try {
      Class.forName("com.mysql.jdbc.Driver");
      String url = "jdbc:mysql://172.20.95.137/mongo_test?" + "user=manoj&password=qwerty1!";
      Connection conn = DriverManager.getConnection(url);
      System.out.println(url);
      Statement stmt = conn.createStatement();

      ConfigurationBuilder cb = new ConfigurationBuilder();
      cb.setOAuthConsumerKey("ujS6V0corQ4v8K4dp6bIlVYsq");
      cb.setOAuthConsumerSecret("XhmTLcS4BI6zy9fZ0xIOy1ZZkAnQYZ1jFrlHojWuWpx2RCWH9H");
      cb.setOAuthAccessToken("30427118-YNLB3WfviD4j7MvNgU4gZ7SPQbCDBXWh4kPZcHXrY");
      cb.setOAuthAccessTokenSecret("LkhcZUJbj33RBc1DTXrxJps4ELgog8WxoRS6gCReemOHc");
      cb.setJSONStoreEnabled(true);
      cb.setIncludeEntitiesEnabled(true);
      cb.setHttpProxyHost("proxy.tcs.com");
      cb.setHttpProxyPort(8080);
      cb.setHttpProxyUser("467098");
      cb.setHttpProxyPassword("See@0615");
      Twitter twitter = new TwitterFactory(cb.build()).getInstance();
      ResponseList<Location> locations;
      locations = twitter.getAvailableTrends();
      System.out.println("Showing available trends");
      Trends trends = twitter.getPlaceTrends(2295386);
      stmt.executeUpdate("drop table hashtag");
      stmt.executeUpdate("create table hashtag(name varchar(50),count int)");
      for (int i = 0; i <= 5; i++) {
        System.out.println(trends.getTrends()[i].getName());
        hashtags[i] = trends.getTrends()[i].getName();
        stmt.executeUpdate("insert into hashtag values (\'" + hashtags[i] + "\'" + "," + 0 + ")");
      }
      System.out.println("done.");

    } catch (Exception te) {
      te.printStackTrace();
      System.out.println("Failed to get trends: " + te.getMessage());
      System.exit(-1);
    }
    tp.start(context);
  }
예제 #11
0
  private static Twitter getTwitter(HandlerContext handlerContext) {
    Twitter res = (Twitter) handlerContext.get("twitter");
    if (res == null) {
      TwitterLinksHandlerConfig twitterLinksHandlerConfig =
          (TwitterLinksHandlerConfig) handlerContext.getHandlerConfig();
      ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
      configurationBuilder
          .setDebugEnabled(true)
          .setOAuthConsumerKey(twitterLinksHandlerConfig.getOauthConsumerKey());
      configurationBuilder.setOAuthConsumerSecret(
          twitterLinksHandlerConfig.getOauthConsumerSecret());
      configurationBuilder.setOAuthAccessToken(twitterLinksHandlerConfig.getOauthAccessToken());
      configurationBuilder.setOAuthAccessTokenSecret(
          twitterLinksHandlerConfig.getOauthAccessTokenSecret());
      TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build());
      res = twitterFactory.getInstance();

      handlerContext.put("twitter", res);
    }
    return res;
  }
  /**
   * The initialization method for the Source. The context contains all the Flume configuration
   * info, and can be used to retrieve any configuration values necessary to set up the Source.
   */
  @Override
  public void configure(Context context) {
    consumerKey = context.getString(TwitterSourceConstants.CONSUMER_KEY_KEY);
    consumerSecret = context.getString(TwitterSourceConstants.CONSUMER_SECRET_KEY);
    accessToken = context.getString(TwitterSourceConstants.ACCESS_TOKEN_KEY);
    accessTokenSecret = context.getString(TwitterSourceConstants.ACCESS_TOKEN_SECRET_KEY);

    String keywordString = context.getString(TwitterSourceConstants.KEYWORDS_KEY, "");
    keywords = keywordString.split(",");
    for (int i = 0; i < keywords.length; i++) {
      keywords[i] = keywords[i].trim();
    }

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
  }
  public void loadMenu() throws InterruptedException {

    System.out.print("Please choose a name for your stream:\t");

    Scanner input = new Scanner(System.in);
    String keyword = input.nextLine();

    connectdb(keyword);

    cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true);
    cb.setOAuthConsumerKey("XXX");
    cb.setOAuthConsumerSecret("XXX");
    cb.setOAuthAccessToken("XXX");
    cb.setOAuthAccessTokenSecret("XXX");

    TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    StatusListener listener =
        new StatusListener() {

          public void onStatus(Status status) {
            System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());

            BasicDBObject basicObj = new BasicDBObject();
            basicObj.put("user_name", status.getUser().getScreenName());
            basicObj.put("retweet_count", status.getRetweetCount());
            basicObj.put("tweet_followers_count", status.getUser().getFollowersCount());
            basicObj.put("source", status.getSource());
            // basicObj.put("coordinates",tweet.getGeoLocation());

            UserMentionEntity[] mentioned = status.getUserMentionEntities();
            basicObj.put("tweet_mentioned_count", mentioned.length);
            basicObj.put("tweet_ID", status.getId());
            basicObj.put("tweet_text", status.getText());

            try {
              items.insert(basicObj);
            } catch (Exception e) {
              System.out.println("MongoDB Connection Error : " + e.getMessage());
            }
          }

          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
            System.out.println(
                "Got a status deletion notice id:" + statusDeletionNotice.getStatusId());
          }

          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
            System.out.println("Got track limitation notice:" + numberOfLimitedStatuses);
          }

          public void onScrubGeo(long userId, long upToStatusId) {
            System.out.println(
                "Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId);
          }

          @Override
          public void onStallWarning(StallWarning stallWarning) {
            // To change body of implemented methods use File | Settings | File Templates.
          }

          public void onException(Exception ex) {
            ex.printStackTrace();
          }
        };

    FilterQuery fq = new FilterQuery();
    String keywords[] = {"Germany"};

    fq.track(keywords);

    twitterStream.addListener(listener);
    twitterStream.filter(fq);
  }
  private void getAndStoreTweetsOnTopic(String topic) {

    // Configuring twitter
    ConfigurationBuilder configBuilder = new ConfigurationBuilder();
    configBuilder.setOAuthAccessToken(ACCESS_TOKEN);
    configBuilder.setOAuthAccessTokenSecret(ACCESS_TOKEN_SECRET);
    configBuilder.setOAuthConsumerKey(CONSUMER_KEY);
    configBuilder.setOAuthConsumerSecret(CONSUMER_KEY_SECRET);

    Configuration configuration = configBuilder.build();

    TwitterFactory factory = new TwitterFactory(configuration);

    Twitter twitter = factory.getInstance();

    Query query = new Query(topic);

    // number of tweets to return per page
    query.setCount(100);

    try {

      // search for tweets on specifc topics
      QueryResult search = twitter.search(query);

      List<Status> tweets = search.getTweets();

      // initialize Mongo DB
      Mongo mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT);
      DB db = mongo.getDB(MONGO_DB_NAME);
      DBCollection collection = db.getCollection(MONGO_DB_COLLECTION);

      for (Status tweet : tweets) {

        System.out.println(getTweetInfo(tweet));

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dateWithoutTime = sdf.parse(sdf.format(tweet.getCreatedAt()));

        // create a document
        BasicDBObject document = new BasicDBObject();
        document.put("createdAt", dateWithoutTime);
        document.put("user", tweet.getUser().getName());
        document.put("rTCount", tweet.getRetweetCount());
        document.put("tweet", tweet.getText());
        document.put("source", tweet.getSource());

        // store it in database
        collection.insert(document);
      }

      /**
       * // Find all records from collection DBCursor cursorDoc = collection.find();
       *
       * <p>// ... and display while (cursorDoc.hasNext()) { System.out.println(cursorDoc.next());
       * }*
       */
    } catch (TwitterException e) {
      e.printStackTrace();
    } catch (UnknownHostException e) {
      e.printStackTrace();
    } catch (ParseException e) {
      e.printStackTrace();
    }
  }
예제 #15
0
  private void start(final Context context) {

    /** Producer properties * */
    Properties props = new Properties();
    props.put("metadata.broker.list", context.getString(BROKER_LIST));
    props.put("serializer.class", context.getString(SERIALIZER));
    props.put("request.required.acks", context.getString(REQUIRED_ACKS));

    ProducerConfig config = new ProducerConfig(props);
    final Producer<String, String> producer = new Producer<String, String>(config);

    /** Twitter properties * */
    consumerKey = context.getString(CONSUMER_KEY_KEY);
    consumerSecret = context.getString(CONSUMER_SECRET_KEY);
    accessToken = context.getString(ACCESS_TOKEN_KEY);
    accessTokenSecret = context.getString(ACCESS_TOKEN_SECRET_KEY);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);

    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
    final boolean shouldPrintTweetsOnScreen =
        Boolean.parseBoolean(context.getString(printTweetsOnScreen));
    final boolean shouldSendTweetsToKafka =
        Boolean.parseBoolean(context.getString(sendTweetsToKafka));

    final StatusListener listener =
        new StatusListener() {
          // The onStatus method is executed every time a new tweet comes
          // in.
          public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the
            // the raw JSON of a tweet
            if (shouldPrintTweetsOnScreen) {
              logger.info(status.getUser().getScreenName() + ": " + status.getText());
            }

            if (shouldSendTweetsToKafka) {
              KeyedMessage<String, String> data =
                  new KeyedMessage<String, String>(
                      context.getString(KAFKA_TOPIC), TwitterObjectFactory.getRawJSON(status));
              producer.send(data);
            }
          }

          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

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

          public void onException(Exception ex) {
            logger.info("Shutting down Twitter sample stream...");
            twitterStream.shutdown();
          }

          public void onStallWarning(StallWarning warning) {}
        };

    twitterStream.addListener(listener);

    twitterStream.filter(new FilterQuery().track(context.getString(keywords).split(",")));
  }
  private void start(final Context context) throws IOException {

    // Producer properties
    Properties props = new Properties();
    props.put("metadata.broker.list", context.getString(TwitterSourceConstant.BROKER_LIST));
    props.put("serializer.class", context.getString(TwitterSourceConstant.SERIALIZER));
    props.put("partitioner.class", context.getString(TwitterSourceConstant.PARTITIONER));
    props.put("request.required.acks", context.getString(TwitterSourceConstant.REQUIRED_ACKS));

    ProducerConfig config = new ProducerConfig(props);

    final Producer<String, String> producer = new Producer<String, String>(config);

    /** Twitter properties * */
    consumerKey = context.getString(TwitterSourceConstant.CONSUMER_KEY_KEY);
    consumerSecret = context.getString(TwitterSourceConstant.CONSUMER_SECRET_KEY);
    accessToken = context.getString(TwitterSourceConstant.ACCESS_TOKEN_KEY);
    accessTokenSecret = context.getString(TwitterSourceConstant.ACCESS_TOKEN_SECRET_KEY);

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setOAuthConsumerKey(consumerKey);
    cb.setOAuthConsumerSecret(consumerSecret);
    cb.setOAuthAccessToken(accessToken);
    cb.setOAuthAccessTokenSecret(accessTokenSecret);
    cb.setJSONStoreEnabled(true);
    cb.setIncludeEntitiesEnabled(true);
    cb.setHttpProxyHost("proxy.tcs.com");
    cb.setHttpProxyPort(8080);
    cb.setHttpProxyUser("876216");
    cb.setHttpProxyPassword("Apple@123");
    twitterStream = new TwitterStreamFactory(cb.build()).getInstance();

    final Map<String, String> headers = new HashMap<String, String>();

    /** Twitter listener * */
    StatusListener listener =
        new StatusListener() {
          // The onStatus method is executed every time a new tweet comes
          // in.
          public void onStatus(Status status) {
            // The EventBuilder is used to build an event using the
            // the raw JSON of a tweet

            System.out.println("Listening :");
            KeyedMessage<String, String> data =
                new KeyedMessage<String, String>(
                    "testing1", TwitterObjectFactory.getRawJSON(status));

            producer.send(data);
            System.out.println(data);
          }

          public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {}

          public void onTrackLimitationNotice(int numberOfLimitedStatuses) {}

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

          public void onException(Exception ex) {
            logger.info("ShutDown");
            twitterStream.shutdown();
          }

          public void onStallWarning(StallWarning warning) {}
        };

    twitterStream.addListener(listener);
    /** GOGOGO * */
    twitterStream.sample();
    FilterQuery query =
        new FilterQuery()
            .track(
                Tweety.hashtags[0],
                Tweety.hashtags[1],
                Tweety.hashtags[2],
                Tweety.hashtags[3],
                Tweety.hashtags[4]);
    twitterStream.filter(query);
    /** Bind the listener * */
  }