Sunteți pe pagina 1din 2

package alarm;

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.MessageDigest;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

public class Main {


public static void main(String[] args) throws UnsupportedEncodingExcepti
on, NoSuchAlgorithmException, IOException {
new Main();
}
public Main() throws UnsupportedEncodingException, NoSuchAlgorithmExcept
ion, IOException {
try {
System.out.println("Parser started.");
String parseURL = "http://nk99.org/node/14";
System.out.println("Parse URL: " + parseURL + "\n\n");
String lastCheckSum = null;
for(int cc=0; cc<2; cc++) {
URL url = new URL(parseURL);
URLConnection conn = url.openConnection();
conn.connect();
BufferedReader in = new BufferedReader(new InputStreamReader
(conn.getInputStream()));
String code = null;
String s = "";
while((s = in.readLine())!=null){
code = code + s;
}
byte[] bytesOfMessage = code.getBytes("UTF-8");
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
String newCheckSum = null;
for(int i=0; i<thedigest.length; i++) {
if(newCheckSum == null) newCheckSum = thedigest[i] + "";
else newCheckSum = newCheckSum + thedigest[i];
}
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:
mm:ss");
Date date = new Date();
if(cc != 0) System.out.print(dateFormat.format(date) + " ");
if(newCheckSum.equals(lastCheckSum)) {
System.out.println("not changed");
}
else {
if(cc != 0) {
System.out.println("NEW CHANGES!!!");
try{
AudioInputStream stream = AudioSystem.getAud
ioInputStream(new File("alarm.wav"));
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.
class, stream.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info)
;
clip.open(stream);
clip.start();
} catch (Exception e)
{
e.printStackTrace();
}
try {
Thread.sleep(180000);
} catch(InterruptedException e) {
}
break;
}
}
lastCheckSum = newCheckSum;
if(cc != 0) cc--;
try {
Thread.sleep(180000);
} catch(InterruptedException e) {
}
}
} catch (MalformedURLException e) {
} catch (IOException e) {
}
}
}