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@1
|
6 |
import com.sun.syndication.feed.rss.Channel;
|
jtulach@1
|
7 |
import com.sun.syndication.feed.rss.Item;
|
jtulach@1
|
8 |
import com.sun.syndication.io.WireFeedInput;
|
jtulach@4
|
9 |
import java.awt.Desktop;
|
jtulach@1
|
10 |
import java.io.IOException;
|
jtulach@0
|
11 |
import java.net.URI;
|
jtulach@1
|
12 |
import java.net.URISyntaxException;
|
jtulach@1
|
13 |
import java.net.URL;
|
jtulach@1
|
14 |
import java.util.prefs.Preferences;
|
jtulach@1
|
15 |
import org.xml.sax.InputSource;
|
jtulach@0
|
16 |
|
jtulach@0
|
17 |
public class Main {
|
jtulach@7
|
18 |
private static String APP_KEY;
|
jtulach@7
|
19 |
private static String APP_SEC;
|
jtulach@7
|
20 |
private static String APP_FEED;
|
jtulach@0
|
21 |
|
jtulach@1
|
22 |
|
jtulach@1
|
23 |
private static String login() throws IOException, FacebookException, URISyntaxException {
|
jtulach@0
|
24 |
FacebookJaxbRestClient login = new FacebookJaxbRestClient(APP_KEY, APP_SEC);
|
jtulach@0
|
25 |
final String token = login.auth_createToken();
|
jtulach@0
|
26 |
URI u = new URI("http://www.facebook.com/login.php?api_key=" + APP_KEY + "&auth_token=" + token);
|
jtulach@5
|
27 |
String msg = "authentication";
|
jtulach@5
|
28 |
browser(msg, u);
|
jtulach@5
|
29 |
return login.auth_getSession(token, false);
|
jtulach@5
|
30 |
}
|
jtulach@5
|
31 |
|
jtulach@5
|
32 |
private static void browser(String msg, URI u) throws IOException {
|
jtulach@5
|
33 |
System.out.println(msg + ": " + u);
|
jtulach@4
|
34 |
try {
|
jtulach@4
|
35 |
Desktop.getDesktop().browse(u);
|
jtulach@4
|
36 |
} catch (Exception ex) {
|
jtulach@4
|
37 |
Runtime.getRuntime().exec("xdg-open " + u);
|
jtulach@4
|
38 |
}
|
jtulach@0
|
39 |
System.out.println("Visit the browser and press enter...");
|
jtulach@0
|
40 |
System.in.read();
|
jtulach@1
|
41 |
}
|
jtulach@1
|
42 |
|
jtulach@1
|
43 |
public static void main( String[] args) throws Exception {
|
jtulach@7
|
44 |
Preferences prefs = Preferences.userNodeForPackage(Main.class);
|
jtulach@7
|
45 |
if (args.length == 3) {
|
jtulach@7
|
46 |
prefs.put("id", args[0]);
|
jtulach@7
|
47 |
prefs.put("secret", args[1]);
|
jtulach@7
|
48 |
prefs.put("feed", args[2]);
|
jtulach@7
|
49 |
} else {
|
jtulach@7
|
50 |
if (args.length != 0) {
|
jtulach@7
|
51 |
usage();
|
jtulach@7
|
52 |
return;
|
jtulach@7
|
53 |
}
|
jtulach@7
|
54 |
}
|
jtulach@7
|
55 |
|
jtulach@7
|
56 |
|
jtulach@7
|
57 |
APP_KEY = prefs.get("id", null);
|
jtulach@7
|
58 |
APP_SEC = prefs.get("secret", null);
|
jtulach@7
|
59 |
APP_FEED = prefs.get("feed", null);
|
jtulach@7
|
60 |
|
jtulach@7
|
61 |
if (APP_FEED == null || APP_SEC == null || APP_KEY == null) {
|
jtulach@7
|
62 |
usage();
|
jtulach@7
|
63 |
return;
|
jtulach@7
|
64 |
}
|
jtulach@7
|
65 |
|
jtulach@1
|
66 |
WireFeedInput rssInput = new WireFeedInput();
|
jtulach@7
|
67 |
URL rssUrl = new URL(APP_FEED);
|
jtulach@1
|
68 |
Channel rss = (Channel)rssInput.build(new InputSource(rssUrl.openStream()));;
|
jtulach@0
|
69 |
|
jtulach@1
|
70 |
|
jtulach@1
|
71 |
FacebookJaxbRestClient fb;
|
jtulach@3
|
72 |
Long user;
|
jtulach@1
|
73 |
for (;;) {
|
jtulach@1
|
74 |
final String stored = prefs.get("session", null);
|
jtulach@1
|
75 |
if (stored != null) {
|
jtulach@3
|
76 |
try {
|
jtulach@3
|
77 |
fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, stored);
|
jtulach@3
|
78 |
user = fb.users_getLoggedInUser();
|
jtulach@3
|
79 |
break;
|
jtulach@3
|
80 |
} catch (FacebookException ex) {
|
jtulach@3
|
81 |
System.err.println(ex.getMessage());
|
jtulach@3
|
82 |
}
|
jtulach@1
|
83 |
}
|
jtulach@1
|
84 |
final String session = login();
|
jtulach@1
|
85 |
fb = new FacebookJaxbRestClient(APP_KEY, APP_SEC, session);
|
jtulach@3
|
86 |
user = fb.users_getLoggedInUser();
|
jtulach@1
|
87 |
prefs.put("session", session);
|
jtulach@1
|
88 |
break;
|
jtulach@1
|
89 |
}
|
jtulach@0
|
90 |
|
jtulach@5
|
91 |
while (!fb.users_hasAppPermission(Permission.SHARE_ITEM)) {
|
jtulach@5
|
92 |
URI u = new URI("https://graph.facebook.com/oauth/authorize?"
|
jtulach@5
|
93 |
+ "client_id=" + APP_KEY
|
jtulach@5
|
94 |
+ "&redirect_uri=http://www.facebook.com/connect/login_success.html"
|
jtulach@5
|
95 |
+ "&scope=publish_stream");
|
jtulach@5
|
96 |
browser("Need permission to publish links", u);
|
jtulach@5
|
97 |
}
|
jtulach@5
|
98 |
Preferences n = prefs.node("feeds");
|
jtulach@6
|
99 |
int cnt = 0;
|
jtulach@5
|
100 |
for (Object o : rss.getItems()) {
|
jtulach@5
|
101 |
Item item = (Item) o;
|
jtulach@5
|
102 |
String hex = Integer.toHexString(item.getLink().hashCode());
|
jtulach@5
|
103 |
if (n.getLong(hex, -1L) != -1L) {
|
jtulach@5
|
104 |
continue;
|
jtulach@2
|
105 |
}
|
jtulach@6
|
106 |
cnt++;
|
jtulach@6
|
107 |
System.out.printf("link: %s date: %s title: %s\n", item.getLink(), item.getPubDate(), item.getTitle());
|
jtulach@5
|
108 |
Long ok = fb.links_post(user, item.getLink(), item.getTitle());
|
jtulach@5
|
109 |
System.out.println("posted as " + ok);
|
jtulach@5
|
110 |
n.putLong(hex, ok);
|
jtulach@5
|
111 |
n.put(hex + ".url", item.getLink());
|
jtulach@0
|
112 |
}
|
jtulach@6
|
113 |
System.out.println("Published " + cnt + " feed item(s)");
|
jtulach@0
|
114 |
}
|
jtulach@7
|
115 |
|
jtulach@7
|
116 |
private static void usage() {
|
jtulach@7
|
117 |
System.out.println("Invoke with three parameters: "
|
jtulach@7
|
118 |
+ "<facebook_app_key> "
|
jtulach@7
|
119 |
+ "<facebook_app_secret "
|
jtulach@7
|
120 |
+ "<rss_feed_url>");
|
jtulach@7
|
121 |
}
|
jtulach@0
|
122 |
}
|