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() throws IOException, FacebookException, URISyntaxException {
25 FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
26 final String token = login.auth_createToken();
27 URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
28 String msg = "authentication";
30 return login.auth_getSession(token, false);
32 // END: feedbook-login
34 private static void browser(String msg, URI u) throws IOException {
35 System.out.println(msg + ": " + u);
37 Desktop.getDesktop().browse(u);
38 } catch (Exception ex) {
39 Runtime.getRuntime().exec("xdg-open " + u);
41 System.out.println("Visit the browser and press enter...");
45 public static void main( String[] args) throws Exception {
46 Preferences prefs = Preferences.userNodeForPackage(Main.class);
47 if (args.length == 3) {
48 prefs.put("id", args[0]);
49 prefs.put("secret", args[1]);
50 prefs.put("feed", args[2]);
52 if (args.length != 0) {
59 APP_KEY = prefs.get("id", null);
60 APP_SEC = prefs.get("secret", null);
61 APP_FEED = prefs.get("feed", null);
63 if (APP_FEED == null || APP_SEC == null || APP_KEY == null) {
68 WireFeedInput rssInput = new WireFeedInput();
69 URL rssUrl = new URL(APP_FEED);
70 Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
73 FacebookJaxbRestClient fb;
76 final String stored = prefs.get("session", null);
79 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
80 user = fb.users_getLoggedInUser();
82 } catch (FacebookException ex) {
83 System.err.println(ex.getMessage());
86 final String session = login();
87 fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
88 user = fb.users_getLoggedInUser();
89 prefs.put("session", session);
93 // BEGIN: feedbook-permission
94 while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
95 URI u = new URI("https://graph.facebook.com/oauth/authorize?"
96 + "client_id=" + APP_KEY
97 + "&redirect_uri=http://www.facebook.com/connect/login_success.html"
98 + "&scope=publish_stream");
99 browser("Need permission to publish links", u);
101 // END: feedbook-permission
102 Preferences n = prefs.node("feeds");
104 for (Object o : rss.getItems()) {
105 Item item = (Item) o;
106 String hex = Integer.toHexString(item.getLink().hashCode());
107 if (n.getLong(hex, -1L) != -1L) {
111 // BEGIN: feedbook-publish-link
113 "link: %s date: %s title: %s\n",
114 item.getLink(), item.getPubDate(), item.getTitle()
116 Long ok = fb.links_post(user, item.getLink(), item.getTitle());
117 System.out.println("posted as " + ok);
118 // END: feedbook-publish-link
120 n.put(hex + ".url", item.getLink());
122 System.out.println("Published " + cnt + " feed item(s)");
125 private static void usage() {
126 System.out.println("Invoke with three parameters: "
127 + "<facebook_app_key> "
128 + "<facebook_app_secret "