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