src/main/java/cz/xelfi/feedbook/Main.java
author Jaroslav Tulach <jtulach@netbeans.org>
Wed, 10 Nov 2010 08:53:58 +0100
changeset 3 ba411f9d13e5
parent 2 21a08b9be14a
child 4 c366470a05e1
permissions -rw-r--r--
Survive when the session key is no longer valid
     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.io.IOException;
    10 import java.net.URI;
    11 import java.net.URISyntaxException;
    12 import java.net.URL;
    13 import java.util.prefs.Preferences;
    14 import org.xml.sax.InputSource;
    15 
    16 public class Main {
    17     private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
    18     private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
    19     
    20     
    21     private static String login() throws IOException, FacebookException, URISyntaxException {
    22         FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
    23         final String token = login.auth_createToken();
    24         URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
    25         System.out.println("authentication: " + u);
    26         Runtime.getRuntime().exec("xdg-open " + u);
    27         //Desktop.getDesktop().browse(u);
    28         System.out.println("Visit the browser and press enter...");
    29         System.in.read();
    30         
    31         return login.auth_getSession(token, false); 
    32     }
    33     
    34     public static void main( String[] args) throws Exception {
    35         WireFeedInput rssInput = new WireFeedInput();
    36         URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
    37         Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
    38         
    39         Preferences prefs = Preferences.userNodeForPackage(Main.class);
    40         
    41         FacebookJaxbRestClient fb;
    42         Long user;
    43         for (;;) {
    44             final String stored = prefs.get("session", null);
    45             if (stored != null) {
    46                 try {
    47                     fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
    48                     user = fb.users_getLoggedInUser();
    49                     break;
    50                 } catch (FacebookException ex) {
    51                     System.err.println(ex.getMessage());
    52                 }
    53             }
    54             final String session = login();
    55             fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
    56             user = fb.users_getLoggedInUser();
    57             prefs.put("session", session);
    58             break;
    59         }
    60         
    61         if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    62             Preferences n = prefs.node("feeds");
    63             for (Object o : rss.getItems()) {
    64                 Item item = (Item) o;
    65                 String hex = Integer.toHexString(item.getLink().hashCode());
    66                 if (n.getLong(hex, -1L) != -1L) {
    67                     continue;
    68                 }
    69                 System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
    70                 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
    71                 System.out.println("posted as " + ok);
    72                 n.putLong(hex, ok);
    73                 n.put(hex + ".url", item.getLink());
    74             }
    75         } else {
    76             System.out.println("No permission to share links");
    77         }
    78     }
    79 }