コード例 #1
0
 public static void main(String[] args) throws TwitterException, IOException {
   Twitter twitter = TwitterFactory.getSingleton();
   JLanguageTool langTool = new JLanguageTool(new AmericanEnglish());
   List<String> twts = new ArrayList<String>();
   for (String arg : args) {
     Query query = new Query(arg);
     QueryResult result;
     int counter = 0;
     do {
       result = twitter.search(query);
       List<Status> tweets = result.getTweets();
       for (Status tweet : tweets) {
         if (isEligible(tweet)) {
           System.out.println("@" + tweet.getUser().getScreenName() + " - " + tweet.getText());
           System.out.println(tweet.getLang());
           twts.add(tweet.getText());
           counter++;
         }
       }
     } while ((query = result.nextQuery()) != null && counter < 5);
   }
   for (String str : twts) {
     List<RuleMatch> matches = langTool.check(str);
     for (RuleMatch match : matches) {
       System.out.println(
           "Potential error at line "
               + match.getLine()
               + ", column "
               + match.getColumn()
               + ": "
               + match.getMessage());
       System.out.println("Suggested correction: " + match.getSuggestedReplacements());
     }
   }
 }
コード例 #2
0
  @Test
  public void testLinkTruncation() throws Exception {
    Twitter tt = TwitterTest.newTestTwitter();
    Status s = tt.getStatus(new BigInteger("154915377170747392"));
    List<TweetEntity> urls = s.getTweetEntities(KEntityType.urls);
    System.out.println(urls);
    System.out.println(s);
    assert !s.getText().contains("http://t.co ...");
    //				"RT @pozorvlak: Delighted to see Alan Bundy (@winterstein's PhD supervisor, IIRC) in the
    // New Year's Honour's list: http://soda.sh/xbE");

    BigInteger id = new BigInteger("154915015965683712");
    tt.setIncludeTweetEntities(true);
    Status s2 = tt.getStatus(id);
    System.out.println(s2);
    System.out.println(s2.getDisplayText());
    List<TweetEntity> urls2 = s2.getTweetEntities(KEntityType.urls);
    TweetEntity te = urls2 == null ? null : urls2.get(0); // this tweet-entity sucks too :(
    System.out.println(urls2);
    assert !s2.getText().contains("http://t.co ...");

    List<Status> joes = tt.getUserTimeline("joehalliwell");
    for (Status status : joes) {
      System.out.println(status);
    }
  }
コード例 #3
0
 public static void printTweet(Status tweet, boolean isStream) {
   String time;
   if (isStream) {
     time = "";
     try {
       TimeUnit.SECONDS.sleep(1);
     } catch (InterruptedException ie) {
       ie.printStackTrace();
     }
   } else {
     time = getTimeForm(tweet) + " ";
   }
   System.out.println(SEPARATOR);
   String uName = tweet.getUser().getScreenName();
   String text = tweet.getText();
   Integer retweets = tweet.getRetweetCount();
   if (tweet.isRetweet()) {
     Pattern myPattern = Pattern.compile("RT @([^ ]*): (.*)");
     Matcher m = myPattern.matcher(text);
     m.find();
     String uRTName = m.group(1);
     text = m.group(2);
     System.out.println(time + "@" + uName + ": ретвитнул @" + uRTName + ": " + text);
   } else {
     System.out.println(time + "@" + uName + ": " + text + retweetsForm(retweets));
   }
 }
コード例 #4
0
  private Tweet getTweetObjectFromStatus(Status status) {

    Tweet tweet = new Tweet();
    tweet.setId(Long.toString(status.getId()));
    tweet.setText(status.getText());
    tweet.setCreatedAt(status.getCreatedAt());

    tweet.setFavCount(status.getFavoriteCount());

    User user = status.getUser();

    tweet.setUserId(user.getId());
    tweet.setUserName(user.getName());
    tweet.setUserScreenName(user.getScreenName());

    HashtagEntity[] hashtagEntities = status.getHashtagEntities();
    List<String> hashtags = new ArrayList<String>();

    for (HashtagEntity hashtagEntity : hashtagEntities) {
      hashtags.add(hashtagEntity.getText());
    }

    tweet.setHashTags(hashtags.toArray(new String[hashtags.size()]));

    GeoLocation geoLocation = status.getGeoLocation();
    if (geoLocation != null) {
      double[] coordinates = {geoLocation.getLongitude(), geoLocation.getLatitude()};
      tweet.setCoordinates(coordinates);
    }

    return tweet;
  }
コード例 #5
0
ファイル: Control.java プロジェクト: maha-team/CelebWatch
  @Override /* method called when new tweet is read*/

  /*Here or in a special method in this class the logic for saving displaying result is called*/
  public void onStatus(Status arg0) {
    String tweet = arg0.getText();
    String userName = arg0.getUser().getName();
    if (checkTweet(tweet)) {
      _mainPageContainer.ShowTweet(userName + "" + tweet);
      /*counts the number of tweets for the chosen celeb and displays them*/
      tweetCount++;
      _mainPageContainer.count.setText(Integer.toString(tweetCount) + " number of tweets");
    }
  }
コード例 #6
0
  /**
   * Does the passed status match the filter pattern? This method checks the status text and the
   * expanded url entities
   *
   * @param status Status to check
   * @return True if the filter expression matches, false otherwise
   */
  private boolean matchesFilter(Status status) {
    boolean shouldFilter = false;
    if (filterPattern != null) {
      Matcher m = filterPattern.matcher(status.getText());
      if (m.matches()) shouldFilter = true;

      if (status.getURLEntities() != null) {
        for (URLEntity ue : status.getURLEntities()) {
          URL expUrl = ue.getExpandedURL();
          if (expUrl != null && expUrl.toString() != null) {
            m = filterPattern.matcher(expUrl.toString());
            if (m.matches()) shouldFilter = true;
          }
        }
      }
    }
    return shouldFilter;
  }
コード例 #7
0
        /**
         * Called when the user's timeline has arrived
         *
         * @param statuses The user's latest statuses
         */
        @Override
        public void gotUserTimeline(ResponseList<Status> statuses) {
          SoomlaUtils.LogDebug(TAG, "getFeed/onComplete");

          List<String> feeds = new ArrayList<String>();
          for (Status post : statuses) {
            feeds.add(post.getText());
          }

          boolean hasMore;
          if (feeds.size() >= PAGE_SIZE) {
            lastFeedCursor++;
            hasMore = true;
          } else {
            lastFeedCursor = 1;
            hasMore = false;
          }
          RefFeedListener.success(feeds, hasMore);
          clearListener(ACTION_GET_FEED);
        }
コード例 #8
0
 public void testRetweetStatusAsJSON() throws Exception {
   // single Status
   HttpClientImpl http = new HttpClientImpl();
   Status status =
       new StatusJSONImpl(
           http.get("http://twitter4j.org/en/testcases/statuses/retweet/6010814202.json"), conf);
   Assert.assertEquals(new Date(1259078050000l), status.getCreatedAt());
   Assert.assertEquals(6011259778l, status.getId());
   Assert.assertEquals(null, status.getInReplyToScreenName());
   Assert.assertEquals(-1l, status.getInReplyToStatusId());
   Assert.assertEquals(-1, status.getInReplyToUserId());
   Assert.assertNull(status.getGeoLocation());
   Assert.assertEquals(
       "<a href=\"http://apiwiki.twitter.com/\" rel=\"nofollow\">API</a>", status.getSource());
   Assert.assertEquals(
       "RT @yusukey: この前取材受けた奴 -> 次世代のシステム環境を見据えたアプリケーションサーバー製品の選択 ITpro: http://special.nikkeibp.co.jp/ts/article/0iaa/104388/",
       status.getText());
   Assert.assertEquals(6358482, status.getUser().getId());
   Assert.assertTrue(status.isRetweet());
   assertDeserializedFormIsEqual(status);
 }
コード例 #9
0
 public void testStatusAsJSON() throws Exception {
   // single Status
   HttpClientImpl http = new HttpClientImpl();
   List<Status> statuses =
       StatusJSONImpl.createStatusList(
           http.get("http://twitter4j.org/en/testcases/statuses/public_timeline.json"), conf);
   Status status = statuses.get(0);
   Assert.assertEquals(new Date(1259041785000l), status.getCreatedAt());
   Assert.assertEquals(6000554383l, status.getId());
   Assert.assertEquals("G_Shock22", status.getInReplyToScreenName());
   Assert.assertEquals(6000444309l, status.getInReplyToStatusId());
   Assert.assertEquals(20159829, status.getInReplyToUserId());
   Assert.assertNull(status.getGeoLocation());
   Assert.assertEquals("web", status.getSource());
   Assert.assertEquals(
       "@G_Shock22 I smelled a roast session coming when yu said that shyt about @2koolNicia lol....",
       status.getText());
   Assert.assertEquals(23459577, status.getUser().getId());
   Assert.assertFalse(status.isRetweet());
   assertDeserializedFormIsEqual(statuses);
 }
コード例 #10
0
 private scholl.both.analyzer.social.Post getSocial(Status s) {
   return new scholl.both.analyzer.social.Post(
       s.getText(), s.getCreatedAt().getTime(), new TwitterUser(s.getUser()));
 }
コード例 #11
0
  public static Record buildTweet(Schema schema, Status status) {
    GenericRecordBuilder builderTweet = new GenericRecordBuilder(schema);

    builderTweet.set("created_at", status.getCreatedAt().getTime());
    builderTweet.set("favorite_count", status.getFavoriteCount());
    builderTweet.set("favorited", status.isFavorited());
    builderTweet.set("id", status.getId());
    builderTweet.set("in_reply_to_screen_name", status.getInReplyToScreenName());
    if (status.getInReplyToStatusId() != -1)
      builderTweet.set("in_reply_to_status_id", status.getInReplyToStatusId());
    if (status.getInReplyToUserId() != -1)
      builderTweet.set("in_reply_to_user_id", status.getInReplyToUserId());
    builderTweet.set("lang", status.getLang());
    builderTweet.set("possibly_sensitive", status.isPossiblySensitive());
    builderTweet.set("retweet_count", status.getRetweetCount());
    builderTweet.set("retweeted", status.isRetweeted());
    builderTweet.set("source", status.getSource());
    builderTweet.set("text", status.getText());
    builderTweet.set("truncated", status.isTruncated());
    if (status.getWithheldInCountries() != null)
      builderTweet.set("withheld_in_countries", Arrays.asList(status.getWithheldInCountries()));
    if (status.getGeoLocation() != null)
      builderTweet.set(
          "coordinates",
          Arrays.asList(
              status.getGeoLocation().getLatitude(), status.getGeoLocation().getLongitude()));

    builderTweet.set("entities", buildEntities(schema.getField("entities").schema(), status));

    if (status.getPlace() != null)
      builderTweet.set(
          "place",
          buildPlace(schema.getField("place").schema().getTypes().get(1), status.getPlace()));

    User user = status.getUser();
    if (user != null && schema.getField("user") != null) {
      Schema schemaUser = schema.getField("user").schema();
      GenericRecordBuilder builderUser = new GenericRecordBuilder(schemaUser);
      builderUser.set("contributors_enabled", user.isContributorsEnabled());
      builderUser.set("created_at", user.getCreatedAt().getTime());
      builderUser.set("default_profile", user.isDefaultProfile());
      builderUser.set("default_profile_image", user.isDefaultProfileImage());
      builderUser.set("description", user.getDescription());
      builderUser.set(
          "entities",
          buildURLEntity(schemaUser.getField("entities").schema(), user.getURLEntity()));
      builderUser.set("favourites_count", user.getFavouritesCount());
      builderUser.set("followers_count", user.getFollowersCount());
      builderUser.set("friends_count", user.getFriendsCount());
      builderUser.set("geo_enabled", user.isGeoEnabled());
      builderUser.set("id", user.getId());
      builderUser.set("is_translator", user.isTranslator());
      builderUser.set("lang", user.getLang());
      builderUser.set("listed_count", user.getListedCount());
      builderUser.set("location", user.getLocation());
      builderUser.set("name", user.getName());
      builderUser.set("screen_name", user.getScreenName());
      builderUser.set("profile_background_color", user.getProfileBackgroundColor());
      builderUser.set("profile_background_image_url", user.getProfileBackgroundImageURL());
      builderUser.set(
          "profile_background_image_url_https", user.getProfileBackgroundImageUrlHttps());
      builderUser.set("profile_background_tile", user.isProfileBackgroundTiled());
      builderUser.set("profile_banner_url", user.getProfileBannerURL());
      builderUser.set("profile_image_url", user.getProfileImageURL());
      builderUser.set("profile_image_url_https", user.getProfileBackgroundImageUrlHttps());
      builderUser.set("profile_link_color", user.getProfileLinkColor());
      builderUser.set("profile_sidebar_border_color", user.getProfileSidebarBorderColor());
      builderUser.set("profile_sidebar_fill_color", user.getProfileSidebarFillColor());
      builderUser.set("profile_text_color", user.getProfileTextColor());
      builderUser.set("profile_use_background_image", user.isProfileUseBackgroundImage());
      builderUser.set("protected", user.isProtected());
      builderUser.set("show_all_inline_media", user.isShowAllInlineMedia());
      builderUser.set("statuses_count", user.getStatusesCount());
      builderUser.set("time_zone", user.getTimeZone());
      builderUser.set("url", user.getURL());
      builderUser.set("utc_offset", user.getUtcOffset());
      builderUser.set("verified", user.isVerified());
      if (user.getStatus() != null && schemaUser.getField("status") != null)
        builderUser.set(
            "status",
            buildTweet(schemaUser.getField("status").schema().getTypes().get(1), user.getStatus()));
      if (user.getWithheldInCountries() != null)
        builderUser.set("withheld_in_countries", Arrays.asList(user.getWithheldInCountries()));
      builderTweet.set("user", builderUser.build());
    }

    if (status.getQuotedStatus() != null && schema.getField("quoted_status") != null)
      builderTweet.set(
          "quoted_status",
          buildTweet(
              schema.getField("quoted_status").schema().getTypes().get(1),
              status.getQuotedStatus()));

    if (status.getRetweetedStatus() != null && schema.getField("retweeted_status") != null)
      builderTweet.set(
          "retweeted_status",
          buildTweet(
              schema.getField("retweeted_status").schema().getTypes().get(1),
              status.getRetweetedStatus()));

    return builderTweet.build();
  }
コード例 #12
0
 @Override
 public void destroyedStatus(Status destroyedStatus) {
   AIR.log("Success destroying status '" + destroyedStatus.getText() + "'");
   StatusUtils.dispatchStatus(destroyedStatus, mCallbackID);
 }
コード例 #13
0
ファイル: meet.java プロジェクト: philnorthmount/jsimongo
  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();
    }
  }
コード例 #14
0
  /**
   * Retrieve a list of statuses. Depending on listId, this is taken from different sources:
   *
   * <ul>
   *   <li>0 : home timeline
   *   <li>-1 : mentions
   *   <li>>0 : User list
   * </ul>
   *
   * This method may trigger a network call if fromDbOnly is false. The filter if not null is a
   * regular expression, that if matches filters the tweet.
   *
   * @param fromDbOnly If true only statuses already in the DB are returned
   * @param listId Id of the list / timeline to fetch (see above)
   * @param updateStatusList Should the currently displayed list be updated?
   * @return List of status items along with some counts
   */
  private MetaList<Status> getTimlinesFromTwitter(
      boolean fromDbOnly, int listId, boolean updateStatusList) {
    Paging paging = new Paging();

    MetaList<Status> myStatuses;

    long last = tdb.getLastRead(account.getId(), listId);
    if (last > 0) // && !Debug.isDebuggerConnected())
    paging.sinceId(last).setCount(200);
    else paging.setCount(50); // 50 Tweets if we don't have the timeline yet

    switch (listId) {
      case 0:
        // Home time line
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);

        break;
      case -1:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      case -2:
        // see below at getDirectsFromTwitter
        myStatuses = new MetaList<Status>();
        break;
      case -3:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      case -4:
        myStatuses = th.getTimeline(paging, listId, fromDbOnly);
        break;
      default:
        myStatuses = th.getUserList(paging, listId, fromDbOnly, unreadCount);
        if (unreadCount > -1) {
          List<Status> list = myStatuses.getList();
          if (list.size() <= unreadCount) unreadCount = list.size() - 1;
          if (unreadCount > -1) last = list.get(unreadCount).getId();
        }

        break;
    }

    long newLast = -1;
    // Update the 'since' id in the database
    if (myStatuses.getList().size() > 0) {
      newLast =
          myStatuses
              .getList()
              .get(0)
              .getId(); // assumption is that twitter sends the newest (=highest id) first
      tdb.updateOrInsertLastRead(account.getId(), listId, newLast);
    }

    // Sync with TweetMarker
    long newLast2 = -1;
    if (listId >= 0 && !account.isStatusNet() && !fromDbOnly) {
      if (listId == 0)
        newLast2 = TweetMarkerSync.syncFromTweetMarker("timeline", account.getName());
      else newLast2 = TweetMarkerSync.syncFromTweetMarker("lists." + listId, account.getName());

      if (newLast2 > newLast) {
        tdb.updateOrInsertLastRead(account.getId(), listId, newLast2);
      } else {
        if (listId == 0)
          TweetMarkerSync.syncToTweetMarker("timeline", newLast, account.getName(), th.getOAuth());
        else
          TweetMarkerSync.syncToTweetMarker(
              "lists." + listId, newLast, account.getName(), th.getOAuth());
      }
    }

    MetaList<Status> metaList;
    if (updateStatusList) {
      statuses = new ArrayList<Status>();
      List<Status> data = new ArrayList<Status>(myStatuses.getList().size());
      if (filterPattern == null) {
        setupFilter(); // TODO report errors?
      }
      for (Status status : myStatuses.getList()) {
        boolean shouldFilter = matchesFilter(status);
        if (shouldFilter) {
          Log.i(
              "TweetListActivity::filter, filtered ",
              status.getUser().getScreenName() + " - " + status.getText());
        } else {
          data.add(status);
          statuses.add(status);
        }
      }
      metaList = new MetaList<Status>(data, myStatuses.getNumOriginal(), myStatuses.getNumAdded());
    } else {
      metaList = new MetaList<Status>(new ArrayList<Status>(), 0, 0);
    }

    if (newLast2 > last) {
      metaList.oldLast = newLast2;
      // the read status from remote is newer than the last read locally, so lets mark those in
      // between as read
      for (Status s : statuses) {
        long id = s.getId();
        if (id > last) {
          th.markStatusAsOld(id);
        }
      }
    } else {
      metaList.oldLast = last;
    }

    for (Status status : metaList.getList()) {
      AccountHolder accountHolder = AccountHolder.getInstance();
      accountHolder.addUserName(status.getUser().getScreenName());
      if (status.getHashtagEntities() != null) {
        for (HashtagEntity hte : status.getHashtagEntities()) {
          accountHolder.addHashTag(hte.getText());
        }
      }
    }

    return metaList;
  }