java-gnome

java-gnome

Pour les besoins d’une conférence sur Apache Camel, je trouvais sympa d’afficher les live-tweets sous forme de notification Ubuntu pendant le déroulement des slides. (J’avais trouvé l’idée du Tweet-wall du DevoXX sympa) En creusant un peu, je suis tombé sur la librairie GTK.jar fournie par java-gnome.

Pré-requis

Tout d’abord, il vous faudra installer le paquet libjava-gnome-java :

1
sudo apt-get install libjava-gnome-java

Twitter API

Les recherche de hashtags, de mots clefs ou de compte se fait assez simplement grâce à l’API Twitter. Par exemple : http://search.twitter.com/search.json?q=%23java (Pour info %23 = # et %40 = @) La réponse est produite en Json dans notre cas.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"completed_in":0.031,
"max_id":312144570609721344,
"max_id_str":"312144570609721344",
"next_page":"?page=2&max_id=312144570609721344&q=%23java",
"page":1,
"query":"%23java",
"refresh_url":"?since_id=312144570609721344&q=%23java",
"results":[
{
"created_at":"Thu, 14 Mar 2013 10:13:59 +0000",
"from_user":"p2pWebMobileIt",
"from_user_id":551581457,
"from_user_id_str":"551581457",
"from_user_name":"p2p WebMobileIT",
"geo":null,
"id":312144570609721344,
"id_str":"312144570609721344",
"iso_language_code":"en",
"metadata":{
"result_type":"recent"
},
"profile_image_url":"http://a0.twimg.com/profile_images/2099631495/p2people_logo_48x48_normal.png",
"profile_image_url_https":"https://si0.twimg.com/profile_images/2099631495/p2people_logo_48x48_normal.png",
"source":"<a href="http://www.p2people.co.uk">p2people</a>",
"text":"Java Developer - Norwegian/Danish/Swedish speaker w/ #Developer #Java skills Oslo @p2people http://t.co/ETi1qRMz5k"
},
[...]
],
"results_per_page":15,
"since_id":0,
"since_id_str":"0"
}

Une fois la requête effectuée, nous pouvons mapper cette structure Json sur un MOO java avec GSon par exemple. Il s’agit de la première requête. On observe un attribut intéressant : “refresh_url”, ça va nous donner la requête suivante pour n’avoir que le delta : http://search.twitter.com/search.json?since_id=312144570609721344&q=%23java

La notification

Voici une version simplifiée :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import org.gnome.gdk.Pixbuf;
import org.gnome.gtk.Gtk;
import org.gnome.notify.Notification;
import org.gnome.notify.Notify;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.WordUtils;
import org.apache.commons.lang.time.DateFormatUtils;

public class FeedShow {
public static void main(final String\[\] args) {
Gtk.init(null);
Notify.init("Ubuntu TweetFeed");
TwittBean twitt = ... // récupération du Tweet à partir du Json
final String imgurl = URLDecoder.decode(twitt.getProfile\_image\_url(), "UTF-8");
final Notification notif = new Notification(
DateFormatUtils.format(
SDF.parse(twitt.getCreated\_at()),
"EEE, d MMM yyyy HH:mm:ss", Locale.FRENCH),
twitt.getFrom\_user\_name() + " (@"
+ twitt.getFrom\_user() + ") :\\n" + WordUtils.wrap(twitt.getText(), 50), "");
try {
p = getIcon(imgurl);
notif.setIcon(p);
} catch (final IOException e) {
e.printStackTrace();
}
notif.show();
Thread.sleep(1000);
notif.close();
}

private static Pixbuf getIcon(final String imgurl) throws IOException {
final String fileName = FilenameUtils.getName(imgurl);
final File local = new File(System.getProperty("java.io.tmpdir") + File.separator + fileName);
if (!local.exists()) {
final URL url = new URL(imgurl);
FileUtils.copyURLToFile(url, local);
}
final Pixbuf pic = new Pixbuf(local.getAbsolutePath());
return pic;
}
}

Conclusion

Trop facile :) Les sources sont disponibles ici : http://giwi.free.fr/docs/Ubuntu-tweedFeed/.

Commentaires