private static String getStringFromClassPath(String path) throws Exception {
   InputStream is = null;
   InputStreamReader isr = null;
   BufferedReader br = null;
   try {
     is = DAOTest.class.getResourceAsStream(path);
     if (is == null) {
       throw new IllegalStateException(path + " not found.");
     }
     isr = new InputStreamReader(is);
     br = new BufferedReader(isr);
     StringBuilder buf = new StringBuilder();
     String line;
     while ((line = br.readLine()) != null) {
       buf.append(line);
     }
     return buf.toString();
   } finally {
     if (is != null) {
       is.close();
       isr.close();
       br.close();
     }
   }
 }
Exemple #2
0
  // loadArray Method loads a file into an array and returns the array
  ArrayList<String> loadArray(String thefile) {
    // System.out.println("Load array");

    ArrayList<String> myarray = new ArrayList<String>();

    try {
      BufferedReader an = new BufferedReader(new FileReader(thefile));
      String line;

      while ((line = an.readLine()) != null) {
        myarray.add(line);
      }
      an.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    ;

    return myarray;
  }
Exemple #3
0
    public void onStatus(Status status) {
      Twitter twitter = new TwitterFactory().getInstance();
      System.out.println("Status: " + status.getText());
      String[] parts = status.getText().split(" ");

      if (status.getText().startsWith("mech2015task add")) {
        try {
          Status st =
              twitter.updateStatus(
                  "@"
                      + status.getUser().getScreenName()
                      + " "
                      + status.getText().substring(17, status.getText().length())
                      + "をタスクに追加しました。");
        } catch (TwitterException e) {
        }
        try {
          File file = new File("task.txt");
          copyFile(file, new File("task_old.txt"));

          if (parts.length > 2) {
            BufferedWriter bw = new BufferedWriter(new FileWriter(file, true));
            bw.write(status.getText().substring(17, status.getText().length()));
            bw.newLine();
            bw.close();
          }
        } catch (IOException e) {
        }
      } else if (status.getText().equals("mech2015task list")) {
        //                try{
        //                Status st = twitter.updateStatus("@" + status.getUser().getScreenName() +
        // " " + status.getText().substring(17, status.getText().length()));
        //                }
        //                catch(TwitterException e){}
        try {
          //                    if (parts.length > 2) {
          File file = new File("task.txt");
          BufferedReader br = new BufferedReader(new FileReader(file));
          String str = br.readLine();
          String text = "";
          while (str != null) {
            System.out.println(str);

            text += " " + str + "\n";
            System.out.println(text);
            str = br.readLine();
          }
          br.close();
          Status st =
              twitter.updateStatus(
                  "@" + status.getUser().getScreenName() + " 現在のタスクは、\n" + text + "です。");

          /*                    bw.write(status.getText().substring(17, status.getText().length()));
          bw.newLine();
          bw.close();*/
          //                    }
        } catch (IOException e) {
        } catch (TwitterException e) {
        }
      } else if (status.getText().startsWith("mech2015task delete")) {
        String delete_str = status.getText().substring(20, status.getText().length());
        String text = "";
        System.out.println(delete_str);
        //                try{
        //                Status st = twitter.updateStatus("@" + status.getUser().getScreenName() +
        // " " + status.getText().substring(17, status.getText().length()));
        //                }
        //                catch(TwitterException e){}
        try {
          //                    if (parts.length > 2) {
          File file = new File("task_old.txt");
          copyFile(new File("task.txt"), file);
          File file_out = new File("task.txt");
          BufferedWriter bw = new BufferedWriter(new FileWriter(file_out));
          BufferedReader br = new BufferedReader(new FileReader(file));
          String str = br.readLine();

          while (str != null) {
            System.out.println(str);
            if (str.contains(delete_str)) {
              text += " " + str + "\n";

            } else {
              bw.write(str);
              bw.newLine();
            }

            // System.out.println(text);
            str = br.readLine();
          }
          br.close();
          bw.close();
          if (!text.equals("")) {
            Status st =
                twitter.updateStatus(
                    "@" + status.getUser().getScreenName() + " " + text + "をタスクから削除しました。");
          } else {
            Status st =
                twitter.updateStatus("@" + status.getUser().getScreenName() + " delete failed");
          }
          /*                    bw.write(status.getText().substring(17, status.getText().length()));
          bw.newLine();
          bw.close();*/
          //                    }
        } catch (IOException e) {
        } catch (TwitterException e) {
        }
      }
      if (status.getText().equals("ご注文は")) {
        try {
          Status st = twitter.updateStatus("@" + status.getUser().getScreenName() + " うさぎですか?");
        } catch (TwitterException e) {
        }
      }
      //  System.out.println("@" + status.getUser().getScreenName() + " | " + status.getText() + " 【
      // https://twitter.com/" + status.getUser().getScreenName() + "/status/" + status.getId() + "
      // 】");
      // こんな感じでstatusについている名前とかを色々表示させるとさらに欲しい情報にたどり着けると思います
    }
  public List<Tweet> readFile(String fileLocation, int limit, String fromTweetId) {

    long startTime = System.currentTimeMillis();

    int errors = 0;
    int added = 0;
    int ignored = 0;
    int skipped = 0;

    List<Tweet> tweets = new ArrayList<Tweet>();

    try {

      // Read gzFile
      InputStream inputStream = new GZIPInputStream(new FileInputStream(fileLocation));
      Reader decoder = new InputStreamReader(inputStream);
      BufferedReader br = new BufferedReader(decoder);

      String status;

      while ((status = br.readLine()) != null) {

        // Ignore garbage and log output lines, all statuses start with JSON bracket
        if (!status.equals("") && status.charAt(0) == '{') {
          try {
            JSONObject jsonObject = new JSONObject(status);

            // We use created_at as an indicator that this is a Tweet.
            if (jsonObject.has("created_at")) {

              Status statusObject = TwitterObjectFactory.createStatus(status);

              Tweet tweet = this.getTweetObjectFromStatus(statusObject);

              if (fromTweetId != null) {

                if (fromTweetId.equals(tweet.getId())) {
                  this.log.write("StatusFileReader - Scanner pickup from " + fromTweetId);
                  fromTweetId = null;
                } else {
                  skipped++;
                }

                continue;
              }

              added++;
              tweets.add(tweet);

              if (limit > 0 && added >= limit) {
                break;
              }

            } else {
              ignored++;
            }

          } catch (JSONException e) {
            this.log.write(
                "Exception in StatusFileReader: Json Parse Failure on: "
                    + status
                    + ", "
                    + e.getMessage());
          }
        } else {
          ignored++;
        }
      }

      br.close();
      decoder.close();
      inputStream.close();

    } catch (Exception e) {
      this.log.write(
          "Exception in StatusFileReader: Error Reading File: "
              + e.getClass().getName()
              + ": "
              + e.getMessage());
    }

    long runTimeSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - startTime);

    double ops = ((double) added / (double) Math.max(runTimeSeconds, 1));

    this.log.write(
        "StatusFileReader - "
            + fileLocation
            + " processed in "
            + runTimeSeconds
            + "s. "
            + added
            + " ok / "
            + errors
            + " errors / "
            + ignored
            + " ignored / "
            + skipped
            + " skipped. "
            + ops
            + " ops. Limit: "
            + limit
            + ", Fetch: "
            + tweets.size());

    return tweets;
  }