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 // BEGIN: feedbook-login
24 private static String login()
25 throws IOException, FacebookException, URISyntaxException {
26 FacebookJaxbRestClient login = new FacebookJaxbRestClient(
29 final String token = login.auth_createToken();
30 final URI u = new URI("http://www.facebook.com/login.php?"
31 + "api_key=" + APP_KEY + "&auth_token=" + token);
32 String msg = "authentication";
34 return login.auth_getSession(token, false);
36 // END: feedbook-login
38 private static void browser(String msg, URI u) throws IOException {
39 System.out.println(msg + ": " + u);
41 Desktop.getDesktop().browse(u);
42 } catch (Exception ex) {
43 Runtime.getRuntime().exec("xdg-open " + u);
45 System.out.println("Visit the browser and press enter...");
49 public static void main( String[] args) throws Exception {
50 Preferences prefs = Preferences.userNodeForPackage(Main.class);
51 if (args.length == 3) {
52 prefs.put("id", args[0]);
53 prefs.put("secret", args[1]);
54 prefs.put("feed", args[2]);
56 if (args.length != 0) {
63 APP_KEY = prefs.get("id", null);
64 APP_SEC = prefs.get("secret", null);
65 APP_FEED = prefs.get("feed", null);
67 if (APP_FEED == null || APP_SEC == null || APP_KEY == null) {
72 WireFeedInput rssInput = new WireFeedInput();
73 URL rssUrl = new URL(APP_FEED);
74 Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
77 FacebookJaxbRestClient fb;
80 final String stored = prefs.get("session", null);
83 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
84 user = fb.users_getLoggedInUser();
86 } catch (FacebookException ex) {
87 System.err.println(ex.getMessage());
90 final String session = login();
91 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
92 user = fb.users_getLoggedInUser();
93 prefs.put("session", session);
97 // BEGIN: feedbook-permission
98 while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
99 URI u = new URI("https://graph.facebook.com/oauth/authorize?"
100 + "client_id=" + APP_KEY
101 + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
102 + "&scope=publish_stream");
103 browser("Need permission to publish links", u);
105 // END: feedbook-permission
106 Preferences n = prefs.node("feeds");
108 for (Object o : rss.getItems()) {
109 Item item = (Item) o;
110 String hex = Integer.toHexString(item.getLink().hashCode());
111 if (n.getLong(hex, -1L) != -1L) {
115 // BEGIN: feedbook-publish-link
117 "link: %s date: %s title: %s\n",
118 item.getLink(), item.getPubDate(), item.getTitle()
120 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
121 System.out.println("posted as " + ok);
122 // END: feedbook-publish-link
124 n.put(hex + ".url", item.getLink());
126 System.out.println("Published " + cnt + " feed item(s)");
129 private static void usage() {
130 System.out.println("Invoke with three parameters: "
131 + "<facebook_app_key> "
132 + "<facebook_app_secret "