예제 #1
0
 public Collection<Tweet> requestTweetsByUser(String otherUser) {
   List<Tweet> tweets = new ArrayList<Tweet>();
   try {
     // The factory instance is re-useable and thread safe.
     Twitter twitter = new TwitterFactory().getInstance();
     AccessToken accessToken = loadAccessToken(1);
     twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
     twitter.setOAuthAccessToken(accessToken);
     System.out.println("Fetching latest 100 tweets for [" + otherUser + "]");
     // First param of Paging() is the page number, second is the number per page (this is capped
     // around 200 I think.
     Paging paging = new Paging(1, 100);
     List<Status> statuses = twitter.getUserTimeline(otherUser, paging);
     for (Status status : statuses) {
       tweets.add(
           new Tweet(
               otherUser,
               status.getText(),
               (status.getGeoLocation() != null
                   ? status.getGeoLocation().getLatitude()
                       + ","
                       + status.getGeoLocation().getLongitude()
                   : "")));
       System.out.println(
           status.getUser().getName() + "(" + status.getGeoLocation() + "):" + status.getText());
     }
   } catch (Exception e) {
     e.printStackTrace();
     throw new RuntimeException(e);
   }
   return tweets;
 }
  private void addNewTwitterStatus(Status status) {
    ContentResolver resolver = getContentResolver();

    // Make sure status doesn't alraedy exist
    String where = TwitterStatusContentProvider.KEY_STATUS_ID + " = " + status.getId();
    Cursor query =
        resolver.query(TwitterStatusContentProvider.CONTENT_URI, null, where, null, null);
    if (query.getCount() == 0) {
      ContentValues values = new ContentValues();
      values.put(TwitterStatusContentProvider.KEY_STATUS_ID, status.getId());
      values.put(TwitterStatusContentProvider.KEY_CREATED_AT, status.getCreatedAt().getTime());
      values.put(TwitterStatusContentProvider.KEY_STATUS_TEXT, status.getText());
      values.put(TwitterStatusContentProvider.KEY_USER_ID, status.getUser().getId());
      values.put(TwitterStatusContentProvider.KEY_USER_NAME, status.getUser().getName());
      values.put(
          TwitterStatusContentProvider.KEY_USER_SCREEN_NAME, status.getUser().getScreenName());
      values.put(
          TwitterStatusContentProvider.KEY_USER_IMAGE, status.getUser().getMiniProfileImageURL());
      values.put(TwitterStatusContentProvider.KEY_USER_URL, status.getUser().getURL());
      values.put(TwitterStatusContentProvider.KEY_LATITUDE, status.getGeoLocation().getLatitude());
      values.put(
          TwitterStatusContentProvider.KEY_LONGITUDE, status.getGeoLocation().getLongitude());
      resolver.insert(TwitterStatusContentProvider.CONTENT_URI, values);
    }
    query.close();
  }
예제 #3
0
  public static void insertTweet(Status tweet) throws SQLException {
    try {
      System.out.println(tweet.getCreatedAt() + ": " + tweet.getText());

      if (tweet.getRetweetedStatus() != null) {
        insertRetweet(tweet);
        return;
      }

      PreparedStatement insertTweetStmt = SqlHelper.getInsertTweetStmt();
      insertTweetStmt.setLong(1, tweet.getId());
      if (tweet.getGeoLocation() != null) {
        insertTweetStmt.setFloat(2, (float) tweet.getGeoLocation().getLatitude());
        insertTweetStmt.setFloat(3, (float) tweet.getGeoLocation().getLongitude());
      } else {
        insertTweetStmt.setFloat(2, 0);
        insertTweetStmt.setFloat(3, 0);
      }
      insertTweetStmt.setString(4, tweet.getText());
      insertTweetStmt.setInt(5, (int) tweet.getRetweetCount());
      insertTweetStmt.setTimestamp(6, new Timestamp(tweet.getCreatedAt().getTime()));
      insertTweetStmt.setLong(7, tweet.getInReplyToStatusId());
      insertTweetStmt.setLong(8, tweet.getUser().getId());
      insertTweetStmt.executeUpdate();
    } catch (MySQLIntegrityConstraintViolationException e) {
      // ignore, this is just a duplicate tweet entry, that's rather normal
    }
  }
예제 #4
0
 public Tweet(Status status) {
   this.text = status.getText();
   this.longitude = status.getGeoLocation().getLongitude();
   this.latitude = status.getGeoLocation().getLatitude();
   this.userName = status.getUser().getName();
   this.userDescription = status.getUser().getDescription();
   this.createdAt = status.getCreatedAt().getTime();
   this.tweetId = status.getId();
 }
예제 #5
0
 /**
  * CONSTRUCTOR
  *
  * @param status : the actual tweet present in the portion of the stream received
  * @param mention : one of the multiple mentions present in the status
  */
 public TwitterTuple(Status status, UserMentionEntity mention) {
   this.user = status.getUser().getScreenName();
   this.lang = status.getUser().getLang();
   this.mentions = mention;
   this.mentionedUser = mention.getScreenName();
   if (status.getGeoLocation() == null) {
     this.latitude = -999.0;
     this.longitude = -999.0;
   } else {
     this.latitude = status.getGeoLocation().getLatitude();
     this.longitude = status.getGeoLocation().getLongitude();
   }
 }
예제 #6
0
 /**
  * Transforms a twitter4j.Status object into a JSONObject
  *
  * @param pStatus a twitter4j.Status object
  * @return JSONObject
  */
 @SuppressWarnings("unchecked")
 public static JSONObject getJson(Status pStatus) {
   JSONObject jsonObj = null;
   if (pStatus != null) {
     jsonObj = new JSONObject();
     jsonObj.put("createdAt", pStatus.getCreatedAt());
     jsonObj.put("id", pStatus.getId());
     jsonObj.put("text", pStatus.getText());
     jsonObj.put("source", pStatus.getSource());
     jsonObj.put("isTruncated", pStatus.isTruncated());
     jsonObj.put("inReplyToStatusId", pStatus.getInReplyToStatusId());
     jsonObj.put("inReplyToUserId", pStatus.getInReplyToUserId());
     jsonObj.put("isFavorited", pStatus.isFavorited());
     jsonObj.put("isRetweeted", pStatus.isRetweeted());
     jsonObj.put("favoriteCount", pStatus.getFavoriteCount());
     jsonObj.put("inReplyToScreenName", pStatus.getInReplyToScreenName());
     jsonObj.put("geoLocation", pStatus.getGeoLocation());
     jsonObj.put("place", pStatus.getPlace());
     jsonObj.put("retweetCount", pStatus.getRetweetCount());
     jsonObj.put("isPossiblySensitive", pStatus.isPossiblySensitive());
     jsonObj.put("isoLanguageCode", pStatus.getIsoLanguageCode());
     jsonObj.put("contributorsIDs", pStatus.getContributors());
     jsonObj.put("retweetedStatus", pStatus.getRetweetedStatus());
     jsonObj.put("userMentionEntities", pStatus.getUserMentionEntities());
     jsonObj.put("urlEntities", pStatus.getURLEntities());
     jsonObj.put("hashtagEntities", pStatus.getHashtagEntities());
     jsonObj.put("mediaEntities", pStatus.getMediaEntities());
     jsonObj.put("currentUserRetweetId", pStatus.getCurrentUserRetweetId());
     jsonObj.put("user", pStatus.getUser());
   }
   return jsonObj;
 }
예제 #7
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;
  }
예제 #8
0
  private void getTweetsFromTwitter() {
    try {
      ConfigurationBuilder confbuilder = new ConfigurationBuilder();
      confbuilder.setOAuthConsumerKey(TWITTER_CONSUMER_KEY);
      confbuilder.setOAuthConsumerSecret(TWITTER_CONSUMER_SECRET);

      // Access Token
      String access_token = mSharedPreferences.getString(PREF_KEY_OAUTH_TOKEN, "");
      // Access Token Secret
      String access_token_secret = mSharedPreferences.getString(PREF_KEY_OAUTH_SECRET, "");

      AccessToken accessToken = new AccessToken(access_token, access_token_secret);

      btnLogoutTwitter.setVisibility(View.GONE);
      btnGetTweets.setVisibility(View.GONE);
      lblUserName.setVisibility(View.GONE);
      btnShowOnMap.setVisibility(View.GONE);
      btnUsers.setVisibility(View.GONE);

      Twitter twitter = new TwitterFactory(confbuilder.build()).getInstance(accessToken);
      List<Status> statuses = twitter.getUserTimeline(USER_ID, new Paging(1, 10));

      IDs following = twitter.getFriendsIDs(USER_ID);

      long[] followingList = following.getIDs();

      for (int i = 0; i < followingList.length; i++) {

        Toast.makeText(getApplicationContext(), twitter.showUser(followingList[i]).toString(), 100)
            .show();
      }

      int count = 0;
      // Collections.reverse(statuses); // reverse order
      ArrayList<String> arrayList = new ArrayList<String>();
      for (Status status : statuses) {
        String tweettext =
            status.getUser().getName()
                + ":"
                + status.getText()
                + "\n ("
                + status.getCreatedAt().toLocaleString()
                + ")"
                + status.getGeoLocation();
        arrayList.add(tweettext);

        // now single tweet is in tweettext
      }

      ListView lv = (ListView) findViewById(R.id.storeTweet);
      ArrayAdapter<String> arrayAdapter =
          new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);

      lv.setAdapter(arrayAdapter);
    } catch (Exception ex) {
      // failed to fetch tweet
    }
  }
예제 #9
0
  @Override
  public void storeData(Status status) throws InterruptedException {

    BasicDBObject basicObj = new BasicDBObject();
    // if(status.getGeoLocation().equals(true))

    // if(status.getGeoLocation().getLatitude()!=0.0d)
    // {

    if (status.getGeoLocation() != null) {
      try {
        System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText());
        basicObj.put("user_name", status.getUser().getScreenName());
        basicObj.put("retweet_count", status.getRetweetCount());
        // basicObj.put("tweet_followers_count",
        // status.getUser().getFollowersCount());
        basicObj.put("source", status.getSource());
        // basicObj.put("coordinates",status.getGeoLocation());
        basicObj.put("latitude", status.getGeoLocation().getLatitude());
        basicObj.put("longitude", status.getGeoLocation().getLongitude());
        // UserMentionEntity[] mentioned =
        // status.getUserMentionEntities();
        // basicObj.put("tweet_mentioned_count", mentioned.length);
        basicObj.put("tweet_ID", status.getId());
        basicObj.put("tweet_text", status.getText());
        // i++;
      } catch (Exception e) {
        System.out.println(e);
      }

      try {
        items.insert(basicObj);
      } catch (Exception e) {
        System.out.println("MongoDB Connection Error : " + e.getMessage());
      }
    }
    // else
    // throw new InterruptedException();

  }
  protected static ListItem toListItem(Status tweet) {
    ListItem item1 = new ListItem();

    item1.image = defaultImage;
    item1.id = tweet.getId();

    item1.screenName = tweet.getUser().getScreenName();
    item1.name = tweet.getUser().getName();
    item1.comment = tweet.getText();

    item1.geoLocation = tweet.getGeoLocation();
    item1.profileImageURL = tweet.getUser().getProfileImageURL().toString();
    item1.date = diffDate(tweet.getCreatedAt());

    //
    item1.source = tweet.getSource();

    return item1;
  }
예제 #11
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);
 }
예제 #12
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);
 }
예제 #13
0
  /*
   * (non-Javadoc)
   *
   * @see edu.umd.cs.dmonner.tweater.StatusEater#persist(java.util.List, twitter4j.Status)
   */
  public void persist(final List<QueryItem> matches, final Status status) {
    final User user = status.getUser();

    // get location information
    final GeoLocation loc = status.getGeoLocation();
    final double lat = loc == null ? 0D : loc.getLatitude();
    final double lon = loc == null ? 0D : loc.getLongitude();
    final String locStr = user.getLocation() == null ? "" : scrub(user.getLocation());
    final String lang = user.getLang() == null ? "" : scrub(user.getLang());

    // get retweet information
    final boolean rt = status.isRetweet();
    final long rtct = rt ? status.getRetweetCount() : 0;
    final long rtid = rt ? status.getRetweetedStatus().getId() : -1;

    synchronized (outfile) {
      this.outfile.println(
          user.getId()
              + ",\""
              + //
              user.getScreenName()
              + "\",\""
              + //
              locStr
              + "\","
              + //
              user.getFollowersCount()
              + ","
              + //
              user.getFriendsCount()
              + ","
              + //
              status.getId()
              + ","
              + //
              status.getCreatedAt()
              + ",\""
              + //
              scrub(status.getText())
              + "\","
              + //
              rt
              + ","
              + //
              rtid
              + ","
              + //
              rtct
              + ","
              + //
              lat
              + ","
              + //
              lon
              + ","
              + //
              user.getCreatedAt()
              + ","
              + //
              user.getStatusesCount()
              + ","
              + //
              user.getListedCount()
              + ","
              + //
              user.isVerified()
              + ",\""
              + //
              lang
              + "\","
              + //
              user.getUtcOffset() / 3600
              + ",\""
              + //
              matches.get(0).toString()
              + "\"");
      this.outfile.flush();
    }
  }
예제 #14
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();
  }
예제 #15
0
  public JSONTweet readLine() {
    String line;
    try {
      line = br.readLine();
    } catch (IOException e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
      return null;
    }
    if (line == null) return null;

    Status tweet = null;
    try {
      // parse json to object type
      tweet = DataObjectFactory.createStatus(line);
    } catch (TwitterException e) {
      System.err.println("error parsing tweet object");
      return null;
    }

    jsontweet.JSON = line;
    jsontweet.id = tweet.getId();
    jsontweet.source = tweet.getSource();
    jsontweet.text = tweet.getText();
    jsontweet.createdat = tweet.getCreatedAt();
    jsontweet.tweetgeolocation = tweet.getGeoLocation();

    User user;
    if ((user = tweet.getUser()) != null) {

      jsontweet.userdescription = user.getDescription();
      jsontweet.userid = user.getId();
      jsontweet.userlanguage = user.getLang();
      jsontweet.userlocation = user.getLocation();
      jsontweet.username = user.getName();
      jsontweet.usertimezone = user.getTimeZone();
      jsontweet.usergeoenabled = user.isGeoEnabled();

      if (user.getURL() != null) {
        String url = user.getURL().toString();

        jsontweet.userurl = url;

        String addr = url.substring(7).split("/")[0];
        String[] countrysuffix = addr.split("[.]");
        String suffix = countrysuffix[countrysuffix.length - 1];

        jsontweet.userurlsuffix = suffix;

        try {
          InetAddress address = null; // InetAddress.getByName(user.getURL().getHost());

          String generate_URL
              // =
              // "http://www.geobytes.com/IpLocator.htm?GetLocation&template=php3.txt&IpAddress="
              = "http://www.geoplugin.net/php.gp?ip=" + address.getHostAddress();
          URL data = new URL(generate_URL);
          URLConnection yc = data.openConnection();
          BufferedReader in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
          String inputLine;
          String temp = "";
          while ((inputLine = in.readLine()) != null) {
            temp += inputLine + "\n";
          }
          temp = temp.split("s:2:\"")[1].split("\"")[0];

          jsontweet.userurllocation = temp;

        } catch (Exception uhe) {
          // uhe.printStackTrace();
          jsontweet.userurllocation = null;
        }
      }
    }

    return jsontweet;
  }
 @Test
 public void testGetTweetWithGeolocation() throws TwitterException {
   Status st = twitterSearch.getTweet(18845491030L);
   assertNotNull(st.getGeoLocation());
   //        System.out.println("geo:" + st.getGeoLocation());
 }