Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Practical /Prog/ Challenge

Name: Anonymous 2007-09-08 17:17 ID:P8+/2q3A

The challenge is to make a PROGWATCH program

What it does is, scans this file:
http://dis.4chan.org/prog/subject.txt
every 1 min, 10 mins or 30 seconds or so..

When a change occurs (e.g. someone makes a new post or whatever) then it should exec some program set by the user, with the given args

so for example, you could set it up to open your webbrowser for  the page, or get growl to display "New thread on /prog/, title: Ive read SICP!" etc etc

I will post mine afterwards.. anyway good luck and GO FOR IT!

Name: Anonymous 2007-09-09 9:15 ID:Alm8D+R2


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Vector;

class Observer extends Thread
{
    private URL                url;
    private String[]         subOld;
    private String[]         subNew;
    private BufferedReader     br;

    Observer( String path )
    {       
        try {
            url = new URL( path );
        } catch (MalformedURLException e) {
            System.out.println("Error, malformed URL: " + e.getLocalizedMessage());
        }
        subNew     = getList();
    }

    public void run()
    {
        while( true )
        {
            sleep( 100 );
           
            subOld         = subNew;
            subNew         = getList();
            int change     = getChange();
           
            if( change != -1 )
            {
                String[] threadLine = subNew[change].split("<>");
                int nr = Integer.valueOf(threadLine[4]);
                if( nr == 1 )
                    System.out.println("Thread created: " + threadLine[0] + ", by " + threadLine[1]);
                else
                    System.out.println("Thread bumped: " + threadLine[0] + ", post number " + nr);
               
                try {
                    Runtime.getRuntime().exec("firefox http://dis.4chan.org/read/prog/"; + threadLine[3] + "/" + nr + "-" + nr);
                } catch (IOException e) {}
            }
        }
    }
    private String[] getList(){
        Vector<String>     list        = new Vector<String>();
        int             line        = 0;
        String[]         stringList = null;
       
        try {
            br = new BufferedReader(new InputStreamReader(
                    url.openStream()));
            while( br.ready() )
            {
                list.add( br.readLine() );
                line ++;
            }
            stringList = new String[line+1];
           
            line = 0;
            for(String s:list)stringList[line++]=s;
            br.close();
           
        } catch (IOException e) {
            System.out.println("Error: " + e.getLocalizedMessage());
        }
        return stringList;
    }
    private void sleep( int t )
    {
        try {
            super.sleep( t );
        } catch (InterruptedException e) {}
    }
    private int getChange()
    {
        int len = subOld.length < subNew.length?
                subOld.length:subNew.length;
        for( int i = 0; i < len-1; i++)
        {
            if( !subOld[i].equals( subNew[i] )
                    && subOld[i] != null
                    && subNew[i] != null)
                return i;
        }
        return -1;
    }
}

public class DirObserver{
    public static void main( String[] args ){
        Thread Observer = new Thread( new Observer( "http://dis.4chan.org/prog/subject.txt"; ) );
        Observer.start();
    }
}


Produces output like:
Thread bumped: Practical /Prog/ Challenge, post number 49
Thread created: sage, by sage

And opens only the newest post in firefox.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List