1 package cz.xelfi.feedbook;
3 import com.google.code.facebookapi.FacebookException;
4 import com.google.code.facebookapi.FacebookJaxbRestClient;
5 import com.google.code.facebookapi.Permission;
6 import com.sun.syndication.feed.rss.Channel;
7 import com.sun.syndication.feed.rss.Item;
8 import com.sun.syndication.io.WireFeedInput;
9 import java.awt.Desktop;
10 import java.io.IOException;
12 import java.net.URISyntaxException;
14 import java.util.prefs.Preferences;
15 import org.xml.sax.InputSource;
18 private static String APP_KEY;
19 private static String APP_SEC;
20 private static String APP_FEED;
23 private static String login() throws IOException, FacebookException, URISyntaxException {
24 FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
25 final String token = login.auth_createToken();
26 URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
27 String msg = "authentication";
29 return login.auth_getSession(token, false);
32 private static void browser(String msg, URI u) throws IOException {
33 System.out.println(msg + ": " + u);
35 Desktop.getDesktop().browse(u);
36 } catch (Exception ex) {
37 Runtime.getRuntime().exec("xdg-open " + u);
39 System.out.println("Visit the browser and press enter...");
43 public static void main( String[] args) throws Exception {
44 Preferences prefs = Preferences.userNodeForPackage(Main.class);
45 if (args.length == 3) {
46 prefs.put("id", args[0]);
47 prefs.put("secret", args[1]);
48 prefs.put("feed", args[2]);
50 if (args.length != 0) {
57 APP_KEY = prefs.get("id", null);
58 APP_SEC = prefs.get("secret", null);
59 APP_FEED = prefs.get("feed", null);
61 if (APP_FEED == null || APP_SEC == null || APP_KEY == null) {
66 WireFeedInput rssInput = new WireFeedInput();
67 URL rssUrl = new URL(APP_FEED);
68 Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
71 FacebookJaxbRestClient fb;
74 final String stored = prefs.get("session", null);
77 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
78 user = fb.users_getLoggedInUser();
80 } catch (FacebookException ex) {
81 System.err.println(ex.getMessage());
84 final String session = login();
85 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
86 user = fb.users_getLoggedInUser();
87 prefs.put("session", session);
91 while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
92 URI u = new URI("https://graph.facebook.com/oauth/authorize?"
93 + "client_id=" + APP_KEY
94 + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
95 + "&scope=publish_stream");
96 browser("Need permission to publish links", u);
98 Preferences n = prefs.node("feeds");
100 for (Object o : rss.getItems()) {
101 Item item = (Item) o;
102 String hex = Integer.toHexString(item.getLink().hashCode());
103 if (n.getLong(hex, -1L) != -1L) {
107 System.out.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
108 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
109 System.out.println("posted as " + ok);
111 n.put(hex + ".url", item.getLink());
113 System.out.println("Published " + cnt + " feed item(s)");
116 private static void usage() {
117 System.out.println("Invoke with three parameters: "
118 + "<facebook_app_key> "
119 + "<facebook_app_secret "