J'avais récuperer un fichier jtwitter.jar puis le code processing etait divisé en deux partie :
Un onglet "thread" auquel je n'ai pas touché (et pas compris ^^) :
//
// ThreadedTwitter.java
// twitterServer
//
// Created by uros petrevski on 24/06/09.
// Copyright 2009 NoDesign. All rights reserved.
//
import winterwell.jtwitter.*;
import winterwell.jtwitter.Twitter.Status;
public class ThreadedTwitter extends Thread implements Runnable
{
private Twitter twitter;
private boolean ready;
private boolean keepRunning;
public int dream = 180000;
private String srch = "";
private String tPhrase="";
private String health="";
private String errLog="";
private int hits = 4;
private int nTweets = 0;
private String sensInstructions[] = {
"Search", "Hits", "Lang", "Twitter", "Reset", "Rate", "Fps"
};
private String buff = null;
ThreadedTwitter(int d, String srch)
{
dream = d*1000;
keepRunning=true;
ready=false;
boolean go = true;
this.srch = srch;
health = "i'm trying to connect to twitter";
while(go) {
try {
twitter = new Twitter();
health = "Connected to Twitter!";
go = false;
}
catch (Exception e) {
errLog = "can't initialize twitter object" + e;
go = true;
}
}
}
public void setHits(int n) {
this.hits = n;
}
public int getNResults() {
return nTweets;
}
public String getPhrase() {
if (ready==false)
{
// You asked for it too early. Or asked for a new one
// before getting this one...
return null;
}
else {
ready=false; // ok I'll give you, and I go back to sleep
return tPhrase;
//We can only return it once, should make user code easier.
}
}
private String getStatusFromAll() {
String o =" ";
List fromTwitter;
try {
println("searching");
fromTwitter = twitter.search(srch);
int hTarget = 0;
if (fromTwitter.size()<hits)
hTarget = fromTwitter.size();
else
hTarget = hits;
println("n results " + fromTwitter.size());
nTweets = fromTwitter.size();
/*
for (int i=0; i<hTarget; i++) {
Status an = (Status) fromTwitter.get(i);
String user = an.user.toString();
String userText = an.getText();
o+= userText + " ";
// println(o);
}
*/
// take only one result
Status an = (Status) fromTwitter.get(0);
String user = an.user.toString();
String userText = an.getText();
o = userText;
}
catch(TwitterException e) {
errLog =" NETWORK IS DOWN baby!!";
//will not update status if network is down
}
ready = true;
o+=" ";
return o;
}
public void goOn()
{
keepRunning = true;
}
public void close()
{
keepRunning=false;
}
public void run()
{
while(keepRunning)
{
if(ready==false)
tPhrase = getStatusFromAll();
try
{
this.sleep(dream);
}
catch (Exception e)
{
//we can be woken up early.. oh woe is us!
}
}
health = "threadedTwittLoader stopping";
}
}
Et un onglet "lampeDanza" dans mon cas, où j'entre le tag dans lequels on va chercher des mots posté dans les messages :
import processing.serial.*;
// The serial port:
Serial myPort;
boolean rouge = false;
boolean vert = false;
boolean bleu = false;
ThreadedTwitter tt = new ThreadedTwitter(3, "#lampeDanza"); // each 30 sec reload !!!ICI LE HASHTAG A CHERCHER!!!
void setup() {
// List all the available serial ports:
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Keyspan adaptor, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
// Send a capital A out the serial port
//myPort.write(65);
tt.start();
}
void draw() {
String phrase = tt.getPhrase();
if (phrase != null) {
println(phrase);
rouge = false;
vert = false;
bleu = false;
String strOrig = phrase;
int intIndex;
/// rouge
intIndex = strOrig.indexOf("rouge"); /// ICI LES MOTS A CHERCHER, "rouge" dans ce cas
if(intIndex == - 1) {
println("Rouge color not found");
}
else {
println("Found Rouge at index " + intIndex);
rouge = true;
}
/// vert
intIndex = strOrig.indexOf("vert");
if(intIndex == - 1) {
println("Vert color not found");
}
else {
println("Found Vert at index " + intIndex);
vert = true;
}
/// bleu
intIndex = strOrig.indexOf("bleu");
if(intIndex == - 1) {
println("Bleu color not found");
}
else {
println("Found Bleu at index " + intIndex);
bleu = true;
}
out();
/*
for (int i=0; i<phrase.length(); i++) {
myPort.write(phrase.charAt(i));
delay(300);
}
*/
}
else {
// println("0 results waiting for tweets");
}
}
void out() {
if (rouge || vert || bleu) {
myPort.write('k');
if (rouge) {
println("rouge");
myPort.write('r');
}
if (vert) {
println("vert");
myPort.write('g');
}
if (bleu) {
println("bleu");
myPort.write('b');
}
}
}
Voila j'espère que ca pourra t'aider. (si besoin du jtwitter.jar je ne sais pas comment te le faire passer -mail?)
Dernière modification par Bozon (2012-12-26 15:16:23)
Hors ligne
Je veux bien le fichier et la librairy, je l'ai téléchargée, mais je dois surement rater quelque chose...
... Sur le site de Processing, ils ont dit que je devait utiliser le Agent.printSearch() ....Je cherche ce que c'est en même temps.
Hors ligne