|
@@ -61,13 +61,13 @@ import org.w3c.dom.Element;
|
61
|
61
|
import org.w3c.dom.NodeList;
|
62
|
62
|
import org.xml.sax.SAXException;
|
63
|
63
|
|
64
|
|
-
|
65
|
64
|
/**
|
66
|
65
|
* Implementation of the twitter API for DMDirc.
|
67
|
66
|
*
|
68
|
67
|
* @author shane
|
69
|
68
|
*/
|
70
|
69
|
public class TwitterAPI {
|
|
70
|
+
|
71
|
71
|
/** OAuth Consumer */
|
72
|
72
|
private OAuthConsumer consumer;
|
73
|
73
|
|
|
@@ -100,10 +100,10 @@ public class TwitterAPI {
|
100
|
100
|
|
101
|
101
|
/** API Allowed status */
|
102
|
102
|
private APIAllowed allowed = APIAllowed.UNKNOWN;
|
103
|
|
-
|
|
103
|
+
|
104
|
104
|
/** How many API calls have we made since the last reset? */
|
105
|
105
|
private int usedCalls = 0;
|
106
|
|
-
|
|
106
|
+
|
107
|
107
|
/** API reset time. */
|
108
|
108
|
private long resetTime = 0;
|
109
|
109
|
|
|
@@ -133,7 +133,7 @@ public class TwitterAPI {
|
133
|
133
|
|
134
|
134
|
/** Should we use ssl? */
|
135
|
135
|
private boolean useSSL = false;
|
136
|
|
-
|
|
136
|
+
|
137
|
137
|
/** Should we enable debug? */
|
138
|
138
|
private boolean debug = false;
|
139
|
139
|
|
|
@@ -190,20 +190,22 @@ public class TwitterAPI {
|
190
|
190
|
this.autoAt = autoAt;
|
191
|
191
|
this.myDisplayUsername = (autoAt ? "@" : "") + myLoginUsername;
|
192
|
192
|
|
193
|
|
- if (this.apiAddress.isEmpty() && myLoginUsername.isEmpty()) { return; }
|
|
193
|
+ if (this.apiAddress.isEmpty() && myLoginUsername.isEmpty()) {
|
|
194
|
+ return;
|
|
195
|
+ }
|
194
|
196
|
|
195
|
197
|
if (!consumerKey.isEmpty() && !consumerSecret.isEmpty()) {
|
196
|
198
|
consumer = new DefaultOAuthConsumer(consumerKey, consumerSecret, SignatureMethod.HMAC_SHA1);
|
197
|
199
|
final String thisOauthAddress = ((useSSL) ? "https" : "http") + "://" + ((oauthAddress == null || oauthAddress.isEmpty()) ? apiAddress.replaceAll("/+$", "") + "/oauth" : oauthAddress.replaceAll("/+$", ""));
|
198
|
|
-
|
199
|
|
- provider = new DefaultOAuthProvider(consumer, thisOauthAddress+"/request_token", thisOauthAddress+"/access_token", thisOauthAddress+"/authorize");
|
|
200
|
+
|
|
201
|
+ provider = new DefaultOAuthProvider(consumer, thisOauthAddress + "/request_token", thisOauthAddress + "/access_token", thisOauthAddress + "/authorize");
|
200
|
202
|
|
201
|
203
|
this.token = token;
|
202
|
204
|
this.tokenSecret = tokenSecret;
|
203
|
205
|
|
204
|
206
|
try {
|
205
|
207
|
useOAuth = !(getOAuthURL().isEmpty());
|
206
|
|
- } catch (TwitterRuntimeException tre) {
|
|
208
|
+ } catch (final TwitterRuntimeException tre) {
|
207
|
209
|
useOAuth = false;
|
208
|
210
|
}
|
209
|
211
|
} else {
|
|
@@ -278,7 +280,7 @@ public class TwitterAPI {
|
278
|
280
|
errorHandlers.add(handler);
|
279
|
281
|
}
|
280
|
282
|
}
|
281
|
|
-
|
|
283
|
+
|
282
|
284
|
/**
|
283
|
285
|
* Remove an error handler.
|
284
|
286
|
*
|
|
@@ -289,7 +291,7 @@ public class TwitterAPI {
|
289
|
291
|
errorHandlers.remove(handler);
|
290
|
292
|
}
|
291
|
293
|
}
|
292
|
|
-
|
|
294
|
+
|
293
|
295
|
/**
|
294
|
296
|
* Clear error handlers.
|
295
|
297
|
*/
|
|
@@ -322,7 +324,7 @@ public class TwitterAPI {
|
322
|
324
|
*/
|
323
|
325
|
private void handleError(final Throwable t, final String source, final String twitterInput, final String twitterOutput, final String message) {
|
324
|
326
|
synchronized (errorHandlers) {
|
325
|
|
- for (TwitterErrorHandler eh : errorHandlers) {
|
|
327
|
+ for (final TwitterErrorHandler eh : errorHandlers) {
|
326
|
328
|
eh.handleTwitterError(this, t, source, twitterInput, twitterOutput, message);
|
327
|
329
|
}
|
328
|
330
|
}
|
|
@@ -366,7 +368,7 @@ public class TwitterAPI {
|
366
|
368
|
*/
|
367
|
369
|
private void handleRawInput(final String raw) {
|
368
|
370
|
synchronized (rawHandlers) {
|
369
|
|
- for (TwitterRawHandler rh : rawHandlers) {
|
|
371
|
+ for (final TwitterRawHandler rh : rawHandlers) {
|
370
|
372
|
rh.handleRawTwitterInput(this, raw);
|
371
|
373
|
}
|
372
|
374
|
}
|
|
@@ -379,7 +381,7 @@ public class TwitterAPI {
|
379
|
381
|
*/
|
380
|
382
|
private void handleRawOutput(final String raw) {
|
381
|
383
|
synchronized (rawHandlers) {
|
382
|
|
- for (TwitterRawHandler rh : rawHandlers) {
|
|
384
|
+ for (final TwitterRawHandler rh : rawHandlers) {
|
383
|
385
|
rh.handleRawTwitterOutput(this, raw);
|
384
|
386
|
}
|
385
|
387
|
}
|
|
@@ -400,9 +402,8 @@ public class TwitterAPI {
|
400
|
402
|
* @param autoAt Should we use autoAt?
|
401
|
403
|
*/
|
402
|
404
|
/* public void setAutoAt(final boolean autoAt) {
|
403
|
|
- this.autoAt = autoAt;
|
|
405
|
+ this.autoAt = autoAt;
|
404
|
406
|
} */
|
405
|
|
-
|
406
|
407
|
/**
|
407
|
408
|
* Is debugging enabled?
|
408
|
409
|
*
|
|
@@ -539,18 +540,18 @@ public class TwitterAPI {
|
539
|
540
|
}
|
540
|
541
|
try {
|
541
|
542
|
consumer.sign(connection);
|
542
|
|
- } catch (OAuthMessageSignerException ex) {
|
|
543
|
+ } catch (final OAuthMessageSignerException ex) {
|
543
|
544
|
handleError(ex, "(1) signURL", apiInput, apiOutput, "Unable to sign URL, are we authorised to use this account?");
|
544
|
|
- } catch (OAuthExpectationFailedException ex) {
|
|
545
|
+ } catch (final OAuthExpectationFailedException ex) {
|
545
|
546
|
handleError(ex, "(2) signURL", apiInput, apiOutput, "Unable to sign URL, are we authorised to use this account?");
|
546
|
547
|
}
|
547
|
548
|
} else {
|
548
|
|
- // final String userpassword = myUsername + ":" + myPassword;
|
549
|
|
- // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
|
550
|
|
- // String encodedAuthorization = enc.encode(userpassword.getBytes());
|
|
549
|
+ // final String userpassword = myUsername + ":" + myPassword;
|
|
550
|
+ // sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
|
|
551
|
+ // String encodedAuthorization = enc.encode(userpassword.getBytes());
|
551
|
552
|
|
552
|
|
- final String encodedAuthorization = b64encode(myLoginUsername + ":" + myPassword);
|
553
|
|
- connection.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
|
|
553
|
+ final String encodedAuthorization = b64encode(myLoginUsername + ":" + myPassword);
|
|
554
|
+ connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
|
554
|
555
|
}
|
555
|
556
|
}
|
556
|
557
|
|
|
@@ -567,7 +568,7 @@ public class TwitterAPI {
|
567
|
568
|
byte[] stringArray;
|
568
|
569
|
try {
|
569
|
570
|
stringArray = string.getBytes("UTF-8");
|
570
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
571
|
+ } catch (final UnsupportedEncodingException ex) {
|
571
|
572
|
stringArray = string.getBytes();
|
572
|
573
|
}
|
573
|
574
|
|
|
@@ -602,7 +603,7 @@ public class TwitterAPI {
|
602
|
603
|
public static Long parseLong(final String string, final long fallback) {
|
603
|
604
|
try {
|
604
|
605
|
return Long.parseLong(string);
|
605
|
|
- } catch (NumberFormatException nfe) {
|
|
606
|
+ } catch (final NumberFormatException nfe) {
|
606
|
607
|
return fallback;
|
607
|
608
|
}
|
608
|
609
|
}
|
|
@@ -618,7 +619,7 @@ public class TwitterAPI {
|
618
|
619
|
public static Long timeStringToLong(final String string, final long fallback) {
|
619
|
620
|
try {
|
620
|
621
|
return (new SimpleDateFormat("EEE MMM dd HH:mm:ss zzzz yyyy").parse(string)).getTime();
|
621
|
|
- } catch (ParseException ex) {
|
|
622
|
+ } catch (final ParseException ex) {
|
622
|
623
|
return fallback;
|
623
|
624
|
}
|
624
|
625
|
}
|
|
@@ -662,13 +663,13 @@ public class TwitterAPI {
|
662
|
663
|
try {
|
663
|
664
|
final URL url = new URL(address);
|
664
|
665
|
return getXML((HttpURLConnection) url.openConnection());
|
665
|
|
- } catch (MalformedURLException ex) {
|
|
666
|
+ } catch (final MalformedURLException ex) {
|
666
|
667
|
if (isDebug()) {
|
667
|
|
- handleError(ex, "* (1) getXML: "+address, apiInput, apiOutput);
|
|
668
|
+ handleError(ex, "* (1) getXML: " + address, apiInput, apiOutput);
|
668
|
669
|
}
|
669
|
|
- } catch (IOException ex) {
|
|
670
|
+ } catch (final IOException ex) {
|
670
|
671
|
if (isDebug()) {
|
671
|
|
- handleError(ex, "* (2) getXML: "+address, apiInput, apiOutput);
|
|
672
|
+ handleError(ex, "* (2) getXML: " + address, apiInput, apiOutput);
|
672
|
673
|
}
|
673
|
674
|
}
|
674
|
675
|
|
|
@@ -696,13 +697,13 @@ public class TwitterAPI {
|
696
|
697
|
try {
|
697
|
698
|
final URL url = new URL(address + (params.isEmpty() ? "" : "?" + params));
|
698
|
699
|
return postXML((HttpURLConnection) url.openConnection());
|
699
|
|
- } catch (MalformedURLException ex) {
|
|
700
|
+ } catch (final MalformedURLException ex) {
|
700
|
701
|
if (isDebug()) {
|
701
|
|
- handleError(ex, "* (1) postXML: "+address+" | "+params, apiInput, apiOutput);
|
|
702
|
+ handleError(ex, "* (1) postXML: " + address + " | " + params, apiInput, apiOutput);
|
702
|
703
|
}
|
703
|
|
- } catch (IOException ex) {
|
|
704
|
+ } catch (final IOException ex) {
|
704
|
705
|
if (isDebug()) {
|
705
|
|
- handleError(ex, "* (2) postXML: "+address+" | "+params, apiInput, apiOutput);
|
|
706
|
+ handleError(ex, "* (2) postXML: " + address + " | " + params, apiInput, apiOutput);
|
706
|
707
|
}
|
707
|
708
|
}
|
708
|
709
|
|
|
@@ -721,9 +722,9 @@ public class TwitterAPI {
|
721
|
722
|
request.setRequestMethod("POST");
|
722
|
723
|
request.setRequestProperty("Content-Length", "0");
|
723
|
724
|
request.setUseCaches(false);
|
724
|
|
- } catch (ProtocolException ex) {
|
|
725
|
+ } catch (final ProtocolException ex) {
|
725
|
726
|
if (isDebug()) {
|
726
|
|
- handleError(ex, "* (3) postXML: "+request.getURL(), apiInput, apiOutput);
|
|
727
|
+ handleError(ex, "* (3) postXML: " + request.getURL(), apiInput, apiOutput);
|
727
|
728
|
}
|
728
|
729
|
}
|
729
|
730
|
return getXML(request);
|
|
@@ -750,11 +751,11 @@ public class TwitterAPI {
|
750
|
751
|
signURL(request);
|
751
|
752
|
request.connect();
|
752
|
753
|
return true;
|
753
|
|
- } catch (MalformedURLException ex) {
|
|
754
|
+ } catch (final MalformedURLException ex) {
|
754
|
755
|
return false;
|
755
|
|
- } catch (NoRouteToHostException ex) {
|
|
756
|
+ } catch (final NoRouteToHostException ex) {
|
756
|
757
|
return false;
|
757
|
|
- } catch (IOException ex) {
|
|
758
|
+ } catch (final IOException ex) {
|
758
|
759
|
if (isDebug()) {
|
759
|
760
|
handleError(ex, "* (1) checkConnection", "", "");
|
760
|
761
|
}
|
|
@@ -777,7 +778,7 @@ public class TwitterAPI {
|
777
|
778
|
resetTime = System.currentTimeMillis() + 3600000;
|
778
|
779
|
}
|
779
|
780
|
usedCalls++;
|
780
|
|
-
|
|
781
|
+
|
781
|
782
|
apiInput = request.getURL().toString();
|
782
|
783
|
handleRawOutput(apiInput);
|
783
|
784
|
BufferedReader in = null;
|
|
@@ -788,9 +789,9 @@ public class TwitterAPI {
|
788
|
789
|
request.setReadTimeout(5000);
|
789
|
790
|
request.connect();
|
790
|
791
|
in = new BufferedReader(new InputStreamReader(request.getInputStream()));
|
791
|
|
- } catch (IOException ex) {
|
|
792
|
+ } catch (final IOException ex) {
|
792
|
793
|
if (isDebug()) {
|
793
|
|
- handleError(ex, "* (4) getXML: "+request.getURL(), apiInput, apiOutput);
|
|
794
|
+ handleError(ex, "* (4) getXML: " + request.getURL(), apiInput, apiOutput);
|
794
|
795
|
}
|
795
|
796
|
if (request.getErrorStream() != null) {
|
796
|
797
|
in = new BufferedReader(new InputStreamReader(request.getErrorStream()));
|
|
@@ -814,14 +815,17 @@ public class TwitterAPI {
|
814
|
815
|
} while (line != null);
|
815
|
816
|
|
816
|
817
|
apiOutput = xml.toString().trim();
|
817
|
|
- } catch (IOException ex) {
|
|
818
|
+ } catch (final IOException ex) {
|
818
|
819
|
apiOutput = xml.toString().trim() + "\n ... Incomplete!";
|
819
|
820
|
incomplete = true;
|
820
|
821
|
if (isDebug()) {
|
821
|
822
|
handleError(ex, "* (5) getXML", apiInput, apiOutput);
|
822
|
823
|
}
|
823
|
824
|
} finally {
|
824
|
|
- try { in.close(); } catch (IOException ex) { }
|
|
825
|
+ try {
|
|
826
|
+ in.close();
|
|
827
|
+ } catch (final IOException ex) {
|
|
828
|
+ }
|
825
|
829
|
}
|
826
|
830
|
}
|
827
|
831
|
|
|
@@ -833,12 +837,12 @@ public class TwitterAPI {
|
833
|
837
|
responseCode = request.getResponseCode();
|
834
|
838
|
if (responseCode != 200) {
|
835
|
839
|
if (isDebug()) {
|
836
|
|
- handleError(null, "* (6) getXML", apiInput, apiOutput, "("+request.getResponseCode()+") "+request.getResponseMessage());
|
|
840
|
+ handleError(null, "* (6) getXML", apiInput, apiOutput, "(" + request.getResponseCode() + ") " + request.getResponseMessage());
|
837
|
841
|
} else if (responseCode >= 500 && responseCode < 600) {
|
838
|
|
- handleError(null, "(6) getXML", apiInput, apiOutput, "("+request.getResponseCode()+") "+request.getResponseMessage());
|
|
842
|
+ handleError(null, "(6) getXML", apiInput, apiOutput, "(" + request.getResponseCode() + ") " + request.getResponseMessage());
|
839
|
843
|
}
|
840
|
844
|
}
|
841
|
|
- } catch (IOException ioe) {
|
|
845
|
+ } catch (final IOException ioe) {
|
842
|
846
|
responseCode = 0;
|
843
|
847
|
if (isDebug()) {
|
844
|
848
|
handleError(ioe, "* (7) getXML", apiInput, apiOutput, "Unable to get response code.");
|
|
@@ -847,7 +851,7 @@ public class TwitterAPI {
|
847
|
851
|
|
848
|
852
|
try {
|
849
|
853
|
final DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
850
|
|
-
|
|
854
|
+
|
851
|
855
|
final Document doc = db.parse(new ByteArrayInputStream(apiOutput.getBytes()));
|
852
|
856
|
|
853
|
857
|
final XMLResponse response = new XMLResponse(request, doc);
|
|
@@ -856,15 +860,15 @@ public class TwitterAPI {
|
856
|
860
|
}
|
857
|
861
|
|
858
|
862
|
return response;
|
859
|
|
- } catch (SAXException ex) {
|
|
863
|
+ } catch (final SAXException ex) {
|
860
|
864
|
if (isDebug()) {
|
861
|
865
|
handleError(ex, "* (9) getXML", apiInput, apiOutput);
|
862
|
866
|
}
|
863
|
|
- } catch (ParserConfigurationException ex) {
|
|
867
|
+ } catch (final ParserConfigurationException ex) {
|
864
|
868
|
if (isDebug()) {
|
865
|
869
|
handleError(ex, "* (10) getXML", apiInput, apiOutput);
|
866
|
870
|
}
|
867
|
|
- } catch (IOException ex) {
|
|
871
|
+ } catch (final IOException ex) {
|
868
|
872
|
if (isDebug()) {
|
869
|
873
|
handleError(ex, "* (11) getXML", apiInput, apiOutput);
|
870
|
874
|
}
|
|
@@ -879,7 +883,9 @@ public class TwitterAPI {
|
879
|
883
|
* @param status
|
880
|
884
|
*/
|
881
|
885
|
protected void uncacheStatus(final TwitterStatus status) {
|
882
|
|
- if (status == null) { return; }
|
|
886
|
+ if (status == null) {
|
|
887
|
+ return;
|
|
888
|
+ }
|
883
|
889
|
synchronized (statusCache) {
|
884
|
890
|
statusCache.remove(status.getID());
|
885
|
891
|
}
|
|
@@ -891,7 +897,9 @@ public class TwitterAPI {
|
891
|
897
|
* @param user
|
892
|
898
|
*/
|
893
|
899
|
protected void uncacheUser(final TwitterUser user) {
|
894
|
|
- if (user == null) { return; }
|
|
900
|
+ if (user == null) {
|
|
901
|
+ return;
|
|
902
|
+ }
|
895
|
903
|
synchronized (userCache) {
|
896
|
904
|
userCache.remove(user.getScreenName().toLowerCase());
|
897
|
905
|
userIDMap.remove(user.getID());
|
|
@@ -920,7 +928,9 @@ public class TwitterAPI {
|
920
|
928
|
* @param user
|
921
|
929
|
*/
|
922
|
930
|
protected void updateUser(final String username, final TwitterUser user) {
|
923
|
|
- if (user == null) { return; }
|
|
931
|
+ if (user == null) {
|
|
932
|
+ return;
|
|
933
|
+ }
|
924
|
934
|
synchronized (userCache) {
|
925
|
935
|
if (!username.equalsIgnoreCase(user.getScreenName())) {
|
926
|
936
|
userCache.remove(username.toLowerCase());
|
|
@@ -971,9 +981,9 @@ public class TwitterAPI {
|
971
|
981
|
TwitterUser user = getCachedUser(username);
|
972
|
982
|
if (user == null || force) {
|
973
|
983
|
if (username.equalsIgnoreCase(myDisplayUsername) && !isAllowed()) {
|
974
|
|
- user = new TwitterUser(this, myLoginUsername, -1, "", true);
|
|
984
|
+ user = new TwitterUser(this, myLoginUsername, -1, "", true);
|
975
|
985
|
} else {
|
976
|
|
- final XMLResponse doc = getXML(getURL("users/show")+"?screen_name="+username);
|
|
986
|
+ final XMLResponse doc = getXML(getURL("users/show") + "?screen_name=" + username);
|
977
|
987
|
|
978
|
988
|
if (doc.isGood()) {
|
979
|
989
|
user = new TwitterUser(this, doc.getDocumentElement());
|
|
@@ -982,7 +992,9 @@ public class TwitterAPI {
|
982
|
992
|
}
|
983
|
993
|
}
|
984
|
994
|
|
985
|
|
- if (user != null) { updateUser(user); }
|
|
995
|
+ if (user != null) {
|
|
996
|
+ updateUser(user);
|
|
997
|
+ }
|
986
|
998
|
}
|
987
|
999
|
|
988
|
1000
|
return user;
|
|
@@ -995,7 +1007,9 @@ public class TwitterAPI {
|
995
|
1007
|
* @param status
|
996
|
1008
|
*/
|
997
|
1009
|
protected void updateStatus(final TwitterStatus status) {
|
998
|
|
- if (status == null) { return; }
|
|
1010
|
+ if (status == null) {
|
|
1011
|
+ return;
|
|
1012
|
+ }
|
999
|
1013
|
synchronized (statusCache) {
|
1000
|
1014
|
statusCache.put(status.getID(), status);
|
1001
|
1015
|
}
|
|
@@ -1037,7 +1051,7 @@ public class TwitterAPI {
|
1037
|
1051
|
public TwitterStatus getStatus(final long id, final boolean force) {
|
1038
|
1052
|
TwitterStatus status = getCachedStatus(id);
|
1039
|
1053
|
if (status == null || force) {
|
1040
|
|
- final XMLResponse doc = getXML(getURL("statuses/show/"+id));
|
|
1054
|
+ final XMLResponse doc = getXML(getURL("statuses/show/" + id));
|
1041
|
1055
|
|
1042
|
1056
|
if (doc.isGood()) {
|
1043
|
1057
|
status = new TwitterStatus(this, doc.getDocumentElement());
|
|
@@ -1061,7 +1075,7 @@ public class TwitterAPI {
|
1061
|
1075
|
synchronized (statusCache) {
|
1062
|
1076
|
final Map<Long, TwitterStatus> current = new HashMap<Long, TwitterStatus>(statusCache);
|
1063
|
1077
|
|
1064
|
|
- for (Map.Entry<Long, TwitterStatus> item : current.entrySet()) {
|
|
1078
|
+ for (final Map.Entry<Long, TwitterStatus> item : current.entrySet()) {
|
1065
|
1079
|
if (item.getValue().getTime() < time) {
|
1066
|
1080
|
statusCache.remove(item.getKey());
|
1067
|
1081
|
}
|
|
@@ -1084,12 +1098,12 @@ public class TwitterAPI {
|
1084
|
1098
|
new TwitterMessage(this, doc.getDocumentElement());
|
1085
|
1099
|
return true;
|
1086
|
1100
|
}
|
1087
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1101
|
+ } catch (final UnsupportedEncodingException ex) {
|
1088
|
1102
|
if (isDebug()) {
|
1089
|
|
- handleError(ex, "* (1) newDirectMessage: "+target+" | "+message, apiInput, apiOutput);
|
|
1103
|
+ handleError(ex, "* (1) newDirectMessage: " + target + " | " + message, apiInput, apiOutput);
|
1090
|
1104
|
}
|
1091
|
1105
|
}
|
1092
|
|
-
|
|
1106
|
+
|
1093
|
1107
|
return false;
|
1094
|
1108
|
}
|
1095
|
1109
|
|
|
@@ -1111,7 +1125,7 @@ public class TwitterAPI {
|
1111
|
1125
|
public List<TwitterStatus> getUserTimeline(final long lastUserTimelineId) {
|
1112
|
1126
|
final List<TwitterStatus> result = new ArrayList<TwitterStatus>();
|
1113
|
1127
|
|
1114
|
|
- final XMLResponse doc = getXML(getURL("statuses/user_timeline")+"?since_id="+lastUserTimelineId+"&count=20");
|
|
1128
|
+ final XMLResponse doc = getXML(getURL("statuses/user_timeline") + "?since_id=" + lastUserTimelineId + "&count=20");
|
1115
|
1129
|
|
1116
|
1130
|
if (doc.isGood()) {
|
1117
|
1131
|
final NodeList nodes = doc.getElementsByTagName("status");
|
|
@@ -1145,8 +1159,7 @@ public class TwitterAPI {
|
1145
|
1159
|
final List<TwitterStatus> result = new ArrayList<TwitterStatus>();
|
1146
|
1160
|
|
1147
|
1161
|
try {
|
1148
|
|
- final XMLResponse doc = getXML("http://search.twitter.com/search.atom?since_id="
|
1149
|
|
- +lastStatusId + "&rpp=100&q=" + URLEncoder.encode(term, "utf-8"));
|
|
1162
|
+ final XMLResponse doc = getXML("http://search.twitter.com/search.atom?since_id=" + lastStatusId + "&rpp=100&q=" + URLEncoder.encode(term, "utf-8"));
|
1150
|
1163
|
|
1151
|
1164
|
if (doc.isGood()) {
|
1152
|
1165
|
final NodeList nodes = doc.getElementsByTagName("entry");
|
|
@@ -1161,13 +1174,13 @@ public class TwitterAPI {
|
1161
|
1174
|
result.add(0, new TwitterStatus(this, message, -1, id, user, time));
|
1162
|
1175
|
}
|
1163
|
1176
|
}
|
1164
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1177
|
+ } catch (final UnsupportedEncodingException ex) {
|
1165
|
1178
|
if (isDebug()) {
|
1166
|
|
- handleError(ex, "* (1) getSearchResults: "+term+" | "+lastStatusId, apiInput, apiOutput);
|
|
1179
|
+ handleError(ex, "* (1) getSearchResults: " + term + " | " + lastStatusId, apiInput, apiOutput);
|
1167
|
1180
|
}
|
1168
|
|
- } catch (ParseException ex) {
|
|
1181
|
+ } catch (final ParseException ex) {
|
1169
|
1182
|
if (isDebug()) {
|
1170
|
|
- handleError(ex, "* (2) getSearchResults: "+term+" | "+lastStatusId, apiInput, apiOutput);
|
|
1183
|
+ handleError(ex, "* (2) getSearchResults: " + term + " | " + lastStatusId, apiInput, apiOutput);
|
1171
|
1184
|
}
|
1172
|
1185
|
}
|
1173
|
1186
|
|
|
@@ -1283,7 +1296,7 @@ public class TwitterAPI {
|
1283
|
1296
|
public List<TwitterStatus> getReplies(final long lastReplyId, final int count) {
|
1284
|
1297
|
final List<TwitterStatus> result = new ArrayList<TwitterStatus>();
|
1285
|
1298
|
|
1286
|
|
- final XMLResponse doc = getXML(getURL("statuses/mentions")+"?since_id="+lastReplyId+"&count="+count);
|
|
1299
|
+ final XMLResponse doc = getXML(getURL("statuses/mentions") + "?since_id=" + lastReplyId + "&count=" + count);
|
1287
|
1300
|
if (doc.isGood()) {
|
1288
|
1301
|
final NodeList nodes = doc.getElementsByTagName("status");
|
1289
|
1302
|
|
|
@@ -1304,7 +1317,7 @@ public class TwitterAPI {
|
1304
|
1317
|
public List<TwitterStatus> getFriendsTimeline(final long lastTimelineId) {
|
1305
|
1318
|
return getFriendsTimeline(lastTimelineId, 20);
|
1306
|
1319
|
}
|
1307
|
|
-
|
|
1320
|
+
|
1308
|
1321
|
/**
|
1309
|
1322
|
* Get the messages sent by friends that are later than the given ID.
|
1310
|
1323
|
*
|
|
@@ -1315,7 +1328,7 @@ public class TwitterAPI {
|
1315
|
1328
|
public List<TwitterStatus> getFriendsTimeline(final long lastTimelineId, final int count) {
|
1316
|
1329
|
final List<TwitterStatus> result = new ArrayList<TwitterStatus>();
|
1317
|
1330
|
|
1318
|
|
- final XMLResponse doc = getXML(getURL("statuses/home_timeline")+"?since_id="+lastTimelineId+"&count="+count);
|
|
1331
|
+ final XMLResponse doc = getXML(getURL("statuses/home_timeline") + "?since_id=" + lastTimelineId + "&count=" + count);
|
1319
|
1332
|
if (doc.isGood()) {
|
1320
|
1333
|
final NodeList nodes = doc.getElementsByTagName("status");
|
1321
|
1334
|
for (int i = 0; i < nodes.getLength(); i++) {
|
|
@@ -1346,7 +1359,7 @@ public class TwitterAPI {
|
1346
|
1359
|
public List<TwitterMessage> getDirectMessages(final long lastDirectMessageId, final int count) {
|
1347
|
1360
|
final List<TwitterMessage> result = new ArrayList<TwitterMessage>();
|
1348
|
1361
|
|
1349
|
|
- final XMLResponse doc = getXML(getURL("direct_messages")+"?since_id="+lastDirectMessageId+"&count="+count);
|
|
1362
|
+ final XMLResponse doc = getXML(getURL("direct_messages") + "?since_id=" + lastDirectMessageId + "&count=" + count);
|
1350
|
1363
|
if (doc.isGood()) {
|
1351
|
1364
|
final NodeList nodes = doc.getElementsByTagName("direct_message");
|
1352
|
1365
|
for (int i = 0; i < nodes.getLength(); i++) {
|
|
@@ -1377,7 +1390,7 @@ public class TwitterAPI {
|
1377
|
1390
|
public List<TwitterMessage> getSentDirectMessages(final long lastDirectMessageId, final int count) {
|
1378
|
1391
|
final List<TwitterMessage> result = new ArrayList<TwitterMessage>();
|
1379
|
1392
|
|
1380
|
|
- final XMLResponse doc = getXML(getURL("direct_messages/sent")+"?since_id="+lastDirectMessageId+"&count="+count);
|
|
1393
|
+ final XMLResponse doc = getXML(getURL("direct_messages/sent") + "?since_id=" + lastDirectMessageId + "&count=" + count);
|
1381
|
1394
|
if (doc.isGood()) {
|
1382
|
1395
|
final NodeList nodes = doc.getElementsByTagName("direct_message");
|
1383
|
1396
|
for (int i = 0; i < nodes.getLength(); i++) {
|
|
@@ -1400,10 +1413,10 @@ public class TwitterAPI {
|
1400
|
1413
|
final StringBuilder params = new StringBuilder("status=");
|
1401
|
1414
|
params.append(URLEncoder.encode(status, "utf-8"));
|
1402
|
1415
|
if (id >= 0) {
|
1403
|
|
- params.append("&in_reply_to_status_id="+Long.toString(id));
|
|
1416
|
+ params.append("&in_reply_to_status_id=" + Long.toString(id));
|
1404
|
1417
|
}
|
1405
|
1418
|
if (!useOAuth) {
|
1406
|
|
- params.append("&source="+URLEncoder.encode(mySource, "utf-8"));
|
|
1419
|
+ params.append("&source=" + URLEncoder.encode(mySource, "utf-8"));
|
1407
|
1420
|
}
|
1408
|
1421
|
|
1409
|
1422
|
final XMLResponse doc = postXML(getURL("statuses/update"), params.toString());
|
|
@@ -1413,13 +1426,13 @@ public class TwitterAPI {
|
1413
|
1426
|
}
|
1414
|
1427
|
return true;
|
1415
|
1428
|
}
|
1416
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1429
|
+ } catch (final UnsupportedEncodingException ex) {
|
1417
|
1430
|
if (isDebug()) {
|
1418
|
|
- handleError(ex, "* (1) setStatus: "+status+" | "+id, apiInput, apiOutput);
|
|
1431
|
+ handleError(ex, "* (1) setStatus: " + status + " | " + id, apiInput, apiOutput);
|
1419
|
1432
|
}
|
1420
|
|
- } catch (IOException ex) {
|
|
1433
|
+ } catch (final IOException ex) {
|
1421
|
1434
|
if (isDebug()) {
|
1422
|
|
- handleError(ex, "* (2) setStatus: "+status+" | "+id, apiInput, apiOutput);
|
|
1435
|
+ handleError(ex, "* (2) setStatus: " + status + " | " + id, apiInput, apiOutput);
|
1423
|
1436
|
}
|
1424
|
1437
|
}
|
1425
|
1438
|
|
|
@@ -1433,7 +1446,7 @@ public class TwitterAPI {
|
1433
|
1446
|
* @return True if status was retweeted ok.
|
1434
|
1447
|
*/
|
1435
|
1448
|
public boolean retweetStatus(final TwitterStatus status) {
|
1436
|
|
- final XMLResponse doc = postXML(getURL("statuses/retweet/"+status.getID()));
|
|
1449
|
+ final XMLResponse doc = postXML(getURL("statuses/retweet/" + status.getID()));
|
1437
|
1450
|
if (doc.getResponseCode() == 200) {
|
1438
|
1451
|
if (doc.isGood()) {
|
1439
|
1452
|
new TwitterStatus(this, doc.getDocumentElement());
|
|
@@ -1451,7 +1464,7 @@ public class TwitterAPI {
|
1451
|
1464
|
* @return True if status was deleted ok.
|
1452
|
1465
|
*/
|
1453
|
1466
|
public boolean deleteStatus(final TwitterStatus status) {
|
1454
|
|
- final XMLResponse doc = postXML(getURL("statuses/destroy/"+status.getID()));
|
|
1467
|
+ final XMLResponse doc = postXML(getURL("statuses/destroy/" + status.getID()));
|
1455
|
1468
|
if (doc.getResponseCode() == 200) {
|
1456
|
1469
|
if (doc.isGood()) {
|
1457
|
1470
|
final TwitterStatus deletedStatus = new TwitterStatus(this, doc.getDocumentElement());
|
|
@@ -1485,7 +1498,7 @@ public class TwitterAPI {
|
1485
|
1498
|
|
1486
|
1499
|
final long remaining = parseLong(getElementContents(element, "remaining-hits", ""), -1);
|
1487
|
1500
|
final long total = parseLong(getElementContents(element, "hourly-limit", ""), -1);
|
1488
|
|
-
|
|
1501
|
+
|
1489
|
1502
|
// laconica does this wrong :( so support both.
|
1490
|
1503
|
final String resetTimeString = getElementContents(element, "reset-time-in-seconds", getElementContents(element, "reset_time_in_seconds", "0"));
|
1491
|
1504
|
resetTime = 1000 * parseLong(resetTimeString, -1);
|
|
@@ -1514,28 +1527,28 @@ public class TwitterAPI {
|
1514
|
1527
|
public String getOAuthURL() throws TwitterRuntimeException {
|
1515
|
1528
|
try {
|
1516
|
1529
|
return provider.retrieveRequestToken(OAuth.OUT_OF_BAND);
|
1517
|
|
- } catch (OAuthMessageSignerException ex) {
|
|
1530
|
+ } catch (final OAuthMessageSignerException ex) {
|
1518
|
1531
|
if (myPassword.isEmpty()) {
|
1519
|
1532
|
if (isDebug()) {
|
1520
|
1533
|
handleError(ex, "* (1) getOAuthURL", apiInput, apiOutput);
|
1521
|
1534
|
}
|
1522
|
1535
|
throw new TwitterRuntimeException(ex.getMessage(), ex);
|
1523
|
1536
|
}
|
1524
|
|
- } catch (OAuthNotAuthorizedException ex) {
|
|
1537
|
+ } catch (final OAuthNotAuthorizedException ex) {
|
1525
|
1538
|
if (myPassword.isEmpty()) {
|
1526
|
1539
|
if (isDebug()) {
|
1527
|
1540
|
handleError(ex, "* (2) getOAuthURL", apiInput, apiOutput);
|
1528
|
1541
|
}
|
1529
|
1542
|
throw new TwitterRuntimeException(ex.getMessage(), ex);
|
1530
|
1543
|
}
|
1531
|
|
- } catch (OAuthExpectationFailedException ex) {
|
|
1544
|
+ } catch (final OAuthExpectationFailedException ex) {
|
1532
|
1545
|
if (myPassword.isEmpty()) {
|
1533
|
1546
|
if (isDebug()) {
|
1534
|
1547
|
handleError(ex, "* (3) getOAuthURL", apiInput, apiOutput);
|
1535
|
1548
|
}
|
1536
|
1549
|
throw new TwitterRuntimeException(ex.getMessage(), ex);
|
1537
|
1550
|
}
|
1538
|
|
- } catch (OAuthCommunicationException ex) {
|
|
1551
|
+ } catch (final OAuthCommunicationException ex) {
|
1539
|
1552
|
if (myPassword.isEmpty()) {
|
1540
|
1553
|
if (isDebug()) {
|
1541
|
1554
|
handleError(ex, "* (4) getOAuthURL", apiInput, apiOutput);
|
|
@@ -1555,29 +1568,31 @@ public class TwitterAPI {
|
1555
|
1568
|
* @throws TwitterException if there is a problem with OAuth.
|
1556
|
1569
|
*/
|
1557
|
1570
|
public void setAccessPin(final String pin) throws TwitterException {
|
1558
|
|
- if (!useOAuth) { return; }
|
|
1571
|
+ if (!useOAuth) {
|
|
1572
|
+ return;
|
|
1573
|
+ }
|
1559
|
1574
|
try {
|
1560
|
1575
|
provider.retrieveAccessToken(pin);
|
1561
|
1576
|
token = consumer.getToken();
|
1562
|
1577
|
tokenSecret = consumer.getTokenSecret();
|
1563
|
|
- } catch (OAuthMessageSignerException ex) {
|
|
1578
|
+ } catch (final OAuthMessageSignerException ex) {
|
1564
|
1579
|
if (isDebug()) {
|
1565
|
|
- handleError(ex, "* (1) setAccessPin: "+pin, apiInput, apiOutput);
|
|
1580
|
+ handleError(ex, "* (1) setAccessPin: " + pin, apiInput, apiOutput);
|
1566
|
1581
|
}
|
1567
|
1582
|
throw new TwitterException(ex.getMessage(), ex);
|
1568
|
|
- } catch (OAuthNotAuthorizedException ex) {
|
|
1583
|
+ } catch (final OAuthNotAuthorizedException ex) {
|
1569
|
1584
|
if (isDebug()) {
|
1570
|
|
- handleError(ex, "* (2) setAccessPin: "+pin, apiInput, apiOutput);
|
|
1585
|
+ handleError(ex, "* (2) setAccessPin: " + pin, apiInput, apiOutput);
|
1571
|
1586
|
}
|
1572
|
1587
|
throw new TwitterException(ex.getMessage(), ex);
|
1573
|
|
- } catch (OAuthExpectationFailedException ex) {
|
|
1588
|
+ } catch (final OAuthExpectationFailedException ex) {
|
1574
|
1589
|
if (isDebug()) {
|
1575
|
|
- handleError(ex, "* (3) setAccessPin: "+pin, apiInput, apiOutput);
|
|
1590
|
+ handleError(ex, "* (3) setAccessPin: " + pin, apiInput, apiOutput);
|
1576
|
1591
|
}
|
1577
|
1592
|
throw new TwitterException(ex.getMessage(), ex);
|
1578
|
|
- } catch (OAuthCommunicationException ex) {
|
|
1593
|
+ } catch (final OAuthCommunicationException ex) {
|
1579
|
1594
|
if (isDebug()) {
|
1580
|
|
- handleError(ex, "* (4) setAccessPin: "+pin, apiInput, apiOutput);
|
|
1595
|
+ handleError(ex, "* (4) setAccessPin: " + pin, apiInput, apiOutput);
|
1581
|
1596
|
}
|
1582
|
1597
|
throw new TwitterException(ex.getMessage(), ex);
|
1583
|
1598
|
}
|
|
@@ -1618,7 +1633,9 @@ public class TwitterAPI {
|
1618
|
1633
|
* @return true if we have been authorised, else false.
|
1619
|
1634
|
*/
|
1620
|
1635
|
public boolean isAllowed(final boolean forceRecheck) {
|
1621
|
|
- if (myLoginUsername.isEmpty()) { return false; }
|
|
1636
|
+ if (myLoginUsername.isEmpty()) {
|
|
1637
|
+ return false;
|
|
1638
|
+ }
|
1622
|
1639
|
|
1623
|
1640
|
if ((useOAuth && (getToken().isEmpty() || getTokenSecret().isEmpty())) || (!useOAuth && myPassword.isEmpty())) {
|
1624
|
1641
|
return false;
|
|
@@ -1635,7 +1652,7 @@ public class TwitterAPI {
|
1635
|
1652
|
updateUser(user);
|
1636
|
1653
|
getRemainingApiCalls();
|
1637
|
1654
|
}
|
1638
|
|
- } catch (IOException ex) {
|
|
1655
|
+ } catch (final IOException ex) {
|
1639
|
1656
|
if (isDebug()) {
|
1640
|
1657
|
handleError(ex, "* (1) isAllowed", apiInput, apiOutput);
|
1641
|
1658
|
}
|
|
@@ -1660,9 +1677,9 @@ public class TwitterAPI {
|
1660
|
1677
|
updateUser(user);
|
1661
|
1678
|
return user;
|
1662
|
1679
|
}
|
1663
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1680
|
+ } catch (final UnsupportedEncodingException ex) {
|
1664
|
1681
|
if (isDebug()) {
|
1665
|
|
- handleError(ex, "* (1) addFriend: "+name, apiInput, apiOutput);
|
|
1682
|
+ handleError(ex, "* (1) addFriend: " + name, apiInput, apiOutput);
|
1666
|
1683
|
}
|
1667
|
1684
|
}
|
1668
|
1685
|
|
|
@@ -1684,9 +1701,9 @@ public class TwitterAPI {
|
1684
|
1701
|
|
1685
|
1702
|
return user;
|
1686
|
1703
|
}
|
1687
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1704
|
+ } catch (final UnsupportedEncodingException ex) {
|
1688
|
1705
|
if (isDebug()) {
|
1689
|
|
- handleError(ex, "* (1) delFriend: "+name, apiInput, apiOutput);
|
|
1706
|
+ handleError(ex, "* (1) delFriend: " + name, apiInput, apiOutput);
|
1690
|
1707
|
}
|
1691
|
1708
|
}
|
1692
|
1709
|
|
|
@@ -1708,9 +1725,9 @@ public class TwitterAPI {
|
1708
|
1725
|
|
1709
|
1726
|
return user;
|
1710
|
1727
|
}
|
1711
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1728
|
+ } catch (final UnsupportedEncodingException ex) {
|
1712
|
1729
|
if (isDebug()) {
|
1713
|
|
- handleError(ex, "*(1) blockUser: "+name, apiInput, apiOutput);
|
|
1730
|
+ handleError(ex, "*(1) blockUser: " + name, apiInput, apiOutput);
|
1714
|
1731
|
}
|
1715
|
1732
|
}
|
1716
|
1733
|
|
|
@@ -1732,12 +1749,13 @@ public class TwitterAPI {
|
1732
|
1749
|
|
1733
|
1750
|
return user;
|
1734
|
1751
|
}
|
1735
|
|
- } catch (UnsupportedEncodingException ex) {
|
|
1752
|
+ } catch (final UnsupportedEncodingException ex) {
|
1736
|
1753
|
if (isDebug()) {
|
1737
|
|
- handleError(ex, "* (1) unblockUser: "+name, apiInput, apiOutput);
|
|
1754
|
+ handleError(ex, "* (1) unblockUser: " + name, apiInput, apiOutput);
|
1738
|
1755
|
}
|
1739
|
1756
|
}
|
1740
|
1757
|
|
1741
|
1758
|
return null;
|
1742
|
1759
|
}
|
|
1760
|
+
|
1743
|
1761
|
}
|