예제 #1
0
  @Override
  public boolean execute() {
    Twitter main = new Twitter();
    logger.info(
        "WARNING!!!!!!  Once authorized, this will delete EVERY tweet you ever posted, that is older then 2 weeks.\n"
            + "Do not continue unless you are absolutely certain you want to do this.");
    logger.info(
        "\n\nNote: Twitter rate limits us to 350 actions per hour.  If your twitter stream is very active, this will "
            + "most likely break and you'll need to re-run this application a few times over to do a proper cleanup.");
    Scanner scanner = new Scanner(System.in);
    logger.info("Do you wish to continue? (y/n)?");
    String ans = scanner.nextLine();

    if (ans.toLowerCase().charAt(0) != 'y') {
      logger.info("Exciting application....");
      System.exit(0);
    }

    AccessToken token = main.requestToken();
    main.twitter.setOAuthAccessToken(token);

    Configuration globalConfig = Configuration.getInstance();
    TwitterConfig config = globalConfig.getTwitterConfig("twitter");

    if (config.isCleanDirectMessages()) main.deleteDirectMessages();
    if (config.isCleanLists()) main.removeLists();
    if (config.isCleanTweets()) main.deleteTweets();
    if (config.isCleanFavorites()) main.removeFavorites();
    if (config.isCleanReTweets()) main.removeRetweets();
    if (config.isCleanFriendships()) main.removeFriendships();

    return true;
  }
예제 #2
0
 // Constructor
 public Twitter() {
   config = Configuration.getInstance();
   twitterConfig = config.getTwitterConfig("twitter"); // HACK
   token = twitterConfig.getToken();
   twitter.setOAuthConsumer(
       twitterConfig.getToken().getToken(), twitterConfig.getToken().getTokenSecret());
   earlier = new Date(System.currentTimeMillis() - config.getDelta());
 }
 public static void main(String[] args) {
   JCommanderTwitterStream jcts = new JCommanderTwitterStream();
   new JCommander(jcts, args);
   if (jcts.isHelp()) {
     JCommander jc = new JCommander(jcts, args);
     jc.usage();
     return;
   }
   String stringKeywords = jcts.getKeywords().stream().collect(joining(" "));
   if (jcts.isStream()) {
     try {
       if (jcts.getLimit() < Integer.MAX_VALUE) {
         throw new ParameterException("You can't have stream with limit.");
       }
       streamTweets(stringKeywords, jcts.getLocation(), jcts.isHideRetweets());
     } catch (URLs.ConnectionException
         | URLs.HTTPQueryException
         | MalformedURLException
         | ParameterException
         | GeolocationSearch.NoKeyException
         | GeolocationSearch.SearchLocationException e) {
       System.err.println(e.getMessage());
       System.exit(1);
     }
   } else {
     try {
       printTweets(stringKeywords, jcts.getLocation(), jcts.getLimit(), jcts.isHideRetweets());
     } catch (TwitterException
         | URLs.ConnectionException
         | GeolocationSearch.SearchLocationException
         | MalformedURLException
         | GeolocationSearch.NoKeyException
         | URLs.HTTPQueryException e) {
       System.err.println(e.getMessage());
       System.exit(1);
     }
   }
 }
예제 #4
0
 /**
  * Usage: java twitter4j.examples.trends.GetAvailableTrends [latitude longitude]
  *
  * @param args message
  */
 public static void main(String[] args) {
   try {
     Twitter twitter = new TwitterFactory().getInstance();
     ResponseList<Location> locations;
     if (args.length >= 2) {
       locations =
           twitter.getAvailableTrends(
               new GeoLocation(Double.parseDouble(args[0]), Double.parseDouble(args[1])));
     } else {
       locations = twitter.getAvailableTrends();
     }
     System.out.println("Showing available trends");
     for (Location location : locations) {
       System.out.println(location.getName() + " (woeid:" + location.getWoeid() + ")");
     }
     System.out.println("done.");
     System.exit(0);
   } catch (TwitterException te) {
     te.printStackTrace();
     System.out.println("Failed to get trends: " + te.getMessage());
     System.exit(-1);
   }
 }
예제 #5
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();
    }
  }
예제 #6
0
  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;
  }
  private static JSONObject validateJSONObjectSchema(JSONObject json, String[] knownNames)
      throws JSONException {
    boolean debug = false;
    Map<String, String[]> schemaMap = new HashMap<String, String[]>();
    List<String> names = new ArrayList<String>();
    if (debug) {
      System.out.println("validating:" + json);
    }
    for (int i = 0; i < knownNames.length; i++) {
      if (debug) {
        System.out.println("knownName[" + i + "]:" + knownNames[i]);
      }
      String knownName = knownNames[i];
      int index;
      if (-1 != (index = knownName.indexOf("/"))) {
        String parent = knownName.substring(0, index);
        String child = knownName.substring(index + 1);
        String[] array = schemaMap.get(parent);
        if (null == array) {
          schemaMap.put(parent, new String[] {child});
        } else {
          String[] newArray = new String[array.length + 1];
          System.arraycopy(array, 0, newArray, 0, array.length);
          newArray[newArray.length - 1] = child;
          schemaMap.put(parent, newArray);
        }
        names.add(parent);
      } else {
        names.add(knownName);
      }
    }

    Iterator ite = json.keys();
    while (ite.hasNext()) {
      String name = (String) ite.next();
      boolean found = false;
      if (debug) {
        System.out.println("name:" + name);
      }
      for (String elementName : names) {
        if (debug) {
          System.out.println("elementname:" + elementName);
        }
        Object obj = json.get(name);
        if (obj instanceof JSONObject || obj instanceof JSONArray) {
          String[] children = schemaMap.get(name);
          if (null == children) {
            Assert.fail(
                elementName + ":" + name + " is not supposed to have any child but has:" + obj);
          } else if (!children[0].equals("*")) {
            validateJSONSchema(obj, children);
          }
        }
        if (elementName.equals(name)) {
          found = true;
          break;
        }
      }
      if (!found) {
        Assert.fail("unknown element:[" + name + "] in " + json);
      }
    }
    return json;
  }
  public void testSchema() throws Exception {
    String[] schema;
    String url;

    //        JSONObject json = new
    // JSONObject("{\"a\":\"avalue\",\"b\":\"bvalue\",\"c\":{\"c-1\":12,\"c-2\":\"c-2value\"}}");
    //        schema = new String[]{"a", "b", "c/c-1", "c/c-2"};
    //        validateJSONObjectSchema(json, schema);
    //        try {
    //            schema = new String[]{"a", "b", "c/c-1"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c/c-2 is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        try {
    //            schema = new String[]{"a", "b"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        try {
    //            schema = new String[]{"a", "b","c"};
    //            validateJSONObjectSchema(json, schema);
    //            fail("c/* is missing. expecting an AssertionFailedError.");
    //        } catch (AssertionFailedError ignore) {
    ////            ignore.printStackTrace();
    //        }
    //        schema = new String[]{"a", "b", "c/*"};
    //        validateJSONObjectSchema(json, schema);
    //
    //        JSONArray array = new JSONArray("[{\"slug\":\"art-design\",\"name\":\"Art &
    // Design\"},{\"slug\":\"books\",\"name\":\"Books\"}]");
    //        schema = new String[]{"slug", "name"};
    //        validateJSONArraySchema(array, schema);

    // Location

    //        schema = new
    // String[]{"url","country","woeid","placeType/name","placeType/code","name","countryCode"};
    //        url = "http://api.twitter.com/1.1/trends/available.json";
    //        validateJSONArraySchema(url, schema);
    // Place
    if (!Boolean.valueOf(System.getProperties().getProperty("twitter4j.test.schema"))) {
      // skipping schema validation
      return;
    }

    schema =
        new String[] {
          "slug", "name", "size",
        };
    url = "http://api.twitter.com/1.1/users/suggestions.json";
    List categories =
        CategoryJSONImpl.createCategoriesList(validateJSONArraySchema(url, schema), null, conf);
    Assert.assertEquals(20, categories.size());

    schema = new String[] {"slug", "name", "size", "categories/*", "users/*"};
    url = "http://api.twitter.com/1.1/users/suggestions/art-design.json";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "result/places/name",
          "result/places/street_address",
          "result/places/attributes/*",
          "result/places/country_code",
          "result/places/id",
          "result/places/country",
          "result/places/place_type",
          "result/places/url",
          "result/places/full_name",
          "result/places/bounding_box/*",
          "result/places/contained_within/place_type",
          "result/places/contained_within/attributes/*",
          "result/places/contained_within/street_address",
          "result/places/contained_within/url",
          "result/places/contained_within/bounding_box/type",
          "result/places/contained_within/bounding_box/coordinates/*",
          "result/places/contained_within/full_name",
          "result/places/contained_within/country_code",
          "result/places/contained_within/name",
          "result/places/contained_within/id",
          "result/places/contained_within/country",
          "query",
          "query/type",
          "query/url",
          "query/params",
          "query/params/granularity",
          "query/params/coordinates",
          "query/params/coordinates/type",
          "query/params/coordinates/coordinates",
          "query/params/coordinates/coordinates/*",
          "query/params/accuracy",
        };
    url = "http://api.twitter.com/1.1/geo/reverse_geocode.json?lat=37.78215&long=-122.40060";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "next_cursor",
          "next_cursor_str",
          "previous_cursor",
          "previous_cursor_str",
          "lists/id",
          "lists/id_str",
          "lists/member_count",
          "lists/description",
          "lists/name",
          "lists/subscriber_count",
          "lists/slug",
          "lists/user/*",
          "lists/uri",
          "lists/full_name",
          "lists/mode",
          "lists/following",
        };
    url = "http://api.twitter.com/1.1/twit4j2/lists.json";
    validateJSONObjectSchema(url, schema);

    schema =
        new String[] {
          "id",
          "id_str",
          "member_count",
          "description",
          "name",
          "subscriber_count",
          "slug",
          "user/*",
          "uri",
          "full_name",
          "mode",
          "following",
        };
    url = "http://api.twitter.com/1.1/twit4j2/lists/9499823.json";
    UserList userList = new UserListJSONImpl(validateJSONObjectSchema(url, schema));
    Assert.assertEquals("", userList.getDescription());
    Assert.assertEquals("@twit4j2/test", userList.getFullName());
    Assert.assertEquals(9499823, userList.getId());
    Assert.assertTrue(1 < userList.getMemberCount());
    Assert.assertEquals("test", userList.getName());
    Assert.assertEquals("test", userList.getSlug());
    Assert.assertEquals(0, userList.getSubscriberCount());
    Assert.assertEquals("/twit4j2/test", userList.getURI().toString());
    Assert.assertNotNull(userList.getUser());
    Assert.assertTrue(userList.isPublic());
    Assert.assertFalse(userList.isFollowing());

    schema =
        new String[] {
          "favorited",
          "in_reply_to_status_id",
          "in_reply_to_status_id_str",
          "created_at",
          "geo",
          "place",
          "source",
          "in_reply_to_screen_name",
          "in_reply_to_user_id",
          "in_reply_to_user_id_str",
          "coordinates",
          "truncated",
          "contributors",
          "id",
          "id_str",
          "text",
          "user/*",
          "retweeted",
          "retweet_count"
        };
    url = "http://api.twitter.com/1.1/statuses/show/2245071380.json";
    Status status = new StatusJSONImpl(validateJSONObjectSchema(url, schema));

    schema =
        new String[] {
          "profile_background_image_url",
          "created_at",
          "friends_count",
          "profile_link_color",
          "description",
          "contributors_enabled",
          "status/*",
          "following",
          "profile_background_tile",
          "favourites_count",
          "profile_sidebar_fill_color",
          "url",
          "profile_image_url",
          "geo_enabled",
          "notifications",
          "profile_sidebar_border_color",
          "location",
          "screen_name",
          "verified",
          "time_zone",
          "profile_background_color",
          "profile_use_background_image",
          "protected",
          "name",
          "profile_text_color",
          "followers_count",
          "id",
          "id_str",
          "lang",
          "statuses_count",
          "follow_request_sent",
          "utc_offset",
          "listed_count",
          "is_translator",
          "show_all_inline_media"
        };

    url = "http://api.twitter.com/1.1/users/show/yusukey.json";
    User user = new UserJSONImpl(validateJSONObjectSchema(url, schema));
  }