コード例 #1
0
  public void run() {
    try {
      System.out.println("Starting Twitter public stream consumer thread.");

      // Enter your consumer key and secret below
      OAuthService service =
          new ServiceBuilder()
              .provider(TwitterApi.class)
              .apiKey("qFiiPwXFjaiRsFAq5OSYHFT0f")
              .apiSecret("V9t7qXkbjglS76TKy6Xw9U2fukb1Nh1xmtKKR30kXR6noap9Qe")
              .build();

      // Set your access token
      Token accessToken =
          new Token(
              "66266917-XNT9WqkdFO8TQz6VQeCQtYEZIBdgpp82tUxCrIcIW",
              "2v4EMHESOz4yeajmjj8788mkGa2UOLAeUR1nrJ27tlSek");

      // Let's generate the request
      System.out.println("Connecting to Twitter Public Stream");
      OAuthRequest request = new OAuthRequest(Verb.POST, STREAM_URI);
      request.addHeader("version", "HTTP/1.1");
      request.addHeader("host", "stream.twitter.com");
      request.setConnectionKeepAlive(true);
      request.addHeader("user-agent", "Twitter Stream Reader");
      request.addBodyParameter(
          "track",
          "Bank of America, bofa, bofa_news, bofa_help, ROLB, BMB, Pingit, Ping-it, BarclaysMobileBanking, Barclays Live, Feature Store, Barclays, Barclaycard, BCS, BarclaysWealth, john mcfarlane, @Barclays, @Barclaysuk, hsbc, hsbc_press, citi, Citigroup Inc, RBS, Royal Bank Of Scotland, LLyods Bank, STAN, @StanChart, Santander, NatWest, Halifax, Tesco Bank"); // Set keywords you'd like to track here
      service.signRequest(accessToken, request);
      Response response = request.send();

      // Create a reader to read Twitter's stream
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getStream()));

      // Create a new file for raw tweets
      File file = new File("C:\\Users\\Striker\\Desktop\\Tweet Data\\raw_tweet.txt");
      FileWriter fw = new FileWriter(file.getAbsoluteFile());
      BufferedWriter bw = new BufferedWriter(fw);

      String line;
      while ((line = reader.readLine()) != null) {
        latestTweet = line;
        bw.write(line);
        bw.newLine();
        tweetCount++;
        System.out.println(line);
      }

      bw.close();
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }
  public void run() {
    try {
      System.out.println("Starting Twitter public stream consumer thread.");

      // Enter your consumer key and secret below
      OAuthService service =
          new ServiceBuilder()
              .provider(TwitterApi.class)
              .apiKey("0GglmCJSWdauygE0dPYVhRoYN")
              .apiSecret("aikSSCPcX1AOayVw98MKuPvalUKvPZhaESEJMawVGK9a9bfG0I")
              .build();

      // Set your access token
      Token accessToken =
          new Token(
              "863517241-dba29VzM7RaYMOvwFv03KOdkki6SVluRiRLqc1Ks",
              "cB8WS8duMCy4I30ZawZJFuv743jLdTWKLKkniMKfZOEQi");

      // Let's generate the request
      System.out.println("Connecting to Twitter Public Stream");
      OAuthRequest request = new OAuthRequest(Verb.POST, STREAM_URI);
      request.addHeader("version", "HTTP/1.1");
      request.addHeader("host", "stream.twitter.com");
      request.setConnectionKeepAlive(true);
      request.addHeader("user-agent", "Twitter Stream Reader");
      // request.addBodyParameter("track", "java,heroku,twitter"); // Set keywords you'd like to
      // track here
      request.addBodyParameter("follow", "4884271164"); // Set keywords you'd like to track here
      service.signRequest(accessToken, request);
      Response response = request.send();

      // Create a reader to read Twitter's stream
      BufferedReader reader = new BufferedReader(new InputStreamReader(response.getStream()));

      String line;
      while ((line = reader.readLine()) != null) {
        System.out.println(line);
      }
    } catch (IOException ioe) {
      ioe.printStackTrace();
    }
  }