jtulach@0: package cz.xelfi.feedbook; jtulach@0: jtulach@1: import com.google.code.facebookapi.FacebookException; jtulach@0: import com.google.code.facebookapi.FacebookJaxbRestClient; jtulach@0: import com.google.code.facebookapi.Permission; jtulach@1: import com.sun.syndication.feed.rss.Channel; jtulach@1: import com.sun.syndication.feed.rss.Item; jtulach@1: import com.sun.syndication.io.WireFeedInput; jtulach@4: import java.awt.Desktop; jtulach@1: import java.io.IOException; jtulach@0: import java.net.URI; jtulach@1: import java.net.URISyntaxException; jtulach@1: import java.net.URL; jtulach@1: import java.util.prefs.Preferences; jtulach@1: import org.xml.sax.InputSource; jtulach@0: jtulach@0: public class Main { jtulach@0: private static final String APP_KEY = "ecdbd5dbacc168f9edfe470ccd7e401b"; jtulach@0: private static final String APP_SEC = "22e3165366e958a6e049e1e0c1d30105"; jtulach@0: jtulach@1: jtulach@1: private static String login() throws IOException, FacebookException, URISyntaxException { jtulach@0: FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC); jtulach@0: final String token = login.auth_createToken(); jtulach@0: URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token); jtulach@1: System.out.println("authentication: " + u); jtulach@4: try { jtulach@4: Desktop.getDesktop().browse(u); jtulach@4: } catch (Exception ex) { jtulach@4: Runtime.getRuntime().exec("xdg-open " + u); jtulach@4: } jtulach@0: System.out.println("Visit the browser and press enter..."); jtulach@0: System.in.read(); jtulach@0: jtulach@3: return login.auth_getSession(token, false); jtulach@1: } jtulach@1: jtulach@1: public static void main( String[] args) throws Exception { jtulach@1: WireFeedInput rssInput = new WireFeedInput(); jtulach@1: URL rssUrl = new URL("http://blogs.apidesign.org/rss/"); jtulach@1: Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));; jtulach@0: jtulach@1: Preferences prefs = Preferences.userNodeForPackage(Main.class); jtulach@1: jtulach@1: FacebookJaxbRestClient fb; jtulach@3: Long user; jtulach@1: for (;;) { jtulach@1: final String stored = prefs.get("session", null); jtulach@1: if (stored != null) { jtulach@3: try { jtulach@3: fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored); jtulach@3: user = fb.users_getLoggedInUser(); jtulach@3: break; jtulach@3: } catch (FacebookException ex) { jtulach@3: System.err.println(ex.getMessage()); jtulach@3: } jtulach@1: } jtulach@1: final String session = login(); jtulach@1: fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session); jtulach@3: user = fb.users_getLoggedInUser(); jtulach@1: prefs.put("session", session); jtulach@1: break; jtulach@1: } jtulach@0: jtulach@0: if (fb.users_hasAppPermission(Permission.SHARE_ITEM)) { jtulach@2: Preferences n = prefs.node("feeds"); jtulach@2: for (Object o : rss.getItems()) { jtulach@2: Item item = (Item) o; jtulach@2: String hex = Integer.toHexString(item.getLink().hashCode()); jtulach@2: if (n.getLong(hex, -1L) != -1L) { jtulach@2: continue; jtulach@2: } jtulach@2: System.err.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle()); jtulach@2: Long ok = fb.links_post(user, item.getLink(), item.getTitle()); jtulach@2: System.out.println("posted as " + ok); jtulach@2: n.putLong(hex, ok); jtulach@2: n.put(hex + ".url", item.getLink()); jtulach@2: } jtulach@0: } else { jtulach@0: System.out.println("No permission to share links"); jtulach@0: } jtulach@0: } jtulach@0: }