Browse files

bugfix for a Twitter bug

  • Loading branch information...
1 parent 4ff26b2 commit f6c2840ed9c35c8d7d72751bc593fb70b1bc85fb @winterstein committed Oct 9, 2012
Showing with 44 additions and 6 deletions.
  1. +1 −0 changelist.txt
  2. +20 −4 src/winterwell/jtwitter/Twitter.java
  3. +23 −2 test/winterwell/jtwitter/TwitterTest.java
View
1 changelist.txt
@@ -9,6 +9,7 @@
- Added BoundingBox.isPoint()
- Support for some odd WordPress return values.
- Allow long posts to WordPress.
+ - Fixes a bug in Twitter where short urls lead to bogus TweetEntity offsets.
2.6.5:
- Twitter_Users.show() handles the 404 which you get if all the usernames are deleted
View
24 src/winterwell/jtwitter/Twitter.java
@@ -500,9 +500,25 @@ private KRequestType(String rateLimit) {
// Protect against (rare) dud data from Twitter
_end = Math.min(_end, rawText.length());
_start = Math.min(_start, _end);
- if (_start==_end) { // paranoia
- start = _start;
- end = _end;
+ if (_start==_end) { // paranoia -- but it happens (last seen Oct 2012; see TwitterTest)
+ // Guess blindly by type!
+ switch(type) {
+ case hashtags:
+ break;
+ case urls:
+ Matcher m = InternalUtils.URL_REGEX.matcher(text);
+ if (m.find()) {
+ start = m.start();
+ end = m.end();
+ return;
+ }
+ break;
+ case user_mentions:
+ break;
+ }
+ // Fail
+ end = Math.min(_end, text.length());
+ start = Math.min(_start, end);
return;
}
@@ -605,7 +621,7 @@ public String toString() {
/**
* JTwitter version
*/
- public final static String version = "2.6.6";
+ public final static String version = "2.6.7";
/**
* The maximum number of characters that a tweet can contain.
View
25 test/winterwell/jtwitter/TwitterTest.java
@@ -48,6 +48,27 @@
public class TwitterTest
extends TestCase // Comment out to remove the JUnit dependency
{
+
+ public void testDisplayTextBug() {
+ Twitter jtwit = newTestTwitter();
+
+ Status _tweet = jtwit.getStatus(new BigInteger("255592522955505665"));
+ System.out.println(_tweet.getId());
+ System.out.println(_tweet.getText());
+ System.out.println(_tweet.getTweetEntities(KEntityType.urls));
+ System.out.println(_tweet.getDisplayText());
+ System.out.println();
+
+ List<Status> tweets = jtwit.search("USEFUL Printable diary calendar decluttering lists Village Voices");
+ for (Status tweet : tweets) {
+ System.out.println(tweet.getId());
+ System.out.println(tweet.getText());
+ System.out.println(tweet.getTweetEntities(KEntityType.urls));
+ System.out.println(tweet.getDisplayText());
+ System.out.println();
+ }
+ }
+
public void testSource() {
Twitter jtwit = new Twitter();
HashMap<String,Integer> distro = new HashMap();
@@ -281,7 +302,7 @@ public void testGetSetFavorite() throws InterruptedException {
if (s.isFavorite()) {
twitter.setFavorite(s, false);
Thread.sleep(5000);
- assert !s.isFavorite();
+ assert ! s.isFavorite();
}
twitter.setFavorite(s, true);
Thread.sleep(5000);
@@ -367,7 +388,7 @@ public void testRetweetsByMe() {
List<Status> rtsByMe = twitter.getRetweetsByMe();
// List<Status> rtsOfMe = source.getRetweetsOfMe();
assert retweet.getOriginal().equals(original) : retweet.getOriginal();
- assert retweet.inReplyToStatusId == original.id : retweet.inReplyToStatusId +" vs "+original.id;
+ assert retweet.inReplyToStatusId.equals(original.id) : retweet.inReplyToStatusId +" vs "+original.id;
assert retweet.getText().startsWith("RT @spoonmcguffin: ");
assert ! rtsByMe.isEmpty();
assert rtsByMe.contains(retweet);

0 comments on commit f6c2840

Please sign in to comment.