Switch to unified view

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