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