Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * Contributors: * ybonnel - initial API and implementation */ package utils; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import models.LastUpdate; import models.MessageTwitter; import twitter4j.ResponseList; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.auth.AccessToken; import twitter4j.conf.ConfigurationBuilder; public class GetTwitters { private static final Logger LOGGER = Logger.getLogger(GetTwitters.class.getName()); private String compte; public GetTwitters(String compte) { this.compte = compte; } private static TwitterFactory twitterFactory; private static synchronized TwitterFactory getFactory() { if (twitterFactory == null) { ConfigurationBuilder builder = new ConfigurationBuilder(); builder.setUseSSL(true); twitterFactory = new TwitterFactory(builder.build()); } return twitterFactory; } public Iterable<MessageTwitter> getMessages() { List<MessageTwitter> messages = new ArrayList<MessageTwitter>(20); if (LastUpdate.getInstance(compte).isUpdate()) { LOGGER.fine("Les messages twitter sont jour, envoie du contenu de la base de donne"); messages.addAll(MessageTwitter.findByCompte(compte)); } else { LOGGER.fine("Les messages twitter ne sont pas jour, rcupration du contenu de twiter"); Twitter twitter = getFactory().getInstance(); twitter.setOAuthConsumer("9Jsib4k1uEMCWZqEHy1t1Q", "vLQQaog60gYRrPCC2bHeEZdod3JDSkTRI9W7r2cZIZ8"); twitter.setOAuthAccessToken(new AccessToken("225864007-Y11ZtDLq2LVZwMR3anKxPW9nR6dIGkLyFlOhdAMx", "GQ16L9QMhhzSiRT4xRia7B25011BoNsXUEgUyp0vKI")); ResponseList<Status> listeStatus; try { listeStatus = twitter.getUserTimeline("@" + compte); } catch (TwitterException e) { LOGGER.log(Level.SEVERE, "Erreur lors de l'accs twitter", e); return MessageTwitter.findByCompte(compte); } for (Status status : listeStatus) { messages.add(new MessageTwitter(status.getCreatedAt(), status.getText(), compte)); } MessageTwitter.deleteAll(); for (MessageTwitter message : messages) { message.save(); } } Collections.sort(messages, new Comparator<MessageTwitter>() { public int compare(MessageTwitter o1, MessageTwitter o2) { return o2.getDateCreation().compareTo(o1.getDateCreation()); } }); return messages; } }