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
[DESIGN PATTERNS] PARTIE 2: DIP: DEPENDENCY INVERSION PRINCIPLE[DESIGN PATTERNS] PARTIE 2: DIP: DEPENDENCY INVERSION PRINCIPLE par tja
C'est le dernier principe des principes du Design Orienté Objet (The Principles of Object Oriented Design) fondés par Robert C. Martin plus connu sous le pseudonyme d'Uncle Bob.
l'image empruntée de LosTechies.
Je ne traite pas les principes dans...
Cliquez pour lire la suite de l'article par tja TECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURSTECHDAYS PARIS 2010 : SHAREPOINT 2010 POUR LES DéVELOPPEURS par ROMELARD Fabrice
Animé par: Laurent Cotton Le développement dans SharePoint 2010 passe par plusieurs axes qui seront évoqués dans cette session, mais plus particulièrement les développements simples lié au besoin Business Business Connectivity Services Ce BCS es...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOURTECHDAYS PARIS 2010 : PLEINIèRE DERNIER JOUR par ROMELARD Fabrice
Cette session est la dernière pleinière de ces 3 jours de TechDays Paris 2010. Généralement, cette troisième journée est plus axée sur l'avenir vu par Microsoft. Après un retour sur l'avenir vu par la Science Fiction ou par ...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice UNE JOLIE-HORLOGE ET PAS QU'UN PEU !UNE JOLIE-HORLOGE ET PAS QU'UN PEU ! par neodante
Pour les possesseurs d'iPhone, ça y est Bijin Tokei - qui se traduit littéralement en Français par " Jolie Horloge " - est arrivé et GRATUITEMENT s'il vous plaît ! Après la version Tokyo, Hokkaido, night club, racing, Gal, "pour les mademoiselles'", . voi...
Cliquez pour lire la suite de l'article par neodante TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Forum
AIDE TALEAU JAVAAIDE TALEAU JAVA par ziliass
Cliquez pour lire la suite par ziliass
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
Comparez les prix

HTC Magic
Entre 429€ et 429€
|