Getting tweet results based on geolocation specification
in
Contributed Library Questions
•
5 months ago
Dear all,
For a school project I'm gathering peoples 'thoughts' by using the following code:
And it works pretty well. At first I used just the searchword "Denk" (which means "Think" in dutch), and then I started specifying my search results, for example, I wanted only dutch tweets with the word denk. This was also succesfull by adding: "lang:nl" to the search-line. Then I wanted to specify a location to look for tweets, which I should be able to do by adding: near:\"den haag\" within:15mi. And this is where all goes wrong, cause once I added this location to my search, I got a NullPointerException. I tried increasing the range from 15mi to 1500mi, just to see if it would get any results at all, but I still got a NullPointerException.
This is the specific line that I'm struggling with:
So my question is; how can I change it so I can find location specific tweets?
I hope this was clear enough, thanks for reading.
For a school project I'm gathering peoples 'thoughts' by using the following code:
- import processing.pdf.*;
boolean record;
int teller = 3705;
int lijn;
import twitter4j.conf.*;
import twitter4j.internal.async.*;
import twitter4j.internal.org.json.*;
import twitter4j.internal.logging.*;
import twitter4j.http.*;
import twitter4j.internal.util.*;
import twitter4j.api.*;
import twitter4j.util.*;
import twitter4j.internal.http.*;
import twitter4j.*;
//import twitter4j.org.json.*;
import twitter4j.*;
import twitter4j.http.*;
//import twitter4j.examples.*;
PFont font;
PFont font2;
PImage b;
int y[] = new int[3];
float target[] = new float[3];
//float timer = 200;
String[] LABEL = {
"x", "x", "x"
};
//Use this line for altering search results
String searchTerms[] = {
"denk -replies -filter:replies -RT lang:nl near:\"den haag\" within:15mi", "vind",
"denk"
};
String gt[] = new String[3];
String gf[] = new String[3];
String gn[] = new String[3];
int now;
int lastSecond;
String startTime;
PImage profile;
long lastId[] = new long[3];
boolean connected = true;
void setup() {
size(900, 600);
//size(800,450); //.75 size
//size(533,300); //halfsize
smooth();
// font = loadFont("Sansa-Normal-48.vlw");
font = createFont("Aaux ProBlack", 60);
font2 = createFont("Aaux ProLight", 60);
// font = loadFont("AutoThree-Regular-48.vlw");
// font2 = loadFont("TrebuchetMS-Italic-22.vlw");
if (connected) {
Twitter twitter = new Twitter();
for (int party = 0; party < 3; party++) {
try {
Query query = new Query(searchTerms[party]);
QueryResult result = (QueryResult) twitter.search(query);
java.util.List tweets = result.getTweets();
println(tweets.size());
Tweet t = (Tweet) tweets.get(0);
lastId[party] = t.getId();
println(lastId[party]);
gt[party] = t.getText();
gf[party] = t.getFromUser();
gn[party] = t.getProfileImageUrl();
}
catch (Exception e) {
System.out.println(e);
}
}
}
for (int i = 0; i < 3; i++) {
target[i]=height-12;
}
startTime = hour() + ":" + trim(nfs(minute(), 2));
textAlign(LEFT);
}
void draw() {
teller = teller+1;
if (record) {
beginRecord(PDF, teller+".pdf");
}
now = second();
{
background(1, 1, 255);
//KLEURVLAK ACHTER DE TWEET
//noStroke();
//fill(2, 2, 216);
//rect(140, 0, 600, height);
//BEWEGENDE LIJN
stroke(226, 204, 0);
strokeWeight(2);
line(1, lijn, width, lijn);
lijn = lijn+1;
if (lijn > height) {
lijn = 0;
}
}
int maximum = max(y[0], y[1], y[2]);
int total = y[0]+y[1]+y[2];
fill(200, 200);
textFont(font, 15);
fill(0, 0, 200);
textFont(font2, 15);
for (int i = 0; i < 1; i++) {
if (y[i]!=0) target[i] = 0.95*target[i] + 0.05*map(y[i], 0, maximum, height, height/3);
stroke(0);
strokeWeight(4);
fill(255);
// bericht
textFont(font2, 35);
text(gt[i], 144, 200, 600, 300); //width*.14 use to be 150, .83 replaces 500, .215 replaces 280, .5 replaces 300
textFont(font, 35);
fill(0);
// username
text(gf[i]+ " denkt:", 140, 100, 600, 600); //width*.14 use to be 150, .83 replaces 500, .215 replaces 280, .5 replaces 300
}
//every 3 seconds
if (now!= lastSecond && now%3==0) {
//do a new twitter search since the last ID
if (connected) {
Twitter twitter = new Twitter();
for (int party = 0; party < 3; party++) {
try {
Query query = new Query(searchTerms[party]);
query.setSinceId(lastId[party]);
QueryResult result = (QueryResult) twitter.search(query);
java.util.List tweets = result.getTweets();
//println(tweets.size());
if (tweets.size()>0) {
Tweet t = (Tweet) tweets.get(0);
lastId[party] = t.getId();
gt[party] = t.getText();
gf[party] = t.getFromUser();
gn[party] = t.getProfileImageUrl();
println(gt[party]);
//println(lastId[party]);
y[party]=min(height/6*height, y[party]+tweets.size()); // height/6 replaces 100
}
}
catch (Exception e) {
System.out.println(e);
}
}
}
else {
for (int i = 0; i < 3; i++) {
y[i]+= int(random(10));
}
}
}
lastSecond = now;
if (record) {
endRecord();
record = false;
}
}
//String timestamp() {
//Calendar now = Calendar.getInstance();
//return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
//}
void keyReleased() {
if (key == 'p' || key == 'P') record = true;
}
And it works pretty well. At first I used just the searchword "Denk" (which means "Think" in dutch), and then I started specifying my search results, for example, I wanted only dutch tweets with the word denk. This was also succesfull by adding: "lang:nl" to the search-line. Then I wanted to specify a location to look for tweets, which I should be able to do by adding: near:\"den haag\" within:15mi. And this is where all goes wrong, cause once I added this location to my search, I got a NullPointerException. I tried increasing the range from 15mi to 1500mi, just to see if it would get any results at all, but I still got a NullPointerException.
This is the specific line that I'm struggling with:
- String searchTerms[] = {
"denk -replies -filter:replies -RT lang:nl near:\"den haag\" within:15mi", "vind",
"denk"
};
So my question is; how can I change it so I can find location specific tweets?
I hope this was clear enough, thanks for reading.
1