Child: [r15] (diff)

Download this file

twitter.java    304 lines (267 with data), 10.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/**
* SPDXVersion: SPDX-1.1
* Creator: Person: Nuno Brito (nuno.brito@triplecheck.de)
* Creator: Organization: TripleCheck (http://triplecheck.de)
* Created: 2013-12-14T00:00:00Z
* LicenseName: NOASSERTION
* FileName: twitter.java
* FileType: SOURCE
* FileCopyrightText: <text> Copyright (c) 2013 Nuno Brito, TripleCheck </text>
* FileComment: <text> Support for Twitter related actions </text>
*/
package www;
import definitions.is;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import script.log;
import twitter4j.Paging;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
import utils.html;
/**
*
* @author Nuno Brito, 21st of December 2013 in Darsmtadt, Germany
*/
public class twitter {
// define where we store all cached files
static File folderCache = new File("cache");
/**
* Downloads a file from the Internet straight onto the local disk
* @param url The link to the web file
* @param localFilename The file where the contents will be stored
* @source on http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java
* @author Muhammad Umair - http://stackoverflow.com/users/1749165/muhammad-umair
* @retrieved 2013-12-14 by Nuno Brito
*/
static void downloadFromUrl(String link, File localFilename) {
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL(link);
URLConnection urlConn = url.openConnection();//connect
is = urlConn.getInputStream(); //get connection inputstream
fos = new FileOutputStream(localFilename); //open outputstream to local file
byte[] buffer = new byte[4096]; //declare 4KB buffer
int len;
//while we have availble data, continue downloading and storing to local file
while ((len = is.read(buffer)) > 0) {
fos.write(buffer, 0, len);
}
} catch (IOException ex) {
Logger.getLogger(twitter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException ex) {
Logger.getLogger(twitter.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ex) {
Logger.getLogger(twitter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
/**
* When given an input, replace all http:// references on text with HTML link
* representations.
*
* @param text the plain text containing the links to be converted
* @return formatted HTML text, links are now clickable
* @source on http://stackoverflow.com/questions/1909534/java-replacing-text-url-with-clickable-html-link
* @author Paul Croarkin - http://stackoverflow.com/users/18995/paul-croarkin
* @retrieved 2013-12-14 by Nuno Brito
*/
public static String textToHtmlConvertingURLsToLinks(String text) {
if (text == null) {
return text;
}
return text.replaceAll("(\\A|\\s)((http|https|ftp|mailto):\\S+)(\\s|\\z)",
"$1<a href=\"$2\">$2</a>$4");
}
/**
* When given an input, replace all http:// references on text with HTML link
* representations.
*
* @param input the plain text containing the links to be converted
* @return formatted HTML text, links are now clickable
*/
public static String replaceTextWithURL(String input){
String result = input;
String temp = input;
while(temp.contains("http://")){
// get the beginning of the http tag
int i = temp.indexOf("http://");
temp = temp.substring(i);
// get the first space
int end = temp.indexOf(" ");
// is the link on the end of text?
if(end == -1){
end = temp.length();
}
String url = temp.substring(0, end);
// replace the URL with the HTML code
result = result.replace(url, "<a href='"+url+"'>"+url+"</a>");
// process the rest of the string
temp = temp.substring(end);
System.out.println(result);
}
return result;
}
/**
* Provides an HTML formatted link to an image
* @param url
* @return
*/
static String getImageHTML(String url){
String temp = url.toLowerCase();
// BMP files are not supported
if(temp.endsWith(".bmp")){
return "";
}
// first, we need to cache this image
//String data = utils.internet.getTextFile(url);
//String data = utils.internet.webget(url);
int pos = url.lastIndexOf("/") + 1;
String fileName = url.substring(pos);
File imageFile = new File(folderCache, fileName);
if(imageFile.exists() == false){
downloadFromUrl(url,imageFile);
}
// second round
if(imageFile.exists() == false){
return "";
}
// do a web request to support both web browsers and desktop images
WebRequest request = new WebRequest();
request.BaseFolder = folderCache;
request.requestOrigin = RequestOrigin.GUI;
return html.getIcon(fileName, request);
// return "<img style=\"width: 48px; height: 48px;\" src=\""+url+"\"'>";
}
static String doItem(String image, String text){
return
"<table style=\"text-align: left; width: 100%;\" border=\"0\""
+ "cellpadding=\"2\" cellspacing=\"2\">"
+ " <tbody>"
+ " <tr>"
+ " <td style=\"height: 50px; width: 50px; text-align: center; vertical-align: middle;\">"
+ getImageHTML(image)
+ "</td>"
+ " <td>"
+ text
+ " </td>"
+ " </tr>"
+ " </tbody>"
+ "</table>"
;
}
/**
* Retrieves a list of tweet entries from a given username
* @param userName The username from where the tweets are read
* @return An HTML formated string
*/
public static String getTimeLine(String userName){
// the file where we cache our results
File file = new File(folderCache, "twitter.html");
// shall we provide the cached version instead?
if(file.exists()){
long lastModified = file.lastModified();
// define cache time as 24 hours
long cacheTime = 24 * 60 * 60 * 1000;
long rightNow = System.currentTimeMillis();
// have more than 24 hours passed?
if(rightNow < (lastModified + cacheTime)){
long inBetween = rightNow - lastModified;
// use the cached file instead
log.write(is.INFO, "Using the cached twitter page from %1 ago",
utils.time.timeNumberToHumanReadable(inBetween));
return utils.files.readAsString(file);
}
}
String result = "";
Paging pg = new Paging();
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setOAuthConsumerKey("hQzmMsrVRc8mnFrWuz4Fvg");
cb.setOAuthConsumerSecret("jAbxaE9fiDQ4DjepmlOOspYCTyaKrrEYTB6h4R5dWY");
cb.setOAuthAccessToken("2238936962-C5W622ue70tUMBwjefRPifhg9U2ReDhhOxC7EcK");
cb.setOAuthAccessTokenSecret("lec0U1dbH7QRch73H9sxxtXD4JgUsec1iDNF2AHf6to4H");
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
int numberOfTweets = 10;
long lastID = Long.MAX_VALUE;
ArrayList<Status> tweets = new ArrayList<Status>();
while (tweets.size () < numberOfTweets) {
try {
tweets.addAll(twitter.getUserTimeline(userName,pg));
System.out.println("Gathered " + tweets.size() + " tweets");
for (Status t: tweets){
if(t.getId() < lastID){
lastID = t.getId();
//System.out.println(t.getText());
User user = t.getUser();
if(t.isRetweeted()){
Status retweetedStatus = t.getRetweetedStatus();
User curRTUser = retweetedStatus.getUser();
//long curRTUserID = curRTUser.getId();
String convertedText =
textToHtmlConvertingURLsToLinks(
t.getText()
);
// needs to contain a link inside
if(convertedText.contains("<a") == false){
continue;
}
result += ""
+ doItem(
curRTUser.getProfileImageURL(),
"<b>" + curRTUser.getName() + "</b>"
+ html.br
+ convertedText
+ html.br )
+ "\n";
}else{
//result += t.getText() + "\n";
String convertedText =
textToHtmlConvertingURLsToLinks(
t.getText()
);
result += ""
+ doItem(
user.getProfileImageURL(),
"<b>" + user.getName() + "</b>"
+ html.br
+ convertedText
+ html.br )
+ "\n";
}
}
}
}
catch (TwitterException te) {
System.out.println("Couldn't connect: " + te);
return null;
}
}
pg.setMaxId(lastID-1);
// save the results to disk (ensure faster start within 24 hours)
utils.files.SaveStringToFile(file, result);
return result;
}
}