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