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 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)


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