Help with Twitter 4j and general geolocation of user in an area
in
Contributed Library Questions
•
6 months ago
Hi,
I found this sketch on this forum posted by mrslash and it contains a code that is exactly what I am looking for. I am completely new to processing and I am not sure how to troubleshoot this problem. I am looking to filter tweets by a specific location and then have the data for the number of tweets in a given time period saved as a csv file. When I run this sketch I receive the error "cannot find a class or type named "date". I have my Oauth and the twitter4j library installed. Can anyone try to run this sketch and let me know if they get the same problem or let me know what I need to do to fix this issue?
Link to the previous post:
https://forum.processing.org/topic/twitter-4j-and-general-geolocation-of-user-in-an-area?reply=true
// here you have to enter your personal data. You'llget them from https://dev.twitter.com/apps
String ConsumerKey = "*******";
String ConsumerSecret = "*******";
String oauth_token = "*******";
String oauth_token_secret = "*******";
TwitterFactory tf;
String geoURL;
double lat;
double lon;
double res;
String resUnit;
import java.util.List;
void setup() { println("starting...");
println();
ConfigurationBuilder cb = new ConfigurationBuilder(); //ConfigurationBuilder declared cb is a new instance of this
cb.setDebugEnabled(true)
.setOAuthConsumerKey(ConsumerKey) //sets the Consumer Key String
.setOAuthConsumerSecret(ConsumerSecret) //sets the Consumer Secret String .setOAuthAccessToken(oauth_token)
.setOAuthAccessTokenSecret(oauth_token_secret);
tf = new TwitterFactory(cb.build());
}
void draw() {
println("entering void draw...");
Date ddate = new Date();
System.out.println(ddate + "\n"+ "\n");
// These values are from Milan and probably you'llhave to change them according to your position
lat = 45.49985;
lon = 9.191093;
res = 25;
resUnit="km";
try {
//FILE SAVE DATA (change the path from below)
String filename = "/Users/yourusername/Documents/Processing/twitterLog.csv";
boolean newFile=false;
File f = new File(filename);
if(!f.exists()){
println("creating file...");
f.createNewFile();
newFile=true;
println("new file "+ filename +" has been created " + newFile);
}else{
println("opening the existing file..." + filename);
}
BufferedWriter writer = new BufferedWriter(new FileWriter(f, true));
//
if (newFile == true){
writer.write("day,month,hour,min,sec,user,longitude,latitude,\n");
writer.flush();
}
int count = 0;
try {
// this value below is about 3 hours of running time
while (count!=1000) {
Twitter twitter = tf.getInstance();
try {
//TWITTER DATA
Query query = new Query().geoCode(new GeoLocation(lat,lon), res, resUnit);
query.rpp(100);
QueryResult result = twitter.search(query);
List <Tweet> tweets = result.getTweets();
for (int i=0; i < tweets.size(); i++) {
Tweet tweet = (Tweet)tweets.get(i);
if (tweet.getGeoLocation()!= null){
println("Found!");
GeoLocation loc=tweet.getGeoLocation();
//Transorming variables
String myLon = Double.toString(loc.getLongitude());
String myLat = Double.toString(loc.getLatitude());
String user = tweet.getFromUser();
Date date = tweet.getCreatedAt();
// Splitting the date in day-month-hour-min-sec
DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
DateFormat ObjMonth = new SimpleDateFormat("MM");
String MyMonth = ObjMonth.format(date);
DateFormat ObjDay = new SimpleDateFormat("dd");
String MyDay = ObjDay.format(date);
DateFormat ObjHour = new SimpleDateFormat("kk");
String MyHour = ObjHour.format(date);
DateFormat ObjMin = new SimpleDateFormat("mm");
String MyMin = ObjMin.format(date);
DateFormat ObjSec = new SimpleDateFormat("ss");
String MySec = ObjSec.format(date);
String[] data = new String[8];
data[0] = MyDay; data[1] = MyMonth; data[2] = MyHour; data[3] = MyMin; data[4] = MySec; data[5] =
user; data[6] = myLon; data[7] = myLat;
//this if case will fill the array just if the search is null
if (data[0] ==null){
data[0] = "24"; data[1] = "05"; data[2] = "17"; data[3] = "19"; data[4] = "55"; data[5] = "ikkio"; data[6] =
"9.2033"; data[7] = "45.4853";
}
for (int k=0; k < data.length; k++) {
writer.write(data[k] + ",");
}
writer.write("\n");
writer.write("\n");
writer.flush();
//println("Created at: " + MyDay +"-"+ MyMonth +" at: "+ MyHour +"-"+ MyMin +"-"+ MySec + " @" + user +
": longitude: "+ myLon + " latitude: "+ myLat );
}
}
println("--");
println("sleeping...");
println("--");
println("Count: "+ count);
println();
println();
count +=1;
Thread.sleep(10400);
} catch (TwitterException te) {
te.printStackTrace();
println("TWITTER ERROR, Failed to search tweets: " + te.getMessage());
exit();
}
}
//exit WHILE (COUNT o TRUE)
writer.close();
exit();
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (IOException ioe) {
println("IO/ERROR: " + ioe);
}
}
Any help is appreciated.
Thanks,
-Ryan
1