Accueil > > > BLOGREADER
BLOGREADER
Information sur la source
Description
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
- 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();
- }
- }
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();
}
}
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Forum
PARSER DE XMLPARSER DE XML par fioreT
Cliquez pour lire la suite par fioreT
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|