Blogreader

Contenu du snippet

Comme la plupart d'entre vous j'ai un blog, eh oui :) .
Le seul problème c'est que j'en voulais gratuit et sans pubs, j'ai trouvé pour cela un bon hébergeur sur http://www.zeblog.com.
Malheureusement la gratuité à un prix: le peu de fonctionnalités. Notamment une qui me désespérait jusqu'à ce que j'écrive ce programme: être avertit lorsque que j'ai de nouveaux commentaires afin de répondre à mes lecteurs le plus rapidement possible.
Comme vous l'avez donc deviné, j'ai écris un programme java qui se connecte à mon blog toutes les 15 minutes et me dit qui affiche une boite de dialogue si j'ai de nouveaux commentaires ;-) .
Enjoy!

Source / Exemple :


import javax.swing.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.text.*;

public class BlogReader extends Thread{
	private String fileName;
	private String blogUrl;
	private String lastCount;
	private String currentCount;
	private BufferedInputStream fileIn;
	private int sleepTimeMs;
	
	public BlogReader(String fileName, String blogUrl, int sleepTimeMin){
		this.fileName	= fileName;
		this.blogUrl	= blogUrl;
		this.sleepTimeMs	= sleepTimeMin*60*1000;
	}
	
	private boolean fileInRead(){
		// Saved count
		int car;
		StringBuffer buffer = new StringBuffer();
		try{
			fileIn = new BufferedInputStream(new FileInputStream(fileName));

			while((car = fileIn.read()) != -1){
				buffer.append((char)car);
			}
			
			lastCount = new String(buffer.toString());
			
			if(car == -1)
				fileIn.close();
			
			return true;
		}catch (FileNotFoundException ex){
			JOptionPane.showMessageDialog(null, "Error BlogReader: you have to create and initialize the file "+fileName+" manually !");
			System.exit(0);
			return false;
		}catch (IOException ex){
			System.out.println("Error BlogReader!");
			ex.printStackTrace();
			return false;
		}
	}
	
	private boolean saveNewInformations(String newCount){
		// Save new information
		OutputStream fileOut;
		byte [] bytes= newCount.getBytes();
		try{
			fileOut = new BufferedOutputStream(new FileOutputStream(fileName));
			for(int i = 0; i < newCount.length(); i++ )
				fileOut.write(bytes[i]);
			fileOut.flush();
			fileOut.close();
			return true;
		
		}catch (FileNotFoundException ex){
			JOptionPane.showMessageDialog(null, "Error BlogReader: cannot open the file "+fileName+" for writing !");
			return false;
		}catch (IOException ex){
			System.out.println("Error BlogReader!");
			ex.printStackTrace();
			
			return false;
		}
	}
		
	private void search (){
			if(!fileInRead())
				System.exit(0);
			
			System.out.println( DateFormat.getTimeInstance().format(new Date()));
			System.out.println("Last count was : "+lastCount+" comments");
		
			StringBuffer buffer = new StringBuffer();
			int car;
					
			// String
			String strToSearch = new String("commentaires</li>");
			String page = new String();
					
			try{
				// Connection
				URL myBlog = new URL(blogUrl);
				URLConnection connection = myBlog.openConnection();
				System.out.println("Connection...");
				// 	read the URL
				InputStream is = connection.getInputStream();
						
				while( (car = is.read()) != -1){
					//System.out.print((char)car);
					buffer.append((char)car);
				}
				page = buffer.toString();
				// Parse HTML
				if(page.contains(strToSearch)){
					int firstOccurrence = buffer.indexOf(strToSearch);
					buffer.delete(0, firstOccurrence-(lastCount.length()+1)); // +1 for the space
					firstOccurrence = buffer.indexOf(strToSearch);
					buffer.delete(strToSearch.length()+(lastCount.length()+1)-5, buffer.length());
					
					currentCount = new String(buffer.substring(0, buffer.indexOf(" ")));
					System.out.println("Current count is : "+currentCount+" comments");
					if(lastCount.compareTo(currentCount) != 0){
						System.out.println("You have new comment(s) on your blog !");
						JOptionPane.showMessageDialog(null, "BlogReader: you have new comment(s) on your blog!");
						saveNewInformations(currentCount);
						connection = null;
					}else{
						System.out.println("No new comment...");
					}
				}else{
					JOptionPane.showMessageDialog(null, "Error BlogReader: the doesn't contain strToSearch !");
				}
			}catch (MalformedURLException ex){
				JOptionPane.showMessageDialog(null, "Error BlogReader: Malformed URL!");
			}catch (IOException ex){
				System.out.println("Error BlogReader!");
				ex.printStackTrace();
			}
		}
	
	// run methode
	public void run() {
		while(true){
			search();
			try{
				sleep(sleepTimeMs);
			}catch (Exception e){
				e.printStackTrace();
			}
		}
		
	}
	
	// main
	static public void main(String [] arg){
		System.out.println("-----[BlogReader by psyphi]-----");
		System.out.println("--> http://psyphi.zeblog.com <--");
		System.out.println("--------------------------------");
		BlogReader br = new BlogReader("last_count", "http://psyphi.zeblog.com", 15);
		br.start();
	}
}

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.