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