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