I tried to construct a method that gives me the results from the search of a text in twitter, but only gives me the 6 most recent results, and I need more, 50~100~200... really... Some more..
I tried a lot of for
ordo while
bucles tryin' only to get more results. Failed.
Is there some method to do it? I tried with getMentions()
, but for a error and bad results and another uses, I need to do it with twitter.query(Search)
.
Is there someone that knows about it?
I'm playin' with TWITTER4J - 4.0.2 Version.
Here's my code :
List<Status> estados = new ArrayList<Status>();
BufferedReader br = null;
FileWriter fichero = null;
PrintWriter pw = null;
File file = new File("dbmenciones.txt");
try {
Query query = new Query();
query.setQuery("#tits");
query.setResultType(Query.MIXED);
query.setCount(100);
QueryResult qr = null;
long lastid = Long.MAX_VALUE;
int vv = 0;
do {
vv++;
if (512 - estados.size() > 100){
query.setCount(100);
} else {
query.setCount(512 - estados.size());
}
qr = twitter.search(query);
estados.addAll(qr.getTweets());
System.out.println("Obtenidos "+estados.size()+" tweets.");
for (Status stt : estados){
if (stt.getId()<lastid) lastid = stt.getId();
}
query.setMaxId(lastid-1);
} while (vv < 14);
for (Status status : estados){
System.out.println(status.getId()+" : "+status.getText());
System.out.printf("Publicado : %s, @%-20s ,dijo: %s\n",
status.getCreatedAt().toString(),
status.getUser().getScreenName(), cleanText(status.getText()));
listaUsuarios.add("\""+status.getUser().getId()+"\";\""+status.getUser().getScreenName()+"\";\""+status.getUser().getFollowersCount()+"\"");
fichero = new FileWriter("C:\\Documents and Settings\\a671\\Escritorio\\"+file, true);
pw = new PrintWriter(fichero);
System.out.println("Added : "+listaUsuarios.get(listaUsuarios.size()-1));
pw.println(listaUsuarios.get(listaUsuarios.size()-1));
if (null != fichero){
fichero.close();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != fichero)
fichero.close();
} catch (Exception e2) {
e2.printStackTrace();
}
try {
if (br != null) br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
First I start the query, and then I use do while
to get a lot of times the results.
At the last time, I enter the results in a .txt file.
Nothing awful.
Help.
Thanks.
I have been playing with Twitter4Java and I simplified it. You just need to use the library called
twitter4j-core-4.0.1.jar
.If you let me to give you an advice, I will strongly recommend you to split the code into classes, so the code will be easier to understand. Here is the program that will search the word you are looking for, you just need to change the IO library for your preferred one (or just
System.out.println
):Main.java:
ResultsAnalyzer.java:
TermFinder.java:
Timer.java:
Constants.java:
In.java:
Out.java:
IO.java:
Hope it helps. Please let me know if you have any issue.
Clemencio Morales Lucas.