Bah tout est dans le titre je crois :)
C'est en fait juste une adaptation java de mon code en perl ici :
http://www.pythonfrance.com/codes/AUTO-FOLLOW-UNFOLLOW-TWITTER-PERL-UTILISANT-NET-TWITTER_52919.aspx.
Alors pour ceux qui auraient pas lu le titre, bah ça s'connecte a Twitter toutes les 5 minutes,
regarde si on à des nouveaux followers ou si certains ont arrêté de nous suivre, et le cas échéant les suit ou arrête de les suivre...
Ça sert pas a grand chose mais c'est plus pour apprendre a utiliser JTwitter et Oauth.Signpost qu'autre chose :)
Vous l'aurez remarqué, le code est un peu (très ?) brouillon ... Pas un spécialiste de java à la base :°)
Source / Exemple :
import javax.swing.*;
import java.io.*;
import java.awt.event.*;
import java.awt.*;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.text.SimpleDateFormat;
import winterwell.jtwitter.*;
import winterwell.jtwitter.Twitter.User;
import oauth.signpost.basic.*;
public class AutoTwit extends JFrame{
private static final long serialVersionUID = 1L;
static AutoTwit here;
static Container frame1, frame2;
static JFrame auth;
static JLabel lbl1, lbl2, lbl3, lbl4, lbl5, lbl6;
static JTextField jauth;
static JList list1, list2;
static JButton btn;
static JScrollPane jsp1, jsp2;
static String APP_KEY = "oHzZtZwniPyC2xxxxxxXXXXX";
static String APP_SECRET = "DziLvo6iBEHNNoT7WFFB4xekOShYXxxxxxXXXXX";
static OAuthSignpostClient client = null;
static Twitter jtwit;
static String []toks;
static Vector<String> vlist, events;
static boolean connectOk = false;
static Timer t;
public AutoTwit(){
auth = new JFrame("Authentification");
auth.setSize(400,250);
auth.setVisible(true);
frame2 = auth.getContentPane();
frame2.setLayout(null);
lbl4 = new JLabel("Une fenetre vers le site de Twitter va s'ouvrir.");
lbl4.setBounds(65,0,300,30);frame2.add(lbl4);
lbl5 = new JLabel("Veuillez autoriser l'application et entrer");
lbl5.setBounds(75,20,300,30);frame2.add(lbl5);
lbl6 = new JLabel("le code fourni par Twitter ci-dessous.");
lbl6.setBounds(80,40,300,30);frame2.add(lbl6);
jauth = new JTextField("");
jauth.setBounds(65,120,120,25);frame2.add(jauth);
btn = new JButton("Valider");
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent evt){
ValidOAuth(evt);
}
});
btn.setBounds(200,120,120,25);frame2.add(btn);
this.setTitle("AutoFollower");
this.setSize(500,400);
WindowListener Win = new WindowListener(){
public void windowClosing(WindowEvent e){System.exit(0);}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){System.exit(0);}
public void windowDeactivated(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowOpened(WindowEvent e){}
};
this.addWindowListener(Win);
frame1 = this.getContentPane();
frame1.setLayout(null);
lbl1 = new JLabel("Followers : ");
lbl2 = new JLabel("Logs des changements :");
lbl3 = new JLabel("Mise à jour des followers...");
lbl1.setBounds(50,0,100,30);
lbl2.setBounds(200,0,150,30);
lbl3.setBounds(10,338,160,30);
frame1.add(lbl1);
frame1.add(lbl2);
frame1.add(lbl3);
list1 = new JList(vlist);
list2 = new JList(events);
jsp1 = new JScrollPane(list1);
jsp2 = new JScrollPane(list2);
jsp1.setBounds(10,35,140,300);
jsp2.setBounds(170,35,300,300);
frame1.add(jsp1);
frame1.add(jsp2);
t = new javax.swing.Timer(1000*60, this.new BoucleMaj());
t.start();
}
public static void main(String args[]){
vlist = new Vector<String>();
events = new Vector<String>();
DefaultOAuthProvider provider = new DefaultOAuthProvider(
"https://api.twitter.com/oauth/request_token",
"https://api.twitter.com/oauth/access_token",
"https://api.twitter.com/oauth/authorize");
here = new AutoTwit();
here.setVisible(true);
try{
toks = getTokens();
client = new OAuthSignpostClient(APP_KEY, APP_SECRET, toks[0], toks[1]);
client.setProvider(provider);
auth.setVisible(false);
jtwit = new Twitter("DynasyX", client);
connectOk = true;
MAJFollowers();
}catch (Exception e){
here.setVisible(false);
client = new OAuthSignpostClient(APP_KEY, APP_SECRET, "oob");
client.setProvider(provider);
client.authorizeDesktop();
}
}
class BoucleMaj implements ActionListener{
public void actionPerformed(ActionEvent e){
if(connectOk){
MAJFollowers();
}
}
}
static public void MAJFollowers(){
List<User> fwers = jtwit.getFollowers();
Vector<String> olds = new Vector<String>();
vlist.removeAllElements();
for(int i=0;i<fwers.size();i++){
vlist.add(fwers.get(i).getScreenName());
}
list1.updateUI();
lbl1.setText("Followers : " + fwers.size());
SimpleDateFormat heure = new SimpleDateFormat("HH:mm:ss");
lbl3.setText("Derniere MAJ : " + heure.format(new Date()));
try{
olds = get_old();
events.add("Recuperation anciens followers [OK]");
list2.updateUI();
/* Ya t-il de nouveaux followers ? Si oui on le follow back */
for(int i=0;i<vlist.size();i++){
if(!olds.toString().toLowerCase().contains(vlist.get(i).toLowerCase())){
events.add("Nouveau follower : " + vlist.get(i));
list2.updateUI();
jtwit.follow(vlist.get(i));
}
}
/* Un follower a-t'il cessé de nous suivre ? (Enfoiré) */
for(int i=0;i<olds.size();i++){
if(!vlist.toString().toLowerCase().contains(olds.get(i).toLowerCase())){
events.add(olds.get(i) + " a cessé de vous suivre !");
events.add("Utilisateur auto-unfollowed :)");
try{
jtwit.stopFollowing(olds.get(i));
}catch (Exception e){
events.add(olds.get(i) + " a supprimé son compte ou changé de nom");
}
list2.updateUI();
}
}
}catch (Exception e){
events.add("Fichier anciens followers inexistant. Creation");
list2.updateUI();
}
try{
save_old(vlist);
}catch (Exception e){
events.add("Impossible de sauvegarder les followers");
list2.updateUI();
}
}
static public void ValidOAuth(ActionEvent e){
String verifier = jauth.getText();
client.setAuthorizationCode(verifier);
toks = client.getAccessToken();
try {
saveTokens(toks);
} catch (Exception e1) {
e1.printStackTrace();
}
jtwit = new Twitter("DynasyX", client);
connectOk = true;
MAJFollowers();
here.setVisible(true);
auth.setVisible(false);
}
static public Vector<String> get_old() throws Exception{
Vector<String> olds = new Vector<String>();
BufferedReader fin = new BufferedReader(new FileReader("followers.txt"));
String line = null;
while((line = fin.readLine())!=null){
olds.add(line);
}
fin.close();
return olds;
}
static public void save_old(Vector<String> old) throws Exception{
PrintWriter fout = new PrintWriter(new FileWriter("followers.txt"));
for(int i=0;i<old.size();i++){
fout.println(old.get(i));
}
fout.close();
}
static public String[] getTokens() throws Exception{
BufferedReader ftok = new BufferedReader(new FileReader("tokens.txt"));
String stok[] = ftok.readLine().split(" ");
ftok.close();
return stok;
}
static public void saveTokens(String []str) throws Exception{
PrintWriter fout = new PrintWriter(new FileWriter("tokens.txt"));
fout.print(str[0] + " " + str[1]);
fout.close();
}
}
Conclusion :
Voilà, si vous voyez des choses à modifier, des remarques sur l'optimisation (qui pour l'instant est au niveau 0 voir -1 :P ), des questions, n'hésitez pas ... ^^
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.