src/main/java/cz/xelfi/feedbook/Main.java
author Jaroslav Tulach <jtulach@netbeans.org>
Thu, 11 Nov 2010 09:51:16 +0100
changeset 10 febc09a6a7cf
parent 9 6a5c4c2bda38
permissions -rw-r--r--
Shortening lines included in code snippets
     1 package cz.xelfi.feedbook;
     2 
     3 import com.google.code.facebookapi.FacebookException;
     4 import com.google.code.facebookapi.FacebookJaxbRestClient;
     5 import com.google.code.facebookapi.Permission;
     6 import com.sun.syndication.feed.rss.Channel;
     7 import com.sun.syndication.feed.rss.Item;
     8 import com.sun.syndication.io.WireFeedInput;
     9 import java.awt.Desktop;
    10 import java.io.IOException;
    11 import java.net.URI;
    12 import java.net.URISyntaxException;
    13 import java.net.URL;
    14 import java.util.prefs.Preferences;
    15 import org.xml.sax.InputSource;
    16 
    17 public class Main {
    18     private static String APP_KEY;
    19     private static String APP_SEC;
    20     private static String APP_FEED;
    21     
    22     
    23     // BEGIN: feedbook-login
    24     private static String login() 
    25     throws IOException, FacebookException, URISyntaxException {
    26         FacebookJaxbRestClient login = new FacebookJaxbRestClient(
    27             APP_KEY, APP_SEC
    28         );
    29         final String token = login.auth_createToken();
    30         final URI u = new URI("http://www.facebook.com/login.php?"
    31                 + "api_key=" + APP_KEY + "&auth_token=" + token);
    32         String msg = "authentication";
    33         browser(msg, u);
    34         return login.auth_getSession(token, false); 
    35     }
    36     // END: feedbook-login
    37 
    38     private static void browser(String msg, URI u) throws IOException {
    39         System.out.println(msg + ": " + u);
    40         try {
    41             Desktop.getDesktop().browse(u);
    42         } catch (Exception ex) {
    43             Runtime.getRuntime().exec("xdg-open " + u);
    44         }
    45         System.out.println("Visit the browser and press enter...");
    46         System.in.read();
    47     }
    48     
    49     public static void main( String[] args) throws Exception {
    50         Preferences prefs = Preferences.userNodeForPackage(Main.class);
    51         if (args.length == 3) {
    52             prefs.put("id", args[0]);
    53             prefs.put("secret", args[1]);
    54             prefs.put("feed", args[2]);
    55         } else {
    56             if (args.length != 0) {
    57                 usage();
    58                 return;
    59             }
    60         }
    61         
    62         
    63         APP_KEY = prefs.get("id", null);
    64         APP_SEC = prefs.get("secret", null);
    65         APP_FEED = prefs.get("feed", null);
    66         
    67         if (APP_FEED == null || APP_SEC == null || APP_KEY == null) {
    68             usage();
    69             return;
    70         }
    71         
    72         WireFeedInput rssInput = new WireFeedInput();
    73         URL rssUrl = new URL(APP_FEED);
    74         Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
    75         
    76         
    77         FacebookJaxbRestClient fb;
    78         Long user;
    79         for (;;) {
    80             final String stored = prefs.get("session", null);
    81             if (stored != null) {
    82                 try {
    83                     fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
    84                     user = fb.users_getLoggedInUser();
    85                     break;
    86                 } catch (FacebookException ex) {
    87                     System.err.println(ex.getMessage());
    88                 }
    89             }
    90             final String session = login();
    91             fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
    92             user = fb.users_getLoggedInUser();
    93             prefs.put("session", session);
    94             break;
    95         }
    96         
    97         // BEGIN: feedbook-permission
    98         while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    99             URI u = new URI("https://graph.facebook.com/oauth/authorize?"
   100                     + "client_id=" + APP_KEY
   101                     + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
   102                     + "&scope=publish_stream");
   103             browser("Need permission to publish links", u);
   104         }
   105         // END: feedbook-permission
   106         Preferences n = prefs.node("feeds");
   107         int cnt = 0;
   108         for (Object o : rss.getItems()) {
   109             Item item = (Item) o;
   110             String hex = Integer.toHexString(item.getLink().hashCode());
   111             if (n.getLong(hex, -1L) != -1L) {
   112                 continue;
   113             }
   114             cnt++;
   115             // BEGIN: feedbook-publish-link
   116             System.out.printf(
   117                 "link: %s date: %s title: %s\n", 
   118                 item.getLink(), item.getPubDate(), item.getTitle()
   119             );
   120             Long ok = fb.links_post(user, item.getLink(), item.getTitle());
   121             System.out.println("posted as " + ok);
   122             // END: feedbook-publish-link
   123             n.putLong(hex, ok);
   124             n.put(hex + ".url", item.getLink());
   125         }
   126         System.out.println("Published " + cnt + " feed item(s)");
   127     }
   128 
   129     private static void usage() {
   130         System.out.println("Invoke with three parameters: "
   131                 + "<facebook_app_key> "
   132                 + "<facebook_app_secret "
   133                 + "<rss_feed_url>");
   134     }
   135 }