Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Principal; import BaseDatos.BD; import Modelo.ObjetoBuscar; import Modelo.ObjetoBuscarFacebook; import Modelo.ObjetoBuscarTwitter; import java.io.PrintStream; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.cert.X509Certificate; import java.util.ArrayList; import java.util.Date; import java.util.List; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; import twitter4j.Paging; import twitter4j.ResponseList; import twitter4j.Status; import twitter4j.Twitter; import twitter4j.TwitterException; import twitter4j.TwitterFactory; import twitter4j.User; import twitter4j.conf.ConfigurationBuilder; public class Tracker_Twitter { static String[] consumerKey = { "I9amMhJUguVrbQs4DbDvpSx5J", "Kgu7AW8TqK88Ze5KGVwjSw", "ZM6O6Ofxd0osZqogOhhBw", "oq23WsoslLpokNPVI2vYpQ", "YvlNdbf8Nd8LW1nnGTrfg", "dUWjimtrzu3Q6i5wtEhkA", "czgH7owHAEg5kDWEE2ng", "0ieEt58BcKkkyFiRkp7MA", "miYXVZ275Czgb86DwAxXbQ", "NgorDpyZX2bvr1FgtHuOQ", "Y9UUTKxxRfYuzoUq9IjnQ", "iFOV3U2XQQF9e9iZGtIjeQ", "75PEBRIf5FQ5d4LhYolkg", "R5IQdPITtAhDPFga5ERJPw", "TnXyfrZftvHl5WPlwHnbw", "bpInB1IIutUInFlimBNySQ", "AVgd0w2BBwh3QIGSd2wriA", "kU0Ik0BoSEAgweDARrio4w", "JDLfHRV4L32xSuCrJO8P3w", "y5oM7c9uagsPQUhmOzgHYA", "cCGmZC0sygibb5IwIiR2g", "1lFtA5sb2GCimqonloK3bg", "DIKKNSXVJP8sDyp9OlZXvA", "8TtDSKBjq65AAecPW0yygw", "DTZ1LkxcZ0Sfms1pLRyY4w", "2mN8uxyUs8x4qaRJ4sQkA", "57orBz1WUkm3M8SOneF7zA", "GgcY2n84lYq7fOoe854UMQ", "7OROHA8dyXyMTMvdE2jtPw", "9GCnW3suBYHokHqPhpHVw" }; String[] consumerSecret = { "3cOmWWAeu77dDk5xzUUZeH5Pcz5PIWPnqMCvgiGV0jmrnglYtb", "hRCaj7J0ghCDVQNSSeVv3VbC4cZzpseUF3pVtpUOO40", "d7b1hpqFgU4vm0sTLiFK762gy6pK1Pv2vQLM2roFf4g", "hF2AxK9WQ9hNdgazcvJuJNqKNa2z2yPUP54ifC7eN6g", "eanDMDs1d7ub6k3YnninXvTksObdmF3aYKFZJKFM", "YyusmWRzieS5KmVx9ueL3YisuhiFn6pzOXOAt1UE9I", "tI0V6MAZSesMHUmCE0rbYmL61qxiEOirq6htPBKUJA", "wWRiecVZQ23HdG6rauVEx1ohWTjKKMPnBbAXcLo3o", "G5K1rNeIeaJvFFFypFXhvBe8XNhUHcXkWw8q3A5Th4", "eMuVegJ3nK2mI2yDjKORxg8A9VEVHWmGbec0ds3LQ", "WWMrkhdpx2VHEpYpJZoSqGf8Q5axxGnYC5soSF0mlQ", "diyz7jfPOjeL0M9OTrjIcep750l7K8j6K2urmVePvPg", "H0q3XE07aqr1rc8oIdPwsPpoZbMB2vACRVywWKlyA", "poPWRjpTxduk02fjcGRN1LDCdxOh5FVPTI6p9ujwdE", "rOMV7nvjozMoOydUGxcN9GsyfXVnlaqxFuPgCLkDvKo", "ubiat67N1hDGJs75BM3OPPQjJ41TlotwET7AR0Rqw", "ShCgrHZ3u9IAsqsg4bsBdB0zywOR3Bd867hyzJNnU", "jNWobssmjq4dNht7rL6lrNHifKhhqlDA4pNGR42rY", "ZyfkAXofa0nohq8wKPBRWUT6fiBZPeXmsAzyxuZnqE", "RQ8NV3pdhisoj5qClT385JrQKnL0rITs9E6nL9Xpfc", "ifXC2HlyUvv6WJeduKBe7jX9Uvlj7MYe6xAWxpoLM", "Lf5dgI4kw8456nC8kj8RwCtsUVouoR13iXbG0ZgTJY", "AmlTUzUxjjeW8YRfWOVfqVNHne3R4OAkkZT66CC9o", "ov6FaCkOL1mDAjiaTCoTUlUzrC0W0th5QP5aVZshJM", "WgMga9Z0LG8CWKUppny8RDTGfXTIlw9o4VjEkZmc2k0", "DS6UDFn9lUpbPptU4Q3C7N2lRRXr1G0uUk1pGydYQ4", "gw25cszgSHS9azx581BeKE6hUHOh9KXwdotbBxOLpk", "7CSwHP7sbGCQX2569Oume1xI7ELXWiYROBSsUPcOC4M", "uKVrLsVwksZOEQKEdCxqZ71bLPo2l9riakEMNaw", "igKsQDHO1dAyr68i9TBsEjnV2pEOwfgsuhEfkliHD0" }; String[] token = { "1949832577-3XMfgWjTCWK0rndHMbGHHn8Q1zLDXWQfe4U0Rst", "1949832577-0ZsJ8FPDxAWGzTEC5mi2JbjWeOZn6zFESXLGHXp", "1949832577-NY7mFrW5b5nW0EEqeN0SxXoJHdpQFXSMscq6PEV", "1949832577-qBGLgciUJEBC2l0uGywHYhcpfV3c2kQxXZy51gz", "1949832577-9Cc4N8L3VicqdOJMFzPnt8ELet9bPaGrDjrPye1", "1949832577-MagvL6xqRnvmSkHKLmXmPSFmHsgxTTXY1ALYIZb", "1949832577-PKiYVX5lR6mmPlHL4t6UpA3DCQTtfgnJeOPVbDg", "1949832577-d0MvzUjBJh2DKMlDgFAXy77SS7k2PJ4PBQYyEuz", "1949832577-OsJnqidm6VSc07npi8WsPLJBo2szhsbanKEWZGt", "1949832577-1t55fQBrrRg9flrErJ3CHJrTS5yBqXK5rJxwvnu", "1949832577-sl9jRHeQlsjgdQHKiqgRUOsp0pg7yg0wMft2oxc", "1949832577-E3nWLZV0lZUMPmWRCMdJMqFKgckp7lrTo5yzGUn", "1949832577-PsZN71NYPt6G4vnYb1mKZq5dLsy4QSdoL9CxW99", "1949832577-hCPEzQ1TifGuxARxCoMcFHLuhTcQbbUM7CQMjy3", "1949832577-31WK2j2tsuW4MUhfh7fozrSKxbtkF1vrhekJ9Un", "1949832577-dbxmmCWl06FLpVPztIS2eFbxerVzRohrYPQtCo7", "1949832577-m0TndsaAqrcnBBZFmdSqI30pcniQ6HnfWyOKQt4", "1949832577-I48H1hec9mWYMW7Q3RmbP3omx8pxvWGT0tnaRB6", "1949832577-tNM6Nlg9fJrh1ubEXxciPLFLpCbe6TEDM6NDNVd", "1949832577-l9FqnMXOSSY4INCh3mKonAdC6qkPOnv5COsj0xy", "1949832577-8yX5HiPz4NFjKlfgWuBHa6JUGdxDGf5ADBCenJQ", "1949832577-a5jUCaEM38mC8Kc6qD5w7qvPDaEzQOEHCWOydR7", "1949832577-vSOcPs9fD6UtoahwkZ2ydcWGBYAwciHn9voVMxY", "1949832577-ibHLrMXlltmOBZ8Kc45F17LOFZPaFnjFM4NMf5X", "1949832577-KvcJIgBwCeFmq2Z6ZKxZbHqtDIVw0KUhGKM7tTy", "1949832577-C7GVbWcMvsYi6Sc76Ohv9WxO8VidydcIJJm3pKX", "1949832577-U7OWDQOcLSMb1pfnFhxenYYEuRtxzqKWO5URphR", "1949832577-mOOQxIjwGJ6oq0NXbg0PJDklax9BVM6LryxCQMp", "1949832577-R7uTPKVzIWJZjVmG1iPJxJxG0i4xXWPlQ3HZ2FD", "1949832577-E1vqqgSVByuuFvpv5PwtLEQVH9dRGCZjsxjxinR" }; String[] tokenSecret = { "4DCwg7EPxOK63alLq9H0vAukdkSCPNrM8BxMyBn1L4Ode", "dJpOGI0UWWehrvqVEsIw2N68vxFRx59TA5N2g9SvnESfa", "mc1DHQ3wZDgMNDhdcfV5vb2L78oGcWeHZzCRi9qQ9JNaE", "7Sw2kLfj3qsafI90kvpfsm1WtZZQFt7CgXScWZ42CEz7r", "A385FGC7aN7to3D0J9lfISEexzU27Y0HvUFhfg6HgFN51", "4L2WzlrQeBfcpRQRDcCahPXtIBBZlCiL36NKeyltkUERO", "fylKSOCPvBJPJqYB4LCivrD7dh83wr7bNgTG5wjpp6UH3", "wXK9TqYB4OGBNiZJoKIVb1BTTaPV238HMu9uVXIFSmLFn", "Hiz9vOtkDcUBhGNqW1IrnLSd2j0S4GpNOqfWQTfzsBFCy", "MXdfjmyelhG0EfVCy3PrN39wZlg7tZU9QVEcbpJmvfrZo", "JY3Oh9Gpc3P113zAuMIwd1L96MIdNA3YsQj7UtPDMn5ll", "BZABUPjlGzNitziELUsw6PNuaMLalda0R4ou8CAcEO6C9", "RDjqdMkY5GrGugAfaI350ry2dqqBYCjLtvmGgBoGFZxRU", "qXLaLUwBcMsaEPxcO2smIWMDUm3ZMey8KkWO1w4k3i84p", "mmuSUPHRmDj1vLlw4V0RYKoEAXv3DLWofxnvifY7vMoBo", "Hxp8gCgQJOiOWaALEut8KruqsCQNEuwkcnrMTLfymKeZS", "HSm0A9SzFFTYD1qvYXrvXmr0jLQnnUAzE5eFxr1G6HFNM", "TPXCRnzc1oAVNWtSe25Zi8NMioDRywr1fMsM9sC2E8nu3", "BOqeR5hoglQXFzKPcDDxSlUCxc7S97ldIDntOReXIJJDl", "0hkcc63tFteCdqmnPo4LTdLlXpb9En08CaLrnZvKEzSxH", "qZGDZr1VIsr93lvHCyoYECnZRcQQ5GOL6bhOyIDHzkCz4", "Cs7882sRtKix7VPgDDRkgaEfwEO09vm4Cpw2zLVq7iTOc", "joQlsSMGbo3uE9ot64fl0HBPqDX7n0IsWxzx1WDtlIMg2", "QdSRoiwQ4kV2X1QNCrbYf6uyFunJ9HI4D4bIGrtHw8oFd", "YXvi4T5z5BoFPzcc3672es3eXeenTGOJi6HRzYgRdEWLp", "kuutouDNAo8tyOvrYFNdMqtqgSCpQa2NE4vuSVgOSIb90", "Fm0CDe5p3aHEakMeqDjfSObNlBGaE4l9mLGQdrN7u8RNp", "S1iyRvokXb7piwbREv3Vcs3IofDLuq3DFVlWoJWZ3CH4C", "6uiBLDAAIY9hhZZ85Jk2ltl1qGPZ7Da4T96Dd8tHVzKDk", "2G44mwCb1IM5HBILO3aPU8sE4nqkpjP5gWNK03Csa0K78" }; Date inicial_F = new Date(); String fecha_inicial; Date final_F = new Date(); String fecha_final; List<ObjetoBuscar> lisB = new ArrayList(); List<ObjetoBuscarFacebook> lisF = new ArrayList(); List<ObjetoBuscarTwitter> lisT = new ArrayList(); int varGlobal; int TokenUsado; public static void main(String[] args) throws NoSuchAlgorithmException, KeyManagementException { String metakey = "meta_twitter"; BD base = new BD(); base.iniciarConexionDPRN_Sitio(); Tracker_Twitter objeto = new Tracker_Twitter(); List<ObjetoBuscar> lista = null; System.out.println("antes de buscar"); lista = base.buscarMediosDeclaradosPorUsuario(objeto.inicial_F, objeto.final_F, metakey); objeto.guardarResultados_Twitter(lista, base, 0, 0); base.cerrarConexionDPRN_Sitio(); } public void guardarResultados_Twitter(List<ObjetoBuscar> lista, BD base, int contadorBase, int TokenIndice) throws NoSuchAlgorithmException, KeyManagementException { int nuevoContadorBase = 0; if (contadorBase >= lista.size()) { System.out.println("Termino en:" + contadorBase); } else { System.out.println("---------------------------------------------------------------------:P"); TrustManager[] trustAllCerts = { new X509TrustManager() { public X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); HostnameVerifier allHostsValid = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); System.out.println("Token : " + TokenIndice); System.out.println(consumerKey[TokenIndice]); try { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(consumerKey[TokenIndice]); cb.setOAuthConsumerSecret(this.consumerSecret[TokenIndice]); cb.setOAuthAccessToken(this.token[TokenIndice]); cb.setOAuthAccessTokenSecret(this.tokenSecret[TokenIndice]); Twitter unauthenticatedTwitter = new TwitterFactory(cb.build()).getInstance(); for (int numtTweets = contadorBase; numtTweets < contadorBase + 5; numtTweets++) { if (((ObjetoBuscar) lista.get(numtTweets)).getUrl().equals("") || ((ObjetoBuscar) lista.get(numtTweets)).getUrl() == null) { } else { System.out.println("Usuario: " + ((ObjetoBuscar) lista.get(numtTweets)).getUrl()); String usuariosinArroba = ((ObjetoBuscar) lista.get(numtTweets)).getUrl().replace("@", ""); System.out.println("" + usuariosinArroba); try { User usuario = unauthenticatedTwitter.showUser(usuariosinArroba); List<Status> ret = unauthenticatedTwitter.getRetweetsOfMe(); List<Status> favoritos = unauthenticatedTwitter.getFavorites(); Paging paging = new Paging(1, 1000); ResponseList<Status> statuses = unauthenticatedTwitter.getUserTimeline(usuario.getId(), paging); System.out.println("Followers: " + usuario.getFollowersCount()); System.out.println("Yo sigo: " + usuario.getFriendsCount()); List<String> listaTweets = new ArrayList(); List<Long> ListaRettewts = new ArrayList(); List<Integer> ListaFavoritos = new ArrayList(); List<Integer> ListaMenciones = new ArrayList(); List<Date> ListaFecha = new ArrayList(); List<Long> ListaIds = new ArrayList(); for (Status sta : statuses) { ListaIds.add(Long.valueOf(sta.getId())); listaTweets.add(sta.getText()); ListaRettewts.add(Long.valueOf(Long.parseLong(sta.getRetweetCount() + ""))); ListaMenciones.add(Integer.valueOf(sta.getUserMentionEntities().length)); ListaFecha.add(sta.getCreatedAt()); ListaFavoritos.add(Integer.valueOf(sta.getFavoriteCount())); } for (int i = 0; i < listaTweets.size(); i++) { base.guardarTrackTwitter_Log((Long) ListaIds.get(i), ((ObjetoBuscar) lista.get(numtTweets)).getUrl(), (String) listaTweets.get(i), (Date) ListaFecha.get(i), (Long) ListaRettewts.get(i), ((Integer) ListaFavoritos.get(i)).intValue(), ((Integer) ListaMenciones.get(i)).intValue()); } } catch (Exception e) { System.err.println("Entro al Try por :" + e); } nuevoContadorBase = numtTweets; } } System.out.println("Numero de Contador Base:" + nuevoContadorBase); guardarResultados_Twitter(lista, base, nuevoContadorBase + 1, TokenIndice + 1); } catch (NumberFormatException e) { System.err.println("Fallo por :" + e); } } } }