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 11:42 ID:Alm8D+R2

Changed my code a bit, I store the threads in a hashmap now. Updates less often. Output is still the same. Firefox commented out.


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

class Observer extends Thread
{
    private URL                         url;
    private String[]                  subNew;
    private HashMap<Integer,Integer> hm;
    private BufferedReader              br;

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

    public void run()
    {
        while( true )
        {
            sleep( 30000 );
           
            subNew     = getList();
            getHashMap( subNew );
        }
    }
   
    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;
    }
   
    /**
     * Generate int-int HashMap, mapping the thread number to current posts.
     * @param threadList Threads to map
     */
    private void getHashMap( String[] threadList )
    {
        // Practical /Prog/ Challenge<>Anonymous<><>1189285965<>57<><>1189348564
        // 0: Name
        // 1: OP
        // 2: Empty ( eMail? )
        // 3: OP Number
        // 4: Post Number
        // 5: Empty
        // 6: Last Post Number
        for(String s:threadList)
        {
            if(s==null)break;
            String[] thread = s.split("<>");
            int th = Integer.valueOf(thread[3]);
            int nr = Integer.valueOf(thread[4]);
           
            if( hm.containsKey( th ) )
            {
                if ( hm.get( th ) < nr )
                {
                    hm.put( th, nr );
                    System.out.println("Thread bumped: " + thread[0] + ", post number " + thread[4]);
                    //Runtime.getRuntime().exec("firefox http://dis.4chan.org/read/prog/"; + thread[3] + "/" + nr + "-" + nr);
                }
            } else {
                hm.put( th, nr );
                System.out.println("Thread created: " + thread[0] + ((thread[1].length()>0)?", by " + thread[1]:""));
            }
        }
    }
   
    private void sleep( int t )
    {
        try {
            super.sleep( t );
        } catch (InterruptedException e) {}
    }
}

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

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