Exemplo n.º 1
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についている名前とかを色々表示させるとさらに欲しい情報にたどり着けると思います
    }
Exemplo n.º 2
0
  public static void main(String[] args) {

    try {

      meet myobj = new meet();

      // load jsiconfig.txt
      ArrayList<String> myconfiglist = new ArrayList<String>();
      myconfiglist = myobj.loadArray("jsiconfig.txt");

      // The text uri
      // "mongodb://*****:*****@ds023288.mongolab.com:23288/sample";
      String textUri = myconfiglist.get(0);

      // Create MongoClientURI object from which you get MongoClient obj
      MongoClientURI uri = new MongoClientURI(textUri);

      // Connect to that uri
      MongoClient m = new MongoClient(uri);

      // get the database named sample
      String DBname = myconfiglist.get(1);
      DB d = m.getDB(DBname);

      // get the collection mycollection in sample
      String collectionName = myconfiglist.get(2);
      DBCollection collection = d.getCollection(collectionName);

      // System.out.println("Config: "+textUri+":"+DBname+":"+collectionName);

      // twitter4j
      // Twitter twitter = new TwitterFactory().getInstance();
      Twitter twitter = new TwitterFactory().getSingleton();
      User user = twitter.verifyCredentials();

      // Twitter collection of latest tweets into the home account - defaulted to latest 20 tweets//
      //////////////////////////////////////////////////////////////

      ArrayList<String> mylatesttweetslist = new ArrayList<String>();
      // get list of tweets from a user or the next tweet listed
      try {
        long actid = 0;
        // twitter.createFriendship(actid);

        // The factory instance is re-useable and thread safe.
        // Twitter twitter = TwitterFactory.getSingleton();
        List<Status> statuses = twitter.getHomeTimeline();
        System.out.println("Showing home timeline.");

        for (Status status : statuses) {

          // System.out.println(status.getUser().getName() + ":" +status.getText());
          // Addes timeline to an array
          String mytweets = status.getUser().getName() + ":" + status.getText();
          mylatesttweetslist.add(mytweets);
        }
        ;

      } catch (TwitterException te) {
        te.printStackTrace();
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit(-1);
      }

      // MongoDB Insert Below //
      //////////////////////////

      // System Date
      Date sd = new Date();
      String sysdate = sd.toString();

      // Toggle the below to display and insert the transactions as required
      boolean showtrans = true;
      boolean inserttrans = true;

      // checkArray - loads args to a text string to allow the contains function
      String checkString = "";
      for (int ck = 0; ck < args.length; ck++) {
        checkString = checkString + args[ck];
      }
      ;

      // display transactions flag on runnning jsimongo eg: java jsimongo -d  will NOT display
      // transactions
      // insert transactions flag on runnning jsimongo eg: java jsimongo -i  will NOT insert
      // transactions

      if (args.length > 0) {
        if (checkString.contains("-d")) showtrans = false;
        if (checkString.contains("-i")) inserttrans = false;
      }
      ;

      int x = 0;
      for (String atweet : mylatesttweetslist) {
        x++;
        // Display tweets to console
        if (showtrans == true) {
          System.out.println("tweet : " + atweet);
          System.out.println("Created_DateTime : " + sysdate); // was sysdate
        }
        ;

        // Insert JSON into MongoDB
        if (inserttrans == true) {
          BasicDBObject b = new BasicDBObject();
          System.out.println("tweet : " + atweet);
          System.out.println("Created_DateTime : " + sysdate); // was sysdate

          // Insert the JSON object into the chosen collection
          collection.insert(b);
        }
        ;
        Thread.sleep(1);
      }
      ;

      System.out.println("End, the number of tweets inserted at this time was: " + x);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }