public static void printTweets( String keywords, String location, Integer limit, Boolean hideRetweets) throws TwitterException, URLs.ConnectionException, GeolocationSearch.SearchLocationException, MalformedURLException, GeolocationSearch.NoKeyException, URLs.HTTPQueryException { Twitter twitter = new TwitterFactory().getInstance(); Query query = new Query(keywords); query.setGeoCode(getLocation(location), RADIUS, Query.KILOMETERS); Integer needTweets; Integer numberTweets = 0; if (limit == Integer.MAX_VALUE) { needTweets = NUMBER_TWEETS; } else { needTweets = limit; } QueryResult result; List<Status> tweets; do { result = twitter.search(query); tweets = result.getTweets(); for (Status tweet : tweets) { if (!(hideRetweets && tweet.isRetweet()) && (numberTweets < needTweets)) { printTweet(tweet, false); numberTweets++; } } query = result.nextQuery(); } while ((numberTweets < needTweets) && (query != null)); if (numberTweets == 0) { System.out.println("Твитов по заданному запросу не найдено."); } else { System.out.println(SEPARATOR); } }
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)); } }
/** Posta in Twitt */ public boolean postTwitt(String sMessage) { try { if (twitter != null) { try { AccessToken a = new AccessToken( mSharedPreferences.getString(Const.PREF_KEY_TOKEN, ""), mSharedPreferences.getString(Const.PREF_KEY_SECRET, "")); twitter.setOAuthAccessToken(a); Status oStatus = twitter.updateStatus(sMessage); Log.i(this.getClass().getCanonicalName(), "Post Twitter OK"); return oStatus.isRetweet(); } catch (IllegalArgumentException e) { Log.e( this.getClass().getCanonicalName(), "Post Twitter error: " + mSharedPreferences.getString(Const.PREF_KEY_TOKEN, "") + " " + mSharedPreferences.getString(Const.PREF_KEY_SECRET, "")); return false; } } else { String oauthAccessToken = mSharedPreferences.getString(Const.PREF_KEY_TOKEN, ""); String oAuthAccessTokenSecret = mSharedPreferences.getString(Const.PREF_KEY_SECRET, ""); ConfigurationBuilder confbuilder = new ConfigurationBuilder(); Configuration configuration = confbuilder .setOAuthConsumerKey(Const.CONSUMER_KEY) .setOAuthConsumerSecret(Const.CONSUMER_SECRET) .setOAuthAccessToken(oauthAccessToken) .setOAuthAccessTokenSecret(oAuthAccessTokenSecret) .build(); twitter = new TwitterFactory(configuration).getInstance(); Status oStatus = twitter.updateStatus(sMessage); Log.i(this.getClass().getCanonicalName(), "Post Twitter OK"); return oStatus.isRetweet(); } } catch (TwitterException e) { Log.e(this.getClass().getCanonicalName(), e.getMessage()); } catch (NullPointerException e) { Log.e(this.getClass().getCanonicalName(), e.getMessage()); } return false; }
public static boolean isEligible(Status tweet) { if (!tweet.getLang().equalsIgnoreCase("en")) { return false; } else if (tweet.isRetweet()) { return false; } else { return true; } }
@Override public View getView(int position, View convertView, ViewGroup parent) { Status status = (Status) this.items.get(position); ViewHolder viewHolder; if (convertView == null) { LayoutInflater layoutInflater = LayoutInflater.from(context); convertView = layoutInflater.inflate(textViewId, null, false); viewHolder = new ViewHolder(); viewHolder.profileImage = (ImageView) convertView.findViewById(R.id.profileImage); viewHolder.authorFullName = (TextView) convertView.findViewById(R.id.authorFullName); viewHolder.authorTwitterHandle = (TextView) convertView.findViewById(R.id.authorTwitterHandle); viewHolder.authorTwitterHandle.setAutoLinkMask(0); viewHolder.tweetText = (TextView) convertView.findViewById(R.id.tweetText); viewHolder.tweetText.setAutoLinkMask(0); viewHolder.retweetAuthor = (TextView) convertView.findViewById(R.id.retweetAuthor); viewHolder.retweetAuthor.setAutoLinkMask(0); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } Picasso.with(context) .load(R.drawable.ic_launcher) .resize(125, 125) .into(viewHolder.profileImage); if (status.isRetweet()) { Picasso.with(context) .load(status.getRetweetedStatus().getUser().getBiggerProfileImageURLHttps()) .resize(125, 125) .into(viewHolder.profileImage); viewHolder.authorFullName.setText(status.getRetweetedStatus().getUser().getName()); viewHolder.authorTwitterHandle.setText( "@" + status.getRetweetedStatus().getUser().getScreenName()); LinkifyWithTwitter.addLinks(viewHolder.authorTwitterHandle, LinkifyWithTwitter.AT_MENTIONS); viewHolder.retweetAuthor.setText("retweeted by @" + status.getUser().getScreenName()); LinkifyWithTwitter.addLinks(viewHolder.retweetAuthor, LinkifyWithTwitter.AT_MENTIONS); } else { Picasso.with(context) .load(status.getUser().getBiggerProfileImageURLHttps()) .resize(125, 125) .into(viewHolder.profileImage); viewHolder.authorFullName.setText(status.getUser().getName()); viewHolder.authorTwitterHandle.setText("@" + status.getUser().getScreenName()); LinkifyWithTwitter.addLinks(viewHolder.authorTwitterHandle, LinkifyWithTwitter.AT_MENTIONS); viewHolder.retweetAuthor.setText(""); } viewHolder.tweetText.setText(status.getText()); LinkifyWithTwitter.addLinks(viewHolder.tweetText, LinkifyWithTwitter.ALL); return convertView; }
@Override public void callbackMethod(Status status) { // TODO 自動生成されたメソッド・スタブ if (status.isRetweet()) { System.out.println( color.text_color( ReTweetDisplay( status.getRetweetedStatus(), status.getUser(), Streaming.statues.size() - 1), "blue")); } else { if (status.getUser().getId() == twitter.user_id) { System.out.println( color.text_color(TweetDisplay(status, Streaming.statues.size() - 1), "cyan")); } else { System.out.println(TweetDisplay(status, Streaming.statues.size() - 1)); } } }
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); }
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); }
/* * (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(); } }
private boolean isStatusRetweet(Status status) { return status.isRetweet(); }