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 8:46 ID:Alm8D+R2

Thread bumped: Practical /Prog/ Challenge, by Anonymous

Name: sage 2007-09-09 8:49 ID:Alm8D+R2

trying something

Name: Anonymous 2007-09-09 8:49 ID:Heaven

fuck

Name: Anonymous 2007-09-09 8:50 ID:Heaven

post

Name: Anonymous 2007-09-09 8:52 ID:Heaven

post

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

It opens firefox now to view the thread, cool.

Name: Anonymous 2007-09-09 9:05 ID:Heaven

test

Name: Anonymous 2007-09-09 9:08 ID:Heaven

Now only the post

Name: sage 2007-09-09 9:10 ID:Heaven

sage

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.

Name: Anonymous 2007-09-09 9:23 ID:2naHzNYd

>>50
ENTERPRISE LEVEL CODE

Name: Anonymous 2007-09-09 9:30 ID:Heaven

Test.

Name: Anonymous 2007-09-09 10:04 ID:Heaven

Test2.

Name: Anonymous 2007-09-09 10:14 ID:Heaven

Test2.

Name: Anonymous 2007-09-09 10:25 ID:Heaven

2Test.

Name: Anonymous 2007-09-09 10:28 ID:EBCj6yXA

#!/usr/bin/env python2.5

from os import system
from time import sleep
from urllib import urlopen

# The location of the subject file
subject_file = 'http://dis.4chan.org/prog/subject.txt';

# Thread and post fetch URLs
thread_url = 'http://dis.4chan.org/read/prog/%(thread_num)d';
post_url = thread_url + '/%(latest_post)d'

# Check interval in seconds
interval = 60

# Both callbacks get called with a single thread dictionary as an
# argument. See `dictify' for the list of keys.

def created(thread):
    "Called when a new thread is created."
    print "New thread: ``%(title)s''" % thread
    # system('open ' + thread_url % thread)

def posted(thread):
    "Called when a new post is posted in a thread."
    print "New post in ``%(title)s'' by %(poster)s: %(latest_post)d" % thread
    # system('open ' + post_url % thread)

def dictify(thread):
    "Convert a thread to a dict."
    return {
        'title':       x[0],
        'poster':      x[1],
        'thread_num':  int(x[3]),
        'latest_post': int(x[4])
        }

def parse_list(l):
    "Parse the specified subject file, returning a set of threads."
    return set([tuple(x.strip().split('<>')) for x in l])

def thread_num(thread):
    "Return the thread number."
    return thread[3]

def new_threads(changed, old_threads):
    "Return a list of newly created (as opposed to bumped) threads."
    old_nums = map(thread_num, old_threads)
    return set([x for x in changed if thread_num(x) not in old_nums])

if __name__ == '__main__':
    old_threads, threads = None, None

    while True:
        if threads: old_threads = threads
        threads = parse_list(urlopen(subject_file))
        if not old_threads: continue

        # Which threads have been updated since last check?
        changed = threads - old_threads
        if changed:
            new = new_threads(changed, old_threads)
            for x in new:
                created(dictify(x))
            for x in changed - new:
                posted(dictify(x))

        sleep(interval)

Name: Anonymous 2007-09-09 10:36 ID:Heaven

test

Name: Anonymous 2007-09-09 10:55 ID:Heaven

>>56
Blergh.

def dictify(thread):
    "Convert a thread to a dict."
    return {
        'title':       thread[0],
        'poster':      thread[1],
        'thread_num':  int(thread[3]),
        'latest_post': int(thread[4])
        }

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();
    }
}

Name: Anonymous 2007-09-09 11:49 ID:Heaven

>>59
Still the same ENTERPRISE QUALITY.

Name: Anonymous 2007-09-09 12:01 ID:Alm8D+R2

Revision 3: Made the code more bubbly, to look more enterprise style.




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];
           
            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]: null ) );
            }
        }
    }
   
    private void sleep( int t )
    {
        try {
            super.sleep( t );
        } catch ( InterruptedException e ) {}
    }
}

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

Name: Anonymous 2007-09-09 12:02 ID:Heaven

>>59
import java.net.MalformedURLException;

Name: Anonymous 2007-09-09 12:19 ID:Heaven

>>62
OMG WHY YOU GO IMPORT EXCPETIONZZ?=???? WHEN ECXEPTION THEN YOU PROGRAM GOES BOOM LOL SO DONT IMPORT THEM

Name: Anonymous 2007-09-09 12:24 ID:14U848Hb

>>63
dude, to print e.getLocalizedMessage()
and I didn't wrote the program or in fact anything in java.

Name: Anonymous 2007-09-09 12:27 ID:Heaven

>>64
LOL YOU SOUCK, ALL MY ETNERPRISE SOLUTIONS ARE PORGRAMMED WITH

EXPORT EPXCITON , NO IMPORT NIGGERS

Name: sage 2007-09-09 12:38 ID:Heaven

sage

Name: Anonymous 2007-09-09 12:40 ID:Heaven

changed the program to sage this thread everytime something is bumped

Name: sage 2007-09-09 12:50 ID:Heaven

sage

Name: sage 2007-09-09 12:51 ID:Heaven

sage

Name: sage 2007-09-09 12:52 ID:Heaven

sage

Name: sage 2007-09-09 12:53 ID:Heaven

sage

Name: sage 2007-09-09 12:53 ID:Heaven

sage

Name: sage 2007-09-09 12:54 ID:Heaven

sage

Name: sage 2007-09-09 12:55 ID:Heaven

test

Name: sage 2007-09-09 13:00 ID:Heaven

LISP

Name: sage 2007-09-09 13:01 ID:Heaven

'LISP

Name: sage 2007-09-09 13:02 ID:Heaven

'Practical

Name: sage 2007-09-09 13:02 ID:Heaven

test test

Name: sage 2007-09-09 13:03 ID:Heaven

'Practical

Name: sage 2007-09-09 13:04 ID:Heaven

"Why

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