src/main/java/cz/xelfi/feedbook/Main.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 10 Nov 2010 17:48:58 +0100
changeset 6 cb3f8481a580
parent 5 e1d564765efd
child 7 a2bb3db4c9b6
permissions -rw-r--r--
Print the number of published feeds
     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 final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
    19     private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
    20     
    21     
    22     private static String login() throws IOException, FacebookException, URISyntaxException {
    23         FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
    24         final String token = login.auth_createToken();
    25         URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
    26         String msg = "authentication";
    27         browser(msg, u);
    28         return login.auth_getSession(token, false); 
    29     }
    30 
    31     private static void browser(String msg, URI u) throws IOException {
    32         System.out.println(msg + ": " + u);
    33         try {
    34             Desktop.getDesktop().browse(u);
    35         } catch (Exception ex) {
    36             Runtime.getRuntime().exec("xdg-open " + u);
    37         }
    38         System.out.println("Visit the browser and press enter...");
    39         System.in.read();
    40     }
    41     
    42     public static void main( String[] args) throws Exception {
    43         WireFeedInput rssInput = new WireFeedInput();
    44         URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
    45         Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
    46         
    47         Preferences prefs = Preferences.userNodeForPackage(Main.class);
    48         
    49         FacebookJaxbRestClient fb;
    50         Long user;
    51         for (;;) {
    52             final String stored = prefs.get("session", null);
    53             if (stored != null) {
    54                 try {
    55                     fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
    56                     user = fb.users_getLoggedInUser();
    57                     break;
    58                 } catch (FacebookException ex) {
    59                     System.err.println(ex.getMessage());
    60                 }
    61             }
    62             final String session = login();
    63             fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
    64             user = fb.users_getLoggedInUser();
    65             prefs.put("session", session);
    66             break;
    67         }
    68         
    69         while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    70             URI u = new URI("https://graph.facebook.com/oauth/authorize?"
    71                     + "client_id=" + APP_KEY
    72                     + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
    73                     + "&scope=publish_stream");
    74             browser("Need permission to publish links", u);
    75         }
    76         Preferences n = prefs.node("feeds");
    77         int cnt = 0;
    78         for (Object o : rss.getItems()) {
    79             Item item = (Item) o;
    80             String hex = Integer.toHexString(item.getLink().hashCode());
    81             if (n.getLong(hex, -1L) != -1L) {
    82                 continue;
    83             }
    84             cnt++;
    85             System.out.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
    86             Long ok = fb.links_post(user, item.getLink(), item.getTitle());
    87             System.out.println("posted as " + ok);
    88             n.putLong(hex, ok);
    89             n.put(hex + ".url", item.getLink());
    90         }
    91         System.out.println("Published " + cnt + " feed item(s)");
    92     }
    93 }