src/main/java/cz/xelfi/feedbook/Main.java
changeset 1 93c350696ff6
parent 0 258dc96b1636
child 2 21a08b9be14a
     1.1 --- a/src/main/java/cz/xelfi/feedbook/Main.java	Tue Nov 09 21:01:16 2010 +0100
     1.2 +++ b/src/main/java/cz/xelfi/feedbook/Main.java	Tue Nov 09 23:52:40 2010 +0100
     1.3 @@ -1,40 +1,66 @@
     1.4  package cz.xelfi.feedbook;
     1.5  
     1.6 +import com.google.code.facebookapi.FacebookException;
     1.7  import com.google.code.facebookapi.FacebookJaxbRestClient;
     1.8  import com.google.code.facebookapi.Permission;
     1.9  import com.google.code.facebookapi.schema.FriendsGetResponse;
    1.10  import com.google.code.facebookapi.schema.User;
    1.11  import com.google.code.facebookapi.schema.UsersGetInfoResponse;
    1.12 +import com.sun.syndication.feed.rss.Channel;
    1.13 +import com.sun.syndication.feed.rss.Item;
    1.14 +import com.sun.syndication.io.WireFeedInput;
    1.15 +import java.io.IOException;
    1.16  import java.net.URI;
    1.17 +import java.net.URISyntaxException;
    1.18 +import java.net.URL;
    1.19  import java.util.Collections;
    1.20  import java.util.List;
    1.21 +import java.util.prefs.Preferences;
    1.22 +import org.xml.sax.InputSource;
    1.23  
    1.24  public class Main {
    1.25      private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b";
    1.26      private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105";
    1.27      
    1.28 -    public static void main( String[] args) throws Exception {
    1.29 +    
    1.30 +    private static String login() throws IOException, FacebookException, URISyntaxException {
    1.31          FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
    1.32          final String token = login.auth_createToken();
    1.33          URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
    1.34 +        System.out.println("authentication: " + u);
    1.35          Runtime.getRuntime().exec("xdg-open " + u);
    1.36 -        /*
    1.37 -        InputStream is = u.toURL().openStream();
    1.38 -        for (;;) {
    1.39 -            int ch = is.read();
    1.40 -            if (ch == -1) {
    1.41 -                break;
    1.42 -            }
    1.43 -            System.out.write(ch);
    1.44 -        }
    1.45 -         */
    1.46          //Desktop.getDesktop().browse(u);
    1.47          System.out.println("Visit the browser and press enter...");
    1.48          System.in.read();
    1.49          
    1.50 -        final String session = login.auth_getSession(token, true); 
    1.51 +        return login.auth_getSession(token, true); 
    1.52 +    }
    1.53 +    
    1.54 +    public static void main( String[] args) throws Exception {
    1.55 +        WireFeedInput rssInput = new WireFeedInput();
    1.56 +        URL rssUrl = new URL("http://blogs.apidesign.org/rss/");
    1.57 +        Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
    1.58 +        System.err.printf("stream from %s\n", rss.getUri());
    1.59 +        for (Object o : rss.getItems()) {
    1.60 +            Item item = (Item)o;
    1.61 +            
    1.62 +            System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
    1.63 +        }
    1.64          
    1.65 -        FacebookJaxbRestClient fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
    1.66 +        Preferences prefs = Preferences.userNodeForPackage(Main.class);
    1.67 +        
    1.68 +        FacebookJaxbRestClient fb;
    1.69 +        for (;;) {
    1.70 +            final String stored = prefs.get("session", null);
    1.71 +            if (stored != null) {
    1.72 +                fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
    1.73 +                break;
    1.74 +            }
    1.75 +            final String session = login();
    1.76 +            fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
    1.77 +            prefs.put("session", session);
    1.78 +            break;
    1.79 +        }
    1.80          Long user = fb.users_getLoggedInUser();
    1.81          final FriendsGetResponse ret = fb.friends_get(user);
    1.82          final UsersGetInfoResponse info = fb.users_getInfo(ret.getUid(), Collections.<CharSequence>singleton("last_name"));
    1.83 @@ -48,7 +74,7 @@
    1.84          String cmd = "Testing new application";
    1.85  
    1.86          if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
    1.87 -            Long ok = fb.links_post(user, url, cmd);
    1.88 +            Long ok = 1L; //fb.links_post(user, url, cmd);
    1.89              System.out.println("posted as " + ok);
    1.90          } else {
    1.91              System.out.println("No permission to share links");