#!/usr/bin/env python
"""
Fetch, split and output filtered jobs descriptions from the Python Jobs Board.
You will need 'links' and BeautifulSoup.
"""
__author__ = 'Martin Blais <blais@furius.ca>'

# stdlib imports
import urllib, tempfile, re
from subprocess import *
from os.path import *

# other imports
from BeautifulSoup import BeautifulSoup


srcurl = 'http://www.python.org/community/jobs/'
tmpfile = '/tmp/jobs.html'

def get_python_job_descriptions():
    "Get the Python jobs descriptions (with caching)."
    if not exists(tmpfile):
        urllib.urlretrieve(srcurl, tmpfile)
    soup = BeautifulSoup(open(tmpfile))
    jobs = []
    for title in soup.findAll('h3'):
        sec = title.parent
        # print title
        tmpf = tempfile.NamedTemporaryFile('w')
        tmpf.write(str(sec))
        tmpf.flush()
        
        p = Popen(('links', '-dump', tmpf.name), shell=False,
                  stdout=PIPE)
        out, err = p.communicate()
        jobs.append(out)
    return jobs

def main():
    import optparse
    parser = optparse.OptionParser(__doc__.strip())
    opts, args = parser.parse_args()

    jobs = get_python_job_descriptions()

    # IMPORTANT: you'll have to hack this part to make it filter what YOU like.
    for job in jobs:
        if (re.search(r'\bweb\b', job, re.I) and
            re.search(r'california', job, re.I)):
            print '-' * 100
            print job

    
if __name__ == '__main__':
    main()

