Example usage for twitter4j Paging Paging

List of usage examples for twitter4j Paging Paging

Introduction

In this page you can find the example usage for twitter4j Paging Paging.

Prototype

public Paging(int page, long sinceId) 

Source Link

Usage

From source file:tweete.Tweete.java

License:Open Source License

public void showTimeline() {

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true).setOAuthConsumerKey("######################")
            .setOAuthConsumerSecret("######################")
            .setOAuthAccessToken("############################################")
            .setOAuthAccessTokenSecret("############################################");
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();/*from  ww  w .  j  a v  a  2s  .co  m*/

    try {
        ResponseList<Status> a = twitter.getUserTimeline(new Paging(1, 10));
        String statuses = "";
        for (Status b : a) {
            statuses = statuses + b.getText() + "\n\n---------------------------------------\n\n";

        }

        new TweeteTimeline().Timeline(statuses);
    } catch (TwitterException te) {
        //te.printStackTrace();

        if (401 == te.getStatusCode()) {
            System.out.println("Unable to get the access token.");
        }

        else if (92 == te.getStatusCode()) {
            System.out.println("SSL is required");
        }

        else {
            System.out.println("Failed to get timeline: " + te.getMessage());
            System.exit(-1);
        }
    } catch (Exception e) {
        System.out.println("Something went wrong");
    }
}

From source file:tweetmining.MiningFunctions.java

/**
 * This method mines data from a particular twitter user.
 * @param user String which has the name of a Twitter user without @ 
 *//*from  w ww. ja va  2s . c o m*/
public void MineFromUser(String user) {
    int pageno = 1;
    List<Status> statuses = new ArrayList();

    while (true) {

        try {

            int size = statuses.size();
            Paging page = new Paging(pageno++, 100);
            statuses.addAll(twitter.getUserTimeline(user, page));
            if (statuses.size() == size)
                break;
        } catch (TwitterException e) {

            e.printStackTrace();
        }
    }

    System.out.println(statuses.size());

    for (Status st : statuses) {
        GeoLocation loc = st.getGeoLocation();
        if (loc != null) {
            System.out.println("Loc not null");
            Double lat = loc.getLatitude();
            Double lon = loc.getLongitude();
            pw.println(lat.toString() + ";" + lon.toString() + ";" + st.getUser().getName());
        }
        pw2.println(st.getUser().getName() + "-----" + st.getText());
    }

    pw.close();
    pw2.close();
}

From source file:twitbak.DirectMessageBak.java

License:Open Source License

/**
 * Retrieves page n of user's direct messages. Hard coded to max retrieval value of 200
 * to help mitigate rate limiting issues.
 *//*from   w w  w . j av  a2  s .  c  o m*/
public List<DirectMessage> getStatusPage(int n) throws TwitterException, InterruptedException {
    RateLimitStatus rls = twitter.getRateLimitStatus();
    if (rls.getRemainingHits() < 10) {
        System.out.println("Pausing to let Twitter's rate limiting catch up...");
        //         BackupWorker.setStatus("Pausing to let Twitter's rate limiting catch up...");
        Thread.sleep(30000);
    }
    System.out.println("Retrieving a block of up to 200 direct messages...");
    List<DirectMessage> dmPage = twitter.getDirectMessages(new Paging(n, 200));
    return dmPage;
}

From source file:twitbak.MentionBak.java

License:Open Source License

public List<Status> getStatusPage(int n) throws TwitterException, InterruptedException {
    RateLimitStatus rls = twitter().getRateLimitStatus();
    if (rls.getRemainingHits() < 10) {
        System.out.println("Pausing to let Twitter's rate limiting catch up...");
        Thread.sleep(30000);/*from  www  .  j  a v a 2  s.co  m*/
    }
    System.out.println("Retrieving a block of up to 200 mentions...");
    List<Status> statusPage = twitter().getMentions(new Paging(n, 200));
    return statusPage;
}

From source file:twitbak.StatusBak.java

License:Open Source License

/**
 * Retrieves page n of user's Status messages. Hard coded to max retrieval value of 200
 * to help mitigate rate limiting issues.
 *///from w  ww  .  j  a v a2  s  .c om
public List<Status> getStatusPage(int n) throws TwitterException, InterruptedException {
    RateLimitStatus rls = twitter.getRateLimitStatus();
    if (rls.getRemainingHits() < 10) {
        System.out.println("Pausing to let Twitter's rate limiting catch up...");
        Thread.sleep(30000);
    }
    System.out.println("Retrieving status updates starting from number " + n * 200);
    List<Status> statusPage = twitter.getUserTimeline(new Paging(n, 200));
    System.out.println("Retrieved status updates starting from number " + n * 200);
    return statusPage;
}

From source file:twitter.metrics.TwitterMetrics.java

/**
 * @param args the command line arguments
 *///  ww  w .j  av a  2s. co  m
public static void main(String[] args) {

    try {

        /*Parte que guarda en un archivo*/
        // Se crea el libro
        HSSFWorkbook libro = new HSSFWorkbook();

        // Se crea una hoja dentro del libro
        HSSFSheet hoja = libro.createSheet();

        // Se crea una fila dentro de la hoja
        HSSFRow fila = hoja.createRow(0);

        // Se crea una celda dentro de la fila
        HSSFCell celda = fila.createCell(1);

        // Se crea el contenido de la celda y se mete en ella.
        HSSFRichTextString texto = new HSSFRichTextString("Metricas de Twitter");
        celda.setCellValue(texto);
        /************************************/

        /*Conexion con Mongo DB*/
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        DB db = mongoClient.getDB("JavaMongoTwitter");

        DBCollection datos = db.getCollection("Datos");

        /***********************/

        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true);

        cb.setOAuthConsumerKey("TobUISZXWUhDda04ZBtFGQ");
        cb.setOAuthConsumerSecret("7xurVN3iP6VDcBfKdFJxVuNsJjExERFYNmQIDgtg");

        cb.setOAuthAccessToken("849956971-GJBiORhLIuWK4i3MJ2YCd4vidh65N1GzPIb6duXk");
        cb.setOAuthAccessTokenSecret("4MJgPS9grxVuKbczrPCdSjNnumhcWs7t7OLy2F4kkpOdu");

        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();

        User u = twitter.showUser("Pringles");

        System.out.println("Nombre: " + u.getName());
        System.out.println("Seguidores: " + u.getFollowersCount());
        System.out.println("Favoritos: " + u.getFavouritesCount());
        System.out.println("Seguidos: " + u.getFriendsCount());
        System.out.println("Ubicacion: " + u.getLocation());
        System.out.println("Descripcin: " + u.getDescription());
        System.out.println("");

        fila = hoja.createRow(1);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Nombre:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getName()));

        fila = hoja.createRow(2);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Seguidores:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getFollowersCount() + ""));

        fila = hoja.createRow(3);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Favoritos:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getFavouritesCount() + ""));

        fila = hoja.createRow(4);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Seguidos:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getFriendsCount() + ""));

        fila = hoja.createRow(5);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Ubicacin:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getLocation() + ""));

        fila = hoja.createRow(6);
        celda = fila.createCell(0);
        celda.setCellValue(new HSSFRichTextString("Descripcin:"));
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString(u.getDescription() + ""));

        fila = hoja.createRow(7);
        celda = fila.createCell(3);
        celda.setCellValue(new HSSFRichTextString("Tweets!!!"));

        fila = hoja.createRow(9);
        celda = fila.createCell(1);
        celda.setCellValue(new HSSFRichTextString("IdTweet"));
        celda = fila.createCell(2);
        celda.setCellValue(new HSSFRichTextString("Cuenta"));
        celda = fila.createCell(3);
        celda.setCellValue(new HSSFRichTextString("Tweet"));
        celda = fila.createCell(4);
        celda.setCellValue(new HSSFRichTextString("Geolocation"));
        celda = fila.createCell(5);
        celda.setCellValue(new HSSFRichTextString("Place"));
        celda = fila.createCell(6);
        celda.setCellValue(new HSSFRichTextString("Retweets"));
        celda = fila.createCell(7);
        celda.setCellValue(new HSSFRichTextString("Favoritos"));

        Paging paging = new Paging(1, 1000);
        ResponseList<Status> s = twitter.getUserTimeline(u.getId(), paging);
        int i = 0;
        int filaNum = 10;
        for (Status st : s) {

            BasicDBObject obj = new BasicDBObject();

            obj.append("idTweet", s.get(i).getId() + "");
            obj.append("Cuenta", u.getName());
            obj.append("Tweet", s.get(i).getText());
            obj.append("Geolocation", s.get(i).getGeoLocation());
            obj.append("Place", s.get(i).getPlace());
            obj.append("Reteews", s.get(i).getRetweetCount());
            obj.append("Favoritos", s.get(i).getFavoriteCount());

            fila = hoja.createRow(filaNum);
            celda = fila.createCell(1);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getId() + ""));
            celda = fila.createCell(2);
            celda.setCellValue(new HSSFRichTextString(u.getName()));
            celda = fila.createCell(3);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getText()));
            celda = fila.createCell(4);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getGeoLocation() + ""));
            celda = fila.createCell(5);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getPlace() + ""));
            celda = fila.createCell(6);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getRetweetCount() + ""));
            celda = fila.createCell(7);
            celda.setCellValue(new HSSFRichTextString(s.get(i).getFavoriteCount() + ""));

            i++;
            filaNum++;

            datos.insert(obj);
        }

        FileOutputStream elFichero = new FileOutputStream("Metricas_Twitter.xls");
        libro.write(elFichero);
        elFichero.close();

        /********************************/

        System.out.println(i);

        //User usuario = twitter.showUser("@aaron21007");
        //    List<Status> statuses = twitter.getHomeTimeline();
        //    System.out.println("Showing home timeline.");
        //    for (Status status : statuses) {
        //        System.out.println(status.getUser().getName() + ":" +
        //                           status.getText());
        //    }

        //        Twitter unauthenticatedTwitter = new TwitterFactory(cb.build()).getInstance();
        //        
        //          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();
        ////
        //          User usuario = unauthenticatedTwitter.showUser("@aaron21007");
        //          List<Status> ret = unauthenticatedTwitter.getRetweetsOfMe();
        //          List<Status> favoritos = unauthenticatedTwitter.getFavorites();
        //          Paging paging = new Paging(1, 1000);
        //          ResponseList<Status> statuses = unauthenticatedTwitter.getUserTimeline(usuario.getId(), paging);
        //          
        //          
        //          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()));
        //              System.out.println(sta.getText());
        //              
        //          }
        //          
        //          
        //        
        //       
        //        
    } catch (Exception e) {
        System.err.println("Fallo................." + e);
    }
}

From source file:TwitterDownload.TwitterHandler.java

public List<Status> getUserTimeline(String searchPhrase, int pageSize) throws TwitterException {
    String userName = searchPhrase;

    if (!userName.contains(" ")) {
        if (!userName.startsWith("@"))
            return getSearchTweets(userName, pageSize);

        long lastID = Long.MAX_VALUE;
        ArrayList<Status> tweets = new ArrayList<Status>();
        int count = 200;

        try {/*from ww  w .j  a  va 2  s  . com*/
            int i = 0;
            while (tweets.size() < pageSize) {
                i++;
                if (pageSize - tweets.size() > 200)
                    count = 200;
                else
                    count = pageSize - tweets.size();

                Paging page = new Paging(i, count);
                //page.maxId(lastID);

                List<Status> l = twitter.getUserTimeline(userName, page);

                tweets.addAll(l);

                if (l.size() < 200) {
                    break;
                }

                for (Status t : tweets) {
                    if (t.getId() < lastID)
                        lastID = t.getId();
                    else if (t.getId() == lastID)
                        break;
                }
            }

            return (List<Status>) tweets;
        } catch (TwitterException ex) {
            String s = ex.toString();
            //TODO: needs to be refined to only include user not found exception
            if (ex.resourceNotFound())
                return getSearchTweets(searchPhrase, pageSize);
        } catch (Exception ex) {
            String s = ex.toString();
            return null;
        }
    }
    //else
    return getSearchTweets(userName, pageSize);
}

From source file:twittermarkovchain.Main.java

public static void main(String[] args) throws TwitterException, IOException {
    Args.parseOrExit(Main.class, args);
    Twitter twitter = TwitterFactory.getSingleton();

    List<String> tweets = new ArrayList<>();
    File file = new File(user + ".txt");
    if (file.exists()) {
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
        String line;/*from  w w w  .j  a  va2  s. c  om*/
        while ((line = br.readLine()) != null) {
            tweets.add(line);
        }
    } else {
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
        int total = 0;
        int page = 1;
        int size;
        do {
            ResponseList<Status> statuses = twitter.timelines().getUserTimeline(user, new Paging(page++, 200));
            size = statuses.size();
            total += size;

            for (Status status : statuses) {
                if (status.getInReplyToUserId() == -1 && status.getRetweetedStatus() == null) {
                    String text = status.getText().replaceAll("\n", " ");
                    bw.write(text);
                    bw.newLine();
                    tweets.add(text);
                }
            }
        } while (size > 0);
        bw.close();
    }

    // We need to generate a map of pair frequencies indexed by the first in the pair
    Map<String, Map<String, Integer>> frequencyMap = tweets.stream().flatMap((String s) -> {
        Stream.Builder<Pair> builder = Stream.builder();
        String last = null;
        for (String current : s.toLowerCase().replaceAll("https?://.+\\b", "").replaceAll("[^a-z@# ]", "")
                .split(" ")) {
            if (current.equals(""))
                continue;
            if (last == null) {
                builder.add(new Pair("", current));
            } else {
                builder.add(new Pair(last, current));
            }
            last = current;
        }
        if (last != null) {
            builder.add(new Pair(last, ""));
        }
        return builder.build();
    }).collect(Collectors.toMap(p -> p.s1, p -> ImmutableMap.of(p.s2, 1), (m1, m2) -> {
        HashMap<String, Integer> newmap = new HashMap<>(m1);
        for (Map.Entry<String, Integer> e : m2.entrySet()) {
            String key = e.getKey();
            Integer integer = newmap.get(key);
            if (integer == null) {
                newmap.put(key, 1);
            } else {
                newmap.put(key, integer + e.getValue());
            }
        }
        return newmap;
    }));

    // Random!
    Random random = new SecureRandom();

    // Check using language
    JLanguageTool language = new JLanguageTool(Language.ENGLISH);

    for (int i = 0; i < 1000; i++) {
        StringBuilder sb = new StringBuilder();
        // Now that we have the frequency map we can generate a message.
        String word = "";
        do {
            Map<String, Integer> distribution = frequencyMap.get(word);
            int total = 0;
            for (Map.Entry<String, Integer> e : distribution.entrySet()) {
                total += e.getValue();
            }
            int which = random.nextInt(total);
            int current = 0;
            for (Map.Entry<String, Integer> e : distribution.entrySet()) {
                Integer value = e.getValue();
                if (which >= current && which < current + value) {
                    word = e.getKey();
                }
                current += value;
            }
            if (sb.length() > 0) {
                if (word.length() > 0) {
                    sb.append(" ");
                    sb.append(word);
                }
            } else {
                sb.append(word.substring(0, 1).toUpperCase());
                if (word.length() > 1)
                    sb.append(word.substring(1));
            }
        } while (!word.equals(""));
        sb.append(".");
        List<RuleMatch> check = language.check(sb.toString());
        if (check.isEmpty()) {
            System.out.println(sb);
        }
    }
}

From source file:TwitterStats.Facade.Twitter.java

public List<Status> getTuitsCuenta(String user, int estudio, int cantidad) throws TwitterException {
    ResponseList res;//from ww w  .j a  v a2 s  .  c  o m
    List<Status> lista = new ArrayList<>();
    for (int i = 1; i <= estudio / 200; i++) {
        res = twitter.getUserTimeline(user, new Paging(i, 200));
        lista.addAll(res);
    }

    Collections.sort(lista, new Comparator<Status>() {

        public int compare(Status s1, Status s2) {
            return s1.getFavoriteCount() > s2.getFavoriteCount() ? -1
                    : (s1.getFavoriteCount() < s2.getFavoriteCount()) ? 1 : 0;
        }
    });

    if (lista.size() > cantidad) {
        return lista.subList(0, cantidad);
    } else {
        return lista;
    }
}

From source file:TwitterStats.Facade.Twitter.java

public List<Status> getTuitsCuenta(long id, int estudio, int cantidad) throws TwitterException {
    ResponseList res;//from w  w  w .  j  a v  a  2s.  c o  m
    List<Status> lista = new ArrayList<>();
    for (int i = 1; i <= estudio / 200; i++) {
        res = twitter.getUserTimeline(id, new Paging(i, 200));
        lista.addAll(res);
    }

    Collections.sort(lista, new Comparator<Status>() {

        public int compare(Status s1, Status s2) {
            return s1.getFavoriteCount() > s2.getFavoriteCount() ? -1
                    : (s1.getFavoriteCount() < s2.getFavoriteCount()) ? 1 : 0;
        }
    });

    if (lista.size() > cantidad) {
        return lista.subList(0, cantidad);
    } else {
        return lista;
    }
}