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